How to replace specific Symbols [like ' , ^ , \t' , etc..,] in a tsv file using linux commands? -


i trying replace symbol combinations in tsv file below,

occurance of ^' should replaced nothing occurance of /t' should replaced /t occurance of ' should replaced nothing

finally, should replace starting , ending " in cell nothing whereas " occuring in middle of cell value should remain same. [eg : "apple iphone" should replaced apple iphone , 7" samsung led tv should remain same]

sample file :

7" inch tv "apple iphone" \t' india \t' ^' exit 'paps'

if copy above line excel different columns.

code tried :

sed "s/\^'//g" ${file}.txt > ${file}_new.txt sed "s//t'//t/g" ${file}_new.txt > ${file}_new_1.txt 

please, me out of friends.

thanks in advance.

while still don't understand problem, want to:

  • specify multiple filters sed
  • make sure special characters (e.g. / used delimit search-pattern , replace-pattern) escaped.

e.g. following should trick:

sed -e "s|/t'|/t|g" \     -e "s|[^]'||g" \     -e "s|'||g" \     -e 's|"\t"|\t|g' -e 's|^"||' -e 's|"$||' \     "${file}.txt" > "${file}_fixed.txt" 

this using pipe-character | instead of / structure search/replace command. uses 4 different replace-patterns directly. order important: start biggest-chunks replaced, , gradually make them smaller (so replace ^' before replace '). last line simplistic way rid of double-quotes (first removing doublequote-tab-doubleqoute between columns; removing leading or trailing doublequotes)


Comments