LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2011, 01:40 PM   #1
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Rep: Reputation: 30
sed command to replace 7th tab with newline


I have a tab delimited file. I need to replace the 7th tab with a new line.

I tried the following command (but it does not work):

sed 's/\t{7}/\n/' block_01.txt

... the above simply spits out the original file.

If I write the next command, it replaces the first tab in each line with a newline. How can I make it replace the 7th tab in each line?

sed 's/\t/\n/' block_01.txt

-- Tony
 
Old 03-11-2011, 01:49 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
This site provides the best explanation of replacing occurrences of a character and such:
http://unix-school.blogspot.com/2011...-contents.html

Cheers,

Josh
 
1 members found this post helpful.
Old 03-11-2011, 01:52 PM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
This should do it:

Code:
sed -r "s/([^\t]*(\t[^\t]*){6})\t/\1\n/" block_01.txt
Breakdown:
Code:
( [^\t]*                  (\t[^\t]*){6}                          )   \t
 a word with no tabs in  + (a tab and a word with no tabs in) * 6 + a tab
Replaced by
\1\n
Everything inside the first pair of brackets (in this case, everything up to the 7th tab) + a newline
EDIT: Ah, beaten to it again... Darn you :P

Hope this helps,

Last edited by Snark1994; 03-11-2011 at 01:56 PM.
 
2 members found this post helpful.
Old 03-11-2011, 02:03 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
sed 's/\t/\n/7' block_01.txt
 
2 members found this post helpful.
Old 03-11-2011, 04:36 PM   #5
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Original Poster
Rep: Reputation: 30
elegant sed script 's/\t/\n/7' blah.txt

Thanks people,

I used Snark1994's suggestion which worked; but, I really like colucix's code for it's elegance ... so I'm using that now in my code.

Thanks everyone for your help!

-- Tony
 
  


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
[SOLVED] sed command to replace file extension leighya Linux - Newbie 18 04-22-2012 03:20 AM
[SOLVED] How to replace newline pattern in file by other newline pattern in a shell script XXLRay Linux - Software 9 11-29-2010 07:57 AM
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 > Non-*NIX Forums > Programming

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

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