Operating System - HP-UX
1752564 Members
4663 Online
108788 Solutions
New Discussion юеВ

join lines in a file using getline

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

join lines in a file using getline

I want to use awk (although there are many other ways) to join two lines in a file:

my data
some more data

my data
some more data


I want the output like

my data some more data
my data some more data

I have been looking at using the getline function to do this but I cannot quite get the systax

anyone know the best solution using awk?

thanks

Chris.
hello
9 REPLIES 9
Oviwan
Honored Contributor
Solution

Re: join lines in a file using getline

hey

sed solution:

sed '$!N;s/\n/ /' filename

Regards
lawrenzo_1
Super Advisor

Re: join lines in a file using getline

ok so this works on a single line file but not a file with multiple lines

s:-(


# cat chris1
this is the start
and this is the end

this is the start
and this is the end

this is the start
and this is the end

#sed '$!N;s/\n/ /' chris1
this is the start and this is the end
this is the start
and this is the end
this is the start and this is the end

or my awk effort:

# awk '{printf $0;getline;print " "$0}' chris1
this is the start and this is the end
this is the start
and this is the end
this is the start and this is the end


I wil keep trying ....
hello
James R. Ferguson
Acclaimed Contributor

Re: join lines in a file using getline

Hi Chris:

# awk '{if (NR%2==1) {getline X};print $0,X}' file

Regards!

...JRF...
Oviwan
Honored Contributor

Re: join lines in a file using getline

hence delete all empty line before concatenate:

sed '/^$/d;$!N;s/\n/ /' file
lawrenzo_1
Super Advisor

Re: join lines in a file using getline

hi james,

still the same output from your command:

[root@brli013a tmp]# awk '{if (NR%2==1) {getline X};print $0,X}' chris1
this is the start and this is the end
this is the start
and this is the end
this is the start and this is the end
and this is the end

but the sed example is good ....
hello
James R. Ferguson
Acclaimed Contributor

Re: join lines in a file using getline

Hi (again) Chris:

Oh, so the blank lines were really there...

# awk '/^[[:space:]]*$/ {next};{if (i==0) {getline X;i=1};i=0;print $0,X}' file

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: join lines in a file using getline

brilliant thanks both,

and james you have given me a solution 2 for the price of 1, I amended the original file so no lines and it works ( of course )

[root@brli013a tmp]# awk '{if (NR%2==1) {getline X};print $0,X}' chris1
this is the start and this is the end
this is the start and this is the end
this is the start and this is the end


and the second solution also works well.

Chris.
hello
James R. Ferguson
Acclaimed Contributor

Re: join lines in a file using getline

Hi (again) Chris:

The last suggestion of mine had extraneous (useless) code embedded, It should simply be:

# awk '/^[[:space:]]*$/ {next};{getline X;print $0,X}' file

NO POINTS FOR THE CORRECTION.

Regards!

...JRF...
Saini
New Member

Re: join lines in a file using getline

Hi

I have a problem similar to the one above. But, in my case the file is of this format.
---
line1.1
line1.2
line1.3

line2.1
line2.2
line2.3
line2.4

line3.1
line3.2
---
I want the following output
---
line1.1line1.2line1.3

line2.1line2.2line2.3line2.4

line3.1line3.2
---
So, basically I've to join the lines until a empty line is encountered. Then , i have to skip it and start joining other lines. I am unable to do it with my limited knowledge of awk.
Any help is appreciated.
Thanks
Saini