The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
make multiple line containing a pattern into single line VTAWKVT Shell Programming and Scripting 13 12-04-2008 06:40 PM
replacing multiple lines with single line siba.s.nayak Shell Programming and Scripting 3 05-28-2008 02:43 AM
ls command output in single line arsheshadri AIX 7 04-28-2008 02:50 PM
Multi-line output to single line LinuxRacr Shell Programming and Scripting 7 02-26-2008 10:05 AM
Need output in different lines not in one single line csaha Shell Programming and Scripting 1 02-08-2006 08:28 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-13-2008
mitch's Avatar
mitch mitch is offline
Registered User
  
 

Join Date: Nov 2003
Location: Currently Kuwait/Iraq
Posts: 15
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
  #2 (permalink)  
Old 07-13-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Try this:

Code:
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
  #3 (permalink)  
Old 07-14-2008
mitch's Avatar
mitch mitch is offline
Registered User
  
 

Join Date: Nov 2003
Location: Currently Kuwait/Iraq
Posts: 15
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.
  #4 (permalink)  
Old 07-14-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
You can do it directly in sed if you wish:
(use /usr/xpg4/bin/sed on Solairs)

Code:
sed 's/\([^,]*,\)\{4\}/&\
/g' file
For old versions of sed:

Code:
sed 's/\([^,]*,[^,]*,[^,]*,[^,]*,\)/&\
/g' file
  #5 (permalink)  
Old 07-14-2008
mitch's Avatar
mitch mitch is offline
Registered User
  
 

Join Date: Nov 2003
Location: Currently Kuwait/Iraq
Posts: 15
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.
  #6 (permalink)  
Old 07-14-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Without the entire code it would be difficult to debug,
try copy/pasting the code (there is an escaped new line):

Code:
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:

Code:
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
Closed Thread

Bookmarks

Tags
solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0