LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed command to replace \t(Tab) with '|' (https://www.linuxquestions.org/questions/linux-newbie-8/sed-command-to-replace-%5Ct-tab-with-%7C-4175421424/)

anshaa 08-10-2012 11:13 AM

sed command to replace \t(Tab) with '|'
 
I need to replace the tab(\t) in my text file to '|'.

command used:

sed '/\\t/|/g'

it replace all t with |.

Please help me out.

towheedm 08-10-2012 11:24 AM

Tab is recognized as \t by SED. By escaping it, it now becomes just t. And you forgot to include the s in your SED command.

cbtshare 08-10-2012 11:28 AM

try this

Quote:

sed -e 's/^[ \t]*/|/g'

anshaa 08-12-2012 11:18 PM

hi cbtshare,

When i used this code.
cat infile
1234 test
weye test1 break
576 test break title
2369 test line break
tite break
234589 test like break

sed -e 's/^[ \t]*/|/g' infile1 > aa.dat

cat aa.dat
|1234 test
|weye test1 break
|576 test break title
|2369 test line break
|ite break
|234589 test like break

Please let me know how to solve this.

David the H. 08-13-2012 02:17 PM

Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.

I've seen this happen before. Your file is probably set with dos-style line endings. You need to convert your files to unix-style endings for most *nix tools to work properly.

sed can be used to convert the line endings at the same time. Just add a second expression to remove the extra carriage return.

Code:

sed -e 's/\r$//' -e 'y/\t/|/' infile >outfile
Notice how I used sed's 'y' command instead of 's', which translates one set of characters into another.

But actually, most of the time when you want to do this, tr is usually the preferred tool.

Code:

tr -d '\r' <infile | tr '\t' '|' >outfile


All times are GMT -5. The time now is 08:08 AM.