The UNIX and Linux Forums  

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 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-27-2008 11:43 PM
Multi-line output to single line LinuxRacr Shell Programming and Scripting 7 02-26-2008 06:05 AM
Concatenating multiple lines to one line if match pattern phixsius Shell Programming and Scripting 13 01-24-2008 07:02 PM
Reading Multiple Variables From a Single Line in Shell Drek Shell Programming and Scripting 14 12-21-2006 07:20 AM
Splitting a single line into multiple lines thanuman Shell Programming and Scripting 4 02-23-2005 12:56 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-22-2008
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 10:19 AM. Reason: added code tags
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-22-2008
joeyg's Avatar
premier etoile de match
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 533
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
Reply With Quote
  #3 (permalink)  
Old 05-22-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,432
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 01:23 PM.
Reply With Quote
  #4 (permalink)  
Old 05-23-2008
Registered User
 

Join Date: May 2008
Posts: 3
Thanks!!

It worked so well!!!!
Reply With Quote
  #5 (permalink)  
Old 4 Weeks Ago
Registered User
 

Join Date: Aug 2008
Posts: 3
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
Reply With Quote
  #6 (permalink)  
Old 4 Weeks Ago
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,432
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; 4 Weeks Ago at 12:37 AM. Reason: refactored
Reply With Quote
  #7 (permalink)  
Old 4 Weeks Ago
Registered User
 

Join Date: Aug 2008
Posts: 3
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
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0