make multiple line containing a pattern into single line


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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 03:19 PM.. Reason: added code tags
# 2  
Hammer & Screwdriver 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  
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 06:23 PM..
# 4  
Thanks!!

It worked so well!!!!
# 5  
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  
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 05:37 AM.. Reason: refactored
# 7  
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
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #590
Difficulty: Easy
In a linear search algorithm, the input size is the number of elements in the collection (n) divided by 2.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Group Multiple Lines on SINGLE line matching pattern

Hi Guys, I am trying to format my csv file. When I spool the file using sqlplus the single row output is wrapped on three lines. Somehow I managed to format that file and finally i am trying to make the multiple line on single line. The below command is working fine but I need to pass the... (3 Replies)
Discussion started by: RJSKR28
3 Replies

3. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

4. Shell Programming and Scripting

Multiple pattern match and print the output in a single line

I need to match two patterns in a log file and need to get the next line of the one of the pattern (out of two patterns) that is matched, finally need to print these three values in a single line. Sample Log: 2013/06/11 14:29:04 <0999> (725102) Processing batch 02_1231324 2013/06/11... (4 Replies)
Discussion started by: rpm120
4 Replies

5. Shell Programming and Scripting

Splitting Single line into multiple line

Hi All, I am reading a line from a file and writing it to other file. Whenever I got a particular line then I want that line to be splited into 4 line and written it to new file. e.g My line is U_ABC connector3 pin24E connector4 pin25E connector5 pin26E connector6 pin27E connector7... (2 Replies)
Discussion started by: diehard
2 Replies

6. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

7. Shell Programming and Scripting

convert single line output to multiple line

Hi all, I have a single line output like below echo $ips 10.26.208.28 10.26.208.26 10.26.208.27 want to convert above single line output as below format. Pls advice how to do ? 10.26.208.28 10.26.208.26 10.26.208.27 Regards Kannan (6 Replies)
Discussion started by: kamauv234
6 Replies

8. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

9. Shell Programming and Scripting

Try to make a single line to syslog.

Hi again: Following the thread: https://www.unix.com/shell-programming-scripting/147755-format-lines-file.html I couldn't solve my problem, so I ask you again gurus: I have this code: nohup /usr/sbin/auditstream | /usr/sbin/auditselect -m -e "event== S_ENVIRON_WRITE || event==... (2 Replies)
Discussion started by: iga3725
2 Replies

10. Shell Programming and Scripting

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... (8 Replies)
Discussion started by: mitch
8 Replies

Featured Tech Videos