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 posting
when posting do
posting do no
I have tried
cat demo.txt | xargs -n 2
but it gave result as
use thread
when posting
do no
but I was expecting the above result only. Any advice on how to achieve the result
Jotne
June 12, 2013, 1:31am
2
You are expecting that word are multiplying?
Post your real data and what is your goal, and how to come there.
For combining 2 lines
$ perl -ne 'chomp; push @a, $_; if ($#a >= 1) {@a = @a[@a-2..$#a]; print "$a[0] $a[1]\n"}' join.txt
use thread
thread when
when posting
posting do
do no
For combining 3 lines
$ perl -ne 'chomp; push @a, $_; if ($#a >= 2) {@a = @a[@a-3..$#a]; print "$a[0] $a[1] $a[2]\n"}' join.txt
use thread when
thread when posting
when posting do
posting do no
Jotne
June 12, 2013, 2:01am
4
awk '{if(NR>1) print t,$0;t=$0}' join.txt
use thread
thread when
when posting
posting do
do no
Thank you rajamahavan, perl has done magic.
@jotne . How to do the same for combining 3 lines... ?
Jotne
June 12, 2013, 2:41am
6
3
awk '{if(NR>2) print s,t,$0;s=t;t=$0}' join.txt
use thread when
thread when posting
when posting do
posting do no
4
awk '{if(NR>3) print r,s,t,$0;r=s;s=t;t=$0}' join.txt
use thread when posting
thread when posting do
when posting do no
1 Like
Thank you. My first query has been solved.
I'm interested in knowing the explanation of awk and perl code. So that I can learn.........................