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
replacing multiple lines with single line siba.s.nayak Shell Programming and Scripting 3 05-28-2008 02:43 AM
Multi-line output to single line LinuxRacr Shell Programming and Scripting 7 02-26-2008 10:05 AM
Concatenating multiple lines to one line if match pattern phixsius Shell Programming and Scripting 13 01-24-2008 11:02 PM
Reading Multiple Variables From a Single Line in Shell Drek Shell Programming and Scripting 14 12-21-2006 11:20 AM
Splitting a single line into multiple lines thanuman Shell Programming and Scripting 4 02-23-2005 04:56 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 05-22-2008
VTAWKVT VTAWKVT is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
make multiple line containing a pattern into single line

I have the following data file.
Code:
zz=aa azxc-1234 aa=aa
zz=bb azxc-1234 bb=bb
zz=cc azxc-1234 cc=cc
zz=dd azxc-2345 dd=dd
zz=ee azxc-2345 ee=ee
zz=ff azxc-3456 ff=ff
zz=gg azxc-4567 gg=gg
zz=hh azxc-4567 hh=hh
zz=ii azxc-4567 ii=ii
I want to make 2nd field pattern matching multiple lines into single line.
So the output will be:
Code:
zz=aa azxc-1234 aa=aa zz=bb azxc-1234 bb=bb zz=cc azxc-1234 cc=cc
zz=dd azxc-2345 dd=dd zz=ee azxc-2345 ee=ee
zz=ff azxc-3456 ff=ff
zz=gg azxc-4567 gg=gg zz=hh azxc-4567 hh=hh zz=ii azxc-4567 ii=ii
help!

Last edited by Yogesh Sawant; 05-22-2008 at 01:19 PM.. Reason: added code tags
  #2 (permalink)  
Old 05-22-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Here is an example of something I had to write recently

My input file had customer info and auto info. My goal was to combine records where a customer had more than one auto, so only one letter would be sent. Random notes:
comptxt is what I am comparing, the customer info
autotxt is the vehicle specifics
comptxts hold the saved information
you will see me write lots of "|" pipes, so I can later easily manipulate the data
the script must do a last write since I fall out of the loop when done reading, but I did not write the last record
For you, compare cut -c1-15 similar to my comptxt, and cut -c17-20 similar to my autotxt.
Lastly, there might be a few other variables in that code section you can ignore as they were more for my purposes (like vehcnt).


Code:
#combine lines where key is same
echo "** extracting info from "$wkfile30" to "$wkfile40
comptxts=""
vehicle=""
recno=0
vehcnt=0

while read zf
   do
      recno=$((recno + 1))
#grab the key name for comparison
      comptxt=$(echo "$zf" | cut -d"|" -f2)
      autotxt=$(echo "$zf" | cut -d"|" -f3)
#need to skip first time through since of course different
      if [ -n "$comptxts" ]
         then
         if [ "$comptxts" != "$comptxt" ]
            then
#format output dataline
               vehcnttxt=$(printf "%.4d" "$vehcnt")
               autotxto=$(printf "%-100s" "$autotxts")
               echo "$outtxt1""|""$vehcnttxt""|""$autotxto""|-" >>$wkfile40
               comptxts="$comptxt"
               autotxts="$autotxt"
               vehcnt=0
            else
               autotxts="$autotxts""$autotxt"
         fi 
         else
            comptxts="$comptxt"
            autotxts="$autotxt"
      fi


      vehcnt=$((vehcnt+1))
      outtxt1="$zf"

   done < $wkfile30

#still need to output the last record !!
vehcnttxt=$(printf "%.4d" "$vehcnt")
autotxto=$(printf "%-100s" "$autotxts")
echo "$outtxt1""|""$vehcnttxt""|""$autotxto""|-" >>$wkfile40
  #3 (permalink)  
Old 05-22-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
If the input is ordered by the second field:

Code:
awk 'END{print RS}$0=_[$2]++||NR==1?$0:RS$0' ORS= input
Otherwise:
Code:
awk 'END{for(r in _)print _[r]}{_[$2]=_[$2]?_[$2] FS $0:$0}' input
Use awk or /usr/xpg4/bin/awk on Solaris.

Last edited by radoulov; 05-22-2008 at 04:23 PM..
  #4 (permalink)  
Old 05-23-2008
VTAWKVT VTAWKVT is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
Thanks!!

It worked so well!!!!
  #5 (permalink)  
Old 08-06-2008
Sara-sh Sara-sh is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 9
help!

Can you guys please help me do the following.

I have a file with a server name and a list of mac addresses (variable length) on separate lines, need to put them all in one line separated by space:

Have this:

servername
00:03:6e:4e:4f:5c
00:09:7b:4e:4c:6d
00:04:55:5a:8d:ec
....


want this:
servername 00:03:6e:4e:4f:5c 00:09:7b:4e:4c:6d 00:04:55:5a:8d:ec ....


Can someone please help, thanks so much....
Sara
  #6 (permalink)  
Old 08-06-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Use nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk 'END { print _ }
!/:/ && _ { print _; _ = "" }
{ _ = _ ? _ FS $0 : $0 }
' filename
Or:

Code:
perl -nle'BEGIN { $, = " " }
  print @x and undef @x if not /:/ and @x;
  push @x, $_;
  END { print @x }
' filename

Last edited by radoulov; 08-07-2008 at 03:37 AM.. Reason: refactored
  #7 (permalink)  
Old 08-06-2008
Sara-sh Sara-sh is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 9
Thanks so much for your response however, I am getting error when running the command, any chance you can take a look? Thanks again.

>awk 'END { print _ }
> !/:/ && x++ { print _; _ = "" }
> { _ = _ ? _ FS $0 : $0 }
> ' file.mac
awk: syntax error near line 2
awk: bailing out near line 2
Sponsored Links
Closed Thread

Bookmarks

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 07:49 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