LinuxQuestions.org
Visit Jeremy's Blog.
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, 12:13 PM   #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, 12:24 PM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Jessie
Posts: 592

Rep: Reputation: 119Reputation: 119
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 12:27 PM.
 
Old 08-10-2012, 12:28 PM   #3
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
try this

Quote:
sed -e 's/^[ \t]*/|/g'
 
Old 08-13-2012, 12:18 AM   #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, 03:17 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 04:03 PM
[SOLVED] sed command to replace 7th tab with newline tonyfreeman Programming 4 03-11-2011 05:36 PM
Sed replace string up to tab snakefact Linux - Newbie 17 12-13-2009 08:38 PM
sed - find and replace command bullshit Programming 9 01-05-2006 04:25 AM
sed command to insert tab? iluvatar Linux - Software 4 06-29-2004 02:56 AM


All times are GMT -5. The time now is 12:23 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration