• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need some help with sed

P0ldy

Senior member
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?
 
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 ?
 
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.
 
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. 🙂
 
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.
 
Back
Top