(awk?) print multiple lines on one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting (awk?) print multiple lines on one line
# 1  
Old 01-28-2013
(awk?) print multiple lines on one line

I have a log file something like

Code:
------- report 1 -------
date 27/01/13
time 08:00
records 1234
 
------- report 2-------
date 27/01/13
time 08:00
records 1239

...

I'd like output to show as

Code:
report 1,date 27/01/13,time 08:00,records 1234
report 2,date 27/01/13,time 08:00,records 1239

...

Can anyone advise me best way to do this?

Last edited by Scott; 01-28-2013 at 11:10 AM.. Reason: Please use code tags
# 2  
Old 01-28-2013
Something like

Code:
$ paste - - - - - < text.txt | tr -d '-' | tr ' ' ',' | sed -e 's/^ //' -e 's/,$//'
report 1,date 27/01/13,time 08:00,records 1234
report 2,date 27/01/13,time 08:00,records 1239
report 3,date 27/01/14,time 08:00,records 1240

Will do the trick.
# 3  
Old 01-28-2013
Check with this command
Code:
awk '{printf "%s"",",$0}' input_file

# 4  
Old 01-28-2013
Code:
awk 'BEGIN{FS=","}/^-/&&/report/&&!/^records/{gsub(/[-]+/,"");ORS=FS;}/^records/{ORS=" "}/^-/&&!/report/{$0="";ORS=RS;}1' filename


Last edited by Yoda; 01-28-2013 at 11:37 AM.. Reason: regex correction
# 5  
Old 01-28-2013
Hi.

I may have added code tags in post #1 incorrectly.

Initially, I added them as (assuming each report was a separate file):
Quote:
------- report 1 -------
Code:
date 27/01/13
time 08:00
records 1234

------- report 2-------
Code:
date 27/01/13
time 08:00
records 1239

But have now changed them to:
Quote:
Code:
------- report 1 -------
date 27/01/13
time 08:00
records 1234
 
------- report 2-------
date 27/01/13
time 08:00
records 1239

Sorry if your answers are invalidated as a result.

gefa: Please use code tags, and correct your post as it shouild be. Thanks!
# 6  
Old 01-28-2013
I'll drop a - from my paste then Smilie

Hopefully this is it:

Code:
bash-3.2$ paste - - - - < input.txt | tr -d '-' | tr '  ' ',' | sed -e 's/^ //' -e 's/,$//'
report 1 ,date 27/01/13,time 08:00,records 1234
report 2,date 27/01/13,time 08:00,records 1239
bash-3.2$ cat input.txt
------- report 1 -------
date 27/01/13
time 08:00
records 1234
------- report 2-------
date 27/01/13
time 08:00
records 1239

Edit: Possibly not - I've now seen a blank line appear between the two records in the original post. I will wait for the OP to confirm their source data before continuing.
# 7  
Old 01-28-2013
Thanks for the replies thus far, what I should have said is that I also need to select by date, so,

I would search (for example) for anything with date of 25/01/13,

then I'd want to print previous record, e.g., report 1,

then next 3 lines including date (there could be more than three lines but each record starts with something similar to ------- report 1 -------,

I still require the output one one line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

(n)awk: print regex search output lines in one line

Hello. I have been looking high and low for the solution for this. I seems there should be a simple answer, but alas. I have a big xml file, and I need to extract certain information from specific items. The information I need can be found between a specific set of tags. let's call them... (2 Replies)
Discussion started by: Tobias-Reiper
2 Replies

2. Shell Programming and Scripting

Print multiple lines on one line

Hi All, I have a file looks like: rst:singh:99.0.20-X86 2 rst:ACSI_SIN_SERVICES rst:singh:99.0.20-X86 2 rst:ACSI_BISI want to wrap 3rd col in one line and add variable value at start and ending of line and I wrote command: cat file | awk '{print $3}' | xargs > command.txt sed -e... (1 Reply)
Discussion started by: rakeshtomar82
1 Replies

3. Shell Programming and Scripting

Print the first line for each multiple lines

Hi all, i need help to extract each first line from multiple lines occurrences based on different patterns (name) starting from the fourth lines like follows:- // header 1 header 2 header 3 // no acc name score rank //... (2 Replies)
Discussion started by: redse171
2 Replies

4. Shell Programming and Scripting

Awk: print lines with one of multiple pattern in the same field (column)

Hi all, I am new to using awk and am quickly discovering what a powerful pattern-recognition tool it is. However, I have what seems like a fairly basic task that I just can't figure out how to perform in one line. I want awk to find and print all the lines in which one of multiple patterns (e.g.... (8 Replies)
Discussion started by: elgo4
8 Replies

5. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

6. Shell Programming and Scripting

Print lines between two strings multiple occurencies (with sed, awk, or grep)

Hello, I can extract lines in a file, between two strings but only one time. If there are multiple occurencies, my command show only one block. Example, monfichier.txt contains : debut_sect texte L1 texte L2 texte L3 texte L4 fin_sect donnees inutiles 1 donnees inutiles 2 ... (8 Replies)
Discussion started by: theclem35
8 Replies

7. Shell Programming and Scripting

Can't print multiple lines inside awk :(

Hi Friends, I have small issue with following code snippet. I am trying call one function inside awk in which the function inturn will echo few lines. However when i ran script its throwing an error saying "nawk: syntax error at source line 1". #!/bin/sh eval input=$@ while read... (3 Replies)
Discussion started by: Shahul
3 Replies

8. Shell Programming and Scripting

AWK print lines into multiple files

Hi, i have an input text file like this: Student 1 maths science = Student 2 maths science = Student 3 maths science i would like to print each student information into separate files, each student id is separated by "=". (1 Reply)
Discussion started by: saint2006
1 Replies

9. Shell Programming and Scripting

awk find a string, print the line 2 lines below it

I am parsing a nagios config, searching for a string, and then printing the line 2 lines later (the "members" string). Here's the data: define hostgroup{ hostgroup_name chat-dev alias chat-dev members thisisahostname } define hostgroup{ ... (1 Reply)
Discussion started by: mglenney
1 Replies

10. UNIX for Dummies Questions & Answers

print multiple lines with awk

Hi everyone! I'm not new to Unix, but I've never used awk before. I tried to look up this information on several sites and forums, I also looked in the documentation but I haven't found a solution yet. I would like to print the previous 3 lines before and the following 4 lines after the... (6 Replies)
Discussion started by: djcsabus
6 Replies
Login or Register to Ask a Question