awk joining lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk joining lines
# 1  
Old 03-23-2010
awk joining lines

Hello,
I'm trying to write a piece of code in awk, which should be able recognize by some regexps two lines and then join them together (maybe write them without \n would be enough, I don't know).. the thing is that every line in the file i'm working with starts with some number, for example:

1234: ... <found>
235: ... <found>
...
234: ... <success>
1234: ... <success>
...

The thing i need to do, is recognize two lines with the same number and on the first is regexp "<found>", on the second is "<success>". If these rules are fulfilled then join the lines together. I'm not pretty sure, but something tells me that associative arrays can be the solution, but I have no idea how to do it.
Thanks for ideas.

Last edited by midin; 03-23-2010 at 04:26 PM..
# 2  
Old 03-23-2010
Code:
sort -n -t':' -k1  inputfile |
awk -F':'   '/<found>$/ {arr[$1]=$0}
               /<success>$/ { if ($1 in arr) {print arr[$1], $0; delete arr[$1]}}
               END {for (i in arr) {print arr[i]} } ' inputfile > newfile

start with this, it assumes there are no orphan <success> rows - meaning if there is a <success> row there must exists a matching <found> row.
# 3  
Old 03-23-2010
Keep all datas, not only export the found&success ones, and no worried about the orphan <found> or <success>

Code:
$ cat urfile
1234: ... <found>
235: ... <found>
...
236: ... <found>
234: ... <success>
235: ... <success>
1234: ... <success>
234: ... <success>

$ awk 'NR==FNR && !/success/ {a[$1]=$0;b[$1]=$NF} 
       NR>FNR && /success/ {$NF=b[$1] FS $NF; a[$1]=$0} 
       END { for (i in a) print a[i]}' urfile urfile

1234: ... <found> <success>
234: ...  <success>
235: ... <found> <success>
236: ... <found>
...

# 4  
Old 03-24-2010
Ok, the code from rdcwayx seems to work some way, but there's a thing that i don't understand, for exmaple in my file I have these lines:

5143 execve("/bin/ls", ["ls"], [/* 38 vars */] <unfinished ...>
5143 <... execve resumed> ) = 0

awk 'NR==FNR && !/resumed>/ {a[$1]=$0;b[$1]=$NF}
NR>FNR && /resumed>/ {$NF=b[$1] FS $NF; a[$1]=$0}
END { for (i in a) print a[i]}' urfile urfile

and I need to write this:

5143 execve("/bin/ls", ["ls"], [/* 38 vars */] <unfinished ...> <... execve resumed> ) = 0

but it only writes:

5143 <... execve resumed> ) = ...> 0

thank you for helping me, I very appreciate that Smilie

Last edited by midin; 03-24-2010 at 01:42 PM..
# 5  
Old 03-24-2010
please paste more real sample data to us, and the desired output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk joining multiple lines based on field count

Hi Folks, I have a file with fields as follows which has last field in multiple lines. I would like to combine a line which has three fields with single field line for as shown in expected output. Please help. INPUT hname01 windows appnamec1eda_p1, ... (5 Replies)
Discussion started by: shunya
5 Replies

2. Shell Programming and Scripting

Joining broken lines and removing empty lines

Hi - I have req to join broken lines and remove empty lines but should NOT be in one line. It has to be as is line by line. The challenge here is there is no end of line/start of line char. thanks in advance Source:- 2003-04-34024|04-10-2003|Claims|Claim|01-13-2003|Air Bag:Driver;... (7 Replies)
Discussion started by: Jackceasar123
7 Replies

3. Shell Programming and Scripting

Joining lines in different way

Hi all, I'm excited to the part of unix.com forum, and noob to it. I have an query, where I have an file and it contains data like this use thread when posting do no I was expecting the result as use thread thread when when posting posting do do no use thread when thread when... (6 Replies)
Discussion started by: Jose Nirmal
6 Replies

4. Shell Programming and Scripting

Joining broken lines with awk or perl

Hi, I have a huge file with sql broken statements like: PP3697HB @@@@0 <<<<<<Record has been deleted as per PP3697HB>>>>>> FROM sys.xtab_ref rc,sys.xtab_sys f,sys.domp ur WHE RE rc.milf = ur.milf AND rc.molf = f.molf AND ur.dept = 'SWIT'AND ur .department = 'IND' AND share = '2' AND... (4 Replies)
Discussion started by: som.nitk
4 Replies

5. Shell Programming and Scripting

Joining lines in a file - help!

I'm looking for a way to join lines in a file; e.,g consider the following R|This is line 1 R|This is line 2 R|This is line 3 R|This is line 4 R|This is line 5 what i want to end up with is R|This is line 1 R|This is line 2 R|This is line 3 R|This is line 4 R|This is line 5 so... (15 Replies)
Discussion started by: Storms
15 Replies

6. Shell Programming and Scripting

Joining lines in a text file using AWK or SED

Hi All I'm struggling a bit here :( I need a way of joining lines contained in a text file. I've seen numerous SED and AWK examples and none of them seem to be working for me. The text file has 4 lines: DELL1427 DOC 30189342 79 Now bear with me on this one as I'm actually... (4 Replies)
Discussion started by: huskie69
4 Replies

7. Shell Programming and Scripting

pattern matching lines using the date, and then joining the lines

Hi Guys, Was trying to attempt the below using awk and sed, have no luck so far, so any help would be appreciated. Current Text File: The first line has got an "\n", and the second line has got spaces/tabs then the word and "\n" TIME SERVER/CLIENT TEXT... (6 Replies)
Discussion started by: eo29
6 Replies

8. Shell Programming and Scripting

Need help joining lines

Hi All, I need the command to join 2 lines into one. I found lots of threads but none give me the sollution. Probably because unix scripting is one of my best features ;) I got a logfile where line 2 needs to be joined with line 1, lines 4 needs to be joined with line 3 etc If you need... (16 Replies)
Discussion started by: rene21976
16 Replies

9. Shell Programming and Scripting

joining two lines

Hi , I want to join two lines in a file, where the second line contain query string. if it doesn't contain that string i don't want to join e.g. Input file is as following: name fame game none none none name fame game cat eat mice I need output file as name fame game none none... (2 Replies)
Discussion started by: ashrafonics
2 Replies

10. Shell Programming and Scripting

Joining 3 lines at a time

Hi, I have a file which has the contents as below : 07:38:36 EST date 20041117 07:39:06 EST 07:00:29 EDT date 20050504 07:25:16 EDT 07:00:40 EDT date 20050505 07:23:12 EDT I need to delete the new line character from all lines except 3rd,6th,9th etc. lines so that i get the output... (14 Replies)
Discussion started by: Sabari Nath S
14 Replies
Login or Register to Ask a Question