LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-27-2005, 01:54 PM   #1
P0ldy
LQ Newbie
 
Registered: Feb 2005
Posts: 28

Rep: Reputation: 15
Having some trouble with sed


OK, so here's what I'm trying to do. I've got a file with, for example, the following lines:

Code:
First_Name: John
Last_Name: Smith
Address: 123 Anywhere
Phone: 555-5555
 
First_Name: Jane
Last_Name: Jones
Address: 321 Anywhere
Phone:111-1111
I'm trying to use sed to place each line from First_Name: to Phone: on a single line delimited by tabs, and then the next "First_Name" block on its own line with the same tab delimited fields. However, I just can't seem to figure out the right expression. If I use

sed 's/First_Name*\n/\t/g' foo > bar

nothing happens. Just using sed 's/\n/\t/g' gives me nothing, as if there are no newline characters in this text file at all, when there clearly are.

Anyone have any ideas? I've also tried to outright delete newline characters with sed '/\n/d' and nothing happens. If I use sed '/$/d', however, it erases the contents of the entire file, when I'm only interested in deleting the final end-of-line character, the newline.

Can anyone help?
 
Old 12-27-2005, 04:08 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Here's an example using awk. I'll give sed a try later.
Code:
awk 'BEGIN { FS="\n" ; RS="\n\n" ; OFS="\t" } {print$1,$2,$3,$4}' file.txt
 
Old 12-27-2005, 04:57 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683
You may want to use the "tr" program to replace the new-lines with tabs. This would be an easier solution than using Sed. Using sed, because it's a line editor, you would have to build up the fields in the HOLD buffer before you could perform the substitution.
 
Old 01-04-2006, 04:07 AM   #4
P0ldy
LQ Newbie
 
Registered: Feb 2005
Posts: 28

Original Poster
Rep: Reputation: 15
Thanks for the replies.

I did use tr in the end. Awk's a bit too intimidating for me.
 
Old 01-04-2006, 11:27 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I'm curious ... how did you stop tr from putting ALL lines into one line?


Cheers,
Tink
 
Old 01-04-2006, 02:26 PM   #6
P0ldy
LQ Newbie
 
Registered: Feb 2005
Posts: 28

Original Poster
Rep: Reputation: 15
I figured that would be a problem. So, after switching out the \n for \t, I used sed -e 's/First_Name/\nFirst_Name/g', because I wanted each newline to begin with First_Name.
 
Old 01-04-2006, 04:26 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by P0ldy
Thanks for the replies.

I did use tr in the end. Awk's a bit too intimidating for me.
Hmmmm: If awk is intimidating, then sed is your worst nightmare.....
To really write test manipulation scripts, you need both.

In fairness, I went thru a "sed" phase, where I struggled mightly to make it do everything. Then I bit the bullet and learned awk. Now I know enough about both of them to look up the exact syntax when I need to do something.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
About sed orgazmo Programming 10 05-19-2005 09:21 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
sed !!1 letmein Linux - General 18 07-31-2003 09:36 AM
trouble ahead, trouble behind....trouble with mplayer Goonie Linux - Software 3 07-02-2003 02:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:10 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