need help with sed command
Hi guys. I've been trying to figure out sed for some time with no success. The man pages are confusing at best and I've scoured the internet for help. I've read a few tutorials and getting started guides and things like that but I'm unable to get the right command to do what I want. All I want to do is take a list of IP addresses that are on consecutive lines and print them on the same line separated by commas to a new file. eg:
file "IP1.txt" looks like 1.2.3.4 5.6.7.8 4.3.2.1 8.7.6.5 and I want them to output to "IP2.txt" and look like this: 1.2.3.4, 5.6.7.8, 4.3.2.1, 8.7.6.5 If anyone can help me out with a sed one liner I would really appreciate it and you'd be saving me some more frustration trying to figure it out. Thanks for your help. |
this isn't really something sed is good at without some meddling around. Sed works on data on a line by line basis, so you can't remove the carriage returns as sed never sees them. It's possible to mess with bash environmental variables to change what defines a new record, but this just isn't a good way to learn sed.
I'd use tr instead, which isn't line based... "cat file | tr '\n' ','" |
Quote:
http://www.grymoire.com/Unix/Sed.html I agree with acid that SED is not the best choice, but it is possible. Look at the commands for reading and writing to/from the hold buffer.. |
Why sed?
Use tr instead (unless you want to dive ni sed a little deeper:)) : cat something|tr "\n" "," M. |
As the others have said (sed?) the best way to do this isn't with sed.
This link however, does show a way to do it with sed that looks quite cryptic. It also gives examples with tr, awk and perl. http://linux.dsplabs.com.au/rmnl-rem...-sam-ssam-p65/ |
I don't really know why sed!! hence the linux newbie forum! : )
Thanks everyone for the responses... the 'cat file | tr "\n" ","' command worked perfectly! I used to work with someone who accomplished this with 'sed', but lost the command he used. He was considerably smarter than me on computers. This command is so simple! I really appreciate the help! Thanks again!
|
Quote:
Code:
cp IP1.txt IP2.txt; sed -i 's/\n/, /g' IP2.txt |
no, it won't because sed is a line editor not a stream editor. Despite the s.
this DOES work... sed ':a;N;$!ba;s/\n/, /g' IP1.txt but that's horrible, and *I* don't understand what is being done there... as above, not a good basic teaching question. http://www.thekramms.com/Oreilly/boo...wk/ch06_01.htm |
Quote:
Code:
for i in $(cat IP1.txt);do echo -n "$i, " >> IP2.txt;done |
It doesn't look that bad if you break it up.
Code:
sed ':a |
Quote:
Code:
tr "\n" ","' < file Code:
awk '{$1=$1}1' RS="" OFS="," file |
All times are GMT -5. The time now is 05:32 PM. |