single line input to multiple line output with sed

hey gents,

I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm going to send it to a mysql db. My problem starts once all my sed is done and snmpwalk has done its thing.

Final sed results:
TCF_Distro,10.10.50.2,WS-C3560-24TS,CAT1050RH3D,AHA,10.10.50.3,WS-C3560-24TS,CAT1050RHT2,TCF,10.10.50.4,WS-C3560-48PS,CAT1026RKD3

what I want though is:
TCF_Distro,10.10.50.2,WS-C3560-24TS,CAT1050RH3D
AHA,10.10.50.3,WS-C3560-24TS,CAT1050RHT2
TCF,10.10.50.4,WS-C3560-48PS,CAT1026RKD3

If I can get sed or awk to do this I will not have to do it with mysql commands.

thanks for any assistance.

This is taking place on:
Fedora Core 8 with sed 4.1.5

Try this:

awk -F, '{for(i=1;i<=NF;i++){printf("%s%s",$i,i%4?",":"\n")}}' file

If you get errors use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards

Awesome Franklin52,

Worked like a champ. If you don't mind elaborating on the code so I can understand what is going on.

Thank you.

You can do it directly in sed if you wish:
(use /usr/xpg4/bin/sed on Solairs)

sed 's/\([^,]*,\)\{4\}/&\
/g' file

For old versions of sed:

sed 's/\([^,]*,[^,]*,[^,]*,[^,]*,\)/&\
/g' file

No luck Radoulov,
i received an error that reads

sed: -e expression #1, char 22: unterminated `s' command

char 22 is a } and phpdesigner2008 doesn't see a problem with that.

Without the entire code it would be difficult to debug,
try copy/pasting the code (there is an escaped new line):

zsh-4.3.4% cat file
TCF_Distro,10.10.50.2,WS-C3560-24TS,CAT1050RH3D,AHA,10.10.50.3,WS-C3560-24TS,CAT1050RHT2,TCF,10.10.50.4,WS-C3560-48PS,CAT1026RKD3
zsh-4.3.4% sed 's/\([^,]*,\)\{4\}/&\
quote> /g' file
TCF_Distro,10.10.50.2,WS-C3560-24TS,CAT1050RH3D,
AHA,10.10.50.3,WS-C3560-24TS,CAT1050RHT2,
TCF,10.10.50.4,WS-C3560-48PS,CAT1026RKD3

With GNU sed you could even write it like this:

zsh-4.3.4% sed -r 's/([^,]*,){4}/&\n/g' file
TCF_Distro,10.10.50.2,WS-C3560-24TS,CAT1050RH3D,
AHA,10.10.50.3,WS-C3560-24TS,CAT1050RHT2,
TCF,10.10.50.4,WS-C3560-48PS,CAT1026RKD3

Radoulov.

In like flyn. the bottom code you supplied also worked awesome. thank you for your help. Can you take a min to explain how it works? I understand the basics of sed
's = substitute
first set /XXX/ is the regexp i want to identify
second sed /XXX/ is what I want it changed to and
g' = do it globally

but i can't read whats going on.

oh and the reason I was having a problem yesterday is because when I put your code in a file so that I could pipe my file through it I didn't take the last single ' off the back of the g.

Thank you

Yes,
and the & variable contains the matched pattern.
It says: substitute every four 0 or more non-commas followed by a comma ([^,]*,){4} - four times the pattern in the parentheses - by them self's (the matched pattern) followed by a new line.

Nice,
thank you for explaining.