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 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? |
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 |
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.
|
Thanks for the replies.
I did use tr in the end. Awk's a bit too intimidating for me. ;) |
I'm curious ... how did you stop tr from putting ALL lines into one line?
Cheers, Tink |
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.
|
Quote:
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. |
All times are GMT -5. The time now is 05:56 PM. |