Gawk combining lines unexpectedly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Gawk combining lines unexpectedly
# 1  
Old 04-24-2009
Gawk combining lines unexpectedly

I am attempting to combine sections of log that should be one line but are spaced out over 10-30 lines due to how the software is outputting the info. (If I am making a newbie mistake I apologize)

Example of log I am working with:
2009-04-14 14:51:22 access data here
info.
Info.
Info.
…….. )}
2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here data here
info.
Info.
Info.
data here )}

I am currently trying to use the following:

cat log.txt | /scripting/gawk.exe '{d=d""$o}
/2009-04/ {
print d
d=""
}' | > new.txt

What is happening, is it is merging the lines as expected, but incorrectly merging the first line following the matched string.

I would expect the script to create two lines, one for each match of 2009-04, however what is being returned is:

2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here info. info. info. data here. )} 2009-04-14 14:51:22 get data here )}
2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here )}


How do I stop the second matching line from being appended? Shouldn't it be on its own line since it is the next instance of 2009-04?

Thanks in advance..

Last edited by demanche; 04-24-2009 at 01:55 PM..
# 2  
Old 04-24-2009
What happens when you do this:

Code:
sed -e :a -e '/\.$/N; s/\.\n//; ta' filename

# 3  
Old 04-24-2009
Quote:
Originally Posted by giannicello
What happens when you do this:

Code:
sed -e :a -e '/\.$/N; s/\.\n//; ta' filename

When I run the sed command the ouput still contains some unwanted newlines, this is the output after running the suggested command:


2009-04-14 14:51:22 access data here
info.
Info.
Info.
data here )}
2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here
Info.
Info.
Info.
data here )}

What I need is:

2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here. info. Info. Info. data here )}
2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here )}
2009-04-14 14:51:22 access data here. info. Info. Info. data here )}




The purpose of all this is that its easier to merge files and sort once each line has a timestamp.


Edit: The only definate is that the lines I need to merge starts with 2009 and ends with )}

Last edited by demanche; 04-24-2009 at 01:59 PM..
# 4  
Old 04-24-2009
sorry reposted my response
# 5  
Old 04-24-2009
Try this:

Code:
awk '{printf("%s%c",$0,match($0,/)}$/)?RS:FS)}' log.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk Find Pattern Print Lines Before and After

Using grep I can easily use: cvs log |grep -iB 10 -A 10 'date: 2013-10-30' to display search results and 10 lines before and after. How can this be accompished using gawk? (4 Replies)
Discussion started by: metallica1973
4 Replies

2. Shell Programming and Scripting

Reading two lines in a while loop and combining the lines

Dear all, I have a file like this: imput scaffold_0 1 scaffold_0 10000 scaffold_0 20000 scaffold_0 25000 scaffold_1 1 scaffold_1 10000 scaffold_1 20000 scaffold_1 23283 and I want the output like this: scaffold_0 1 scaffold_0 10000 scaffold_0 10000 scaffold_0 20000... (6 Replies)
Discussion started by: valente
6 Replies

3. Shell Programming and Scripting

Combining lines between two specific lines

Hi, I have a requirement like following: I have input file like: Question: 1 ----Multiple choice--- What is your favourite colour? Options: a) red b) blue c) none of these Question: 2 ---Multiple choice----- In which month did you join your first job? Options: a) Jan b) Feb c)... (11 Replies)
Discussion started by: ppatra
11 Replies

4. Shell Programming and Scripting

Merging Adjacent Lines Using Gawk

Hi all, I have a text file consisting of 4 columns. What I am trying to do is see whether column 2 repeats multiple times, and collapse those repeats into one row. For example, here is a snippet of the file I am trying to analyze: 1 Gamble_Win 14.282 0.502 1 Sure_Thing 14.858 0.174 1... (4 Replies)
Discussion started by: Jahn
4 Replies

5. UNIX for Dummies Questions & Answers

Joining lines of a text file using GAWK

sir... am having a data file of customer master., containing some important fields as a set one line after another., what i want is to have one set of these fields(rows) one after another in line.........then the second set... and so on... till the last set completed. ... (0 Replies)
Discussion started by: KANNI786
0 Replies

6. Shell Programming and Scripting

Combining two text lines into one

Here is the sample file: 646 STARTED Tue Dec 30 06:38:53 2008 Job DdCustXAddr_brad has been reset. 647 STARTED Tue Dec 30 06:38:54 2008 Starting Job DdCustXAddr_brad. (...) 704 STARTED Tue Dec 30 06:49:02 2008 Job DdCustXAddr_brad aborted. I want to combine every non-numbered line... (4 Replies)
Discussion started by: mfavero
4 Replies

7. Shell Programming and Scripting

gawk print some special lines

Hi every body, i have this file example : TD1 TD2 TD3 . . .TDn <DIE_1> xxxxxx <\DIE_1> <TD1> information 1 inormation n <\TD1> <TDq> information (0 Replies)
Discussion started by: kamel.kimo
0 Replies

8. Shell Programming and Scripting

combining lines in files

hey, I want to combine every three lines in a file onto one i.e old file: 82 67 32 62 58 39 29 47 58 27 34 50 27 35 69 38 58 70 new file: 82 67 32 62 58 39 29 47 58 27 34 50 27 25 69 38 58 70 At the moment I am using the following code: gawk 'BEGIN {x=0} { if(x<3)... (14 Replies)
Discussion started by: sme
14 Replies

9. Shell Programming and Scripting

need help appending lines/combining lines within a file...

Is there a way to combine two lines onto a single line...append the following line onto the previous line? I have the following file that contains some blank lines and some lines I would like to append to the previous line... current file: checking dsk c19t2d6 checking dsk c19t2d7 ... (2 Replies)
Discussion started by: mr_manny
2 Replies

10. Shell Programming and Scripting

Combining multiple lines

I am fairly new to scripting. But I have been able to extract and format all of my information required into one file. My issue is that one character is on a separate line. I need to be able to add the character to the previous line. ex. abcdefghi 1 bcdefghij 3 cdefghijk 4 need to... (4 Replies)
Discussion started by: DUST
4 Replies
Login or Register to Ask a Question