Awk 2 lines to 1 line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk 2 lines to 1 line
# 1  
Old 04-22-2009
Awk 2 lines to 1 line

Input File:

Quote:
Input:
host1 server1
database1 5
host2 server2
database2 5
host3 server3
database3 5
Required Result:
Quote:
host1 server1 database1 5
host2 server2 database2 5
host3 server3 database3 5
I have tried the following code:
Code:
nawk '{for(i=1; i<=NR; i+2) {print NR,$0; getline ;print \n $0; NR=NR+2}}' temp

But doesnt gives the right result.
Help is appreciated
# 2  
Old 04-22-2009
Code:
paste -d' ' - - < temp
OR
nawk 'ORS=(FNR%2)?FS:RS' temp

# 3  
Old 04-22-2009
Quote:
Originally Posted by vgersh99
Code:
paste -d' ' - - < temp
OR
nawk 'ORS=(FNR%2)?FS:RS' temp

Thank you pro Smilie

Code:
paste -d' ' - - < temp
nawk 'ORS=(FNR%2)?FS:RS' temp

Would appreciate if you can explain this.
# 4  
Old 04-22-2009
Quote:
Originally Posted by zenith
Thank you pro Smilie

Code:
paste -d' ' - - < temp
nawk 'ORS=(FNR%2)?FS:RS' temp

Would appreciate if you can explain this.
You can read the man pages for 'paste' to give you a high-level of what it does - the rest should be easy.
awk:
Code:
(FNR%2) - get a 'modulo' of the current file RecordNumber (FNR) over 2 - every OTHER line.
If the mod is NON-zero, return 'FS' (FieldSeparator)
If the mod is zero, return RecordSeparator (RS)
ORS= - assign the returned value to the OutputRecordSeparator (ORS)

In other words...
If we're dealing with the ODD record/line numbers (1,3,5,7 etc), print the line and FS (separate the next line)
If we're dealing with the EVEN record/line numbers (2.4.6.8 etc), print the line and the ORS (which is by default is newLine).

# 5  
Old 04-22-2009
Quote:
Originally Posted by vgersh99
You can read the man pages for 'paste' to give you a high-level of what it does - the rest should be easy.
awk:
Code:
(FNR%2) - get a 'modulo' of the current file RecordNumber (FNR) over 2 - every OTHER line.
If the mod is NON-zero, return 'FS' (FieldSeparator)
If the mod is zero, return RecordSeparator (RS)
ORS= - assign the returned value to the OutputRecordSeparator (ORS)
 
In other words...
If we're dealing with the ODD record/line numbers (1,3,5,7 etc), print the line and FS (separate the next line)
If we're dealing with the EVEN record/line numbers (2.4.6.8 etc), print the line and the ORS (which is by default is newLine).

Thank you Sir,

Minor change; need output comma seperated.

I have tried this but has syntax error.

Code:
nawk '{ORS=(FNR%2)?FS:RS}; OFS=,' temp

Appreciate your help
# 6  
Old 04-22-2009
Code:
nawk -F, 'ORS=(FNR%2)?FS:RS' temp

# 7  
Old 04-22-2009
Awk with weird spaces (output from sybase server)

Quote:
Originally Posted by vgersh99
Code:
nawk -F, 'ORS=(FNR%2)?FS:RS' temp

When i pasted input file on website. i have some weird spaces but when i pasted i think website removed it. hence i am attaching it.


Requried Result
host1,server1,database1,5
host2,server2,database2,5
host3,server3,database3,5

Result i am getting is also attached

Appreciate help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

3. Shell Programming and Scripting

Enumerate lines until each line break using awk

Hi, I have the following data: This this DT 0.99955 0 4 is be VBZ 1 5 7 sentence sentence NN 0.916667 8 16 one one NN 0.545078 17 20 . . Fp 1 20 21 This this DT 0.99955 22 26 is be VBZ 1 27 29 the the DT 1 30 33 second 2 JJ 0.930556 34 40 sentence sentence NN 0.916667 41 49... (1 Reply)
Discussion started by: owwow14
1 Replies

4. Shell Programming and Scripting

Display all the matches lines in one line using awk

Please can you let me know how to print all the matching lines from a file in one single line using awk. Thanks I have the following data in the input file data1 voice2 voice1 speech1 data2 data3 ... ... voice4 speech2 data4 and the output should be as follows data1 data2... (4 Replies)
Discussion started by: Sudhakar333
4 Replies

5. Shell Programming and Scripting

awk problem two lines in the same line

Hi guy, I have an output command like this: Policy Name: NBU.POL.ORA.PROD Policy Type: Oracle Active: yes HW/OS/Client: Linux RedHat2.6 node1 Iclude: /usr/openv/netbackup/scripts/backup_ora1.bash I would like to parse the... (1 Reply)
Discussion started by: luca72m
1 Replies

6. Shell Programming and Scripting

(awk?) print multiple lines on one line

I have a log file something like ------- 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 report 1,date 27/01/13,time 08:00,records 1234 report 2,date 27/01/13,time... (6 Replies)
Discussion started by: gefa
6 Replies

7. Shell Programming and Scripting

awk or sed - Convert 2 lines to 1 line

Hi, Just trying to get to grips with sed and awk for some reporting for work and I need some assistance: I have a file that lists policy names on the first line and then on the second line whether the policy is active or not. Policy Name: Policy1 Active: yes Policy... (8 Replies)
Discussion started by: guinch
8 Replies

8. Shell Programming and Scripting

How to calculate mean in AWK? line by line several files, thousands of lines

I'm kinda stuck on this one, I have 7 files with 30.000 lines/file like this 050 0.023 0.504336 050 0.024 0.529521 050 0.025 0.538908 050 0.026 0.537035 I want to find the mean line by line of the third column from the files named like this: Stat-f-1.dat .... Stat-f-7.dat Stat-s-1.dat... (8 Replies)
Discussion started by: AriasFco
8 Replies

9. Shell Programming and Scripting

Break one line to many lines using awk

Break one line to many lines using awk The below code works but i want to implement without combining field 2 and 3 and then splitting i would like to do this in one command instead of writing multiple commands and creating multiple lines. nawk -F"|" '{print $1,$2SUBSEP$3}' OFS="|" file >... (16 Replies)
Discussion started by: pinnacle
16 Replies

10. Shell Programming and Scripting

to make 2 lines into 1 line using awk

suppose i have a file AAAHHHHAHH AJJJJAAAAAA AJJJJJAAJJAK AJJAJJAJKKK the output suhd be AAAHHHHAHHAJJJJAAAAAAAJJJJJAAJJAKAJJAJJAJKKK (2 Replies)
Discussion started by: cdfd123
2 Replies
Login or Register to Ask a Question