Need some help with sed

P0ldy

Senior member
Dec 13, 2004
420
0
0
OK, so here's what I'm trying to do. I've got a file with, for example, the following lines:

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 every line, when I'm only interested in deleting the final end-of-line character, the newline.

Can anyone help?
 

thesix

Member
Jan 23, 2001
133
0
0
I suppose you don't have to use sed.
Here's a awk solution:

# ./myawk
First_Name: John Last_Name: Smith Address: 123 Anywhere Phone: 555-5555
First_Name: Jane Last_Name: Jones Address: 321 Anywhere Phone: 111-1111

Is this what you want ?
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
It's not sed,and it's dirty, but it works (test.txt being a text file with the example text provided):
cat test.txt | tr '\n' '\t' | sed -e 's/First_Name/\nFirst_Name/g' -e 's/^\n//g' > test2.txt

EDIT: Cleaned it up a teeeeeeeny leeeeeettle bit.
 

P0ldy

Senior member
Dec 13, 2004
420
0
0
Originally posted by: n0cmonkey
It's not sed,and it's dirty, but it works (test.txt being a text file with the example text provided):
cat test.txt | tr '\n' '\t' | sed -e 's/First_Name/\nFirst_Name/g' -e 's/^\n//g' > test2.txt

EDIT: Cleaned it up a teeeeeeeny leeeeeettle bit.

That worked well, thanks. If it gets the job done, I'm not worried about eloquence.

Thanks for the help, thesix, but awk is a bit too intimidating for me. :)
 

bersl2

Golden Member
Aug 2, 2004
1,617
0
0
Originally posted by: P0ldy
Originally posted by: n0cmonkey
It's not sed,and it's dirty, but it works (test.txt being a text file with the example text provided):
cat test.txt | tr '\n' '\t' | sed -e 's/First_Name/\nFirst_Name/g' -e 's/^\n//g' > test2.txt

EDIT: Cleaned it up a teeeeeeeny leeeeeettle bit.

That worked well, thanks. If it gets the job done, I'm not worried about eloquence.

Thanks for the help, thesix, but awk is a bit too intimidating for me. :)

awk was awkward :roll: for me too, but I'm finding uses for it, where any equivalent with sed and grep is more awkward.