LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-10-2012, 11:13 AM   #1
anshaa
LQ Newbie
 
Registered: Aug 2012
Posts: 12

Rep: Reputation: Disabled
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.
 
Old 08-10-2012, 11:24 AM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
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.

Last edited by towheedm; 08-10-2012 at 11:27 AM.
 
Old 08-10-2012, 11:28 AM   #3
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Rep: Reputation: 42
try this

Quote:
sed -e 's/^[ \t]*/|/g'
 
Old 08-12-2012, 11:18 PM   #4
anshaa
LQ Newbie
 
Registered: Aug 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-13-2012, 02:17 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace string using sed command bloodstreetboy Linux - General 6 07-06-2012 03:03 PM
[SOLVED] sed command to replace 7th tab with newline tonyfreeman Programming 4 03-11-2011 04:36 PM
Sed replace string up to tab snakefact Linux - Newbie 17 12-13-2009 07:38 PM
sed - find and replace command bullshit Programming 9 01-05-2006 03:25 AM
sed command to insert tab? iluvatar Linux - Software 4 06-29-2004 01:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration