Moving line above possible? after pattern match?

Team,
would like to know if it is possibe to move line above.after TEST pattern match

#cat file1
data1
data2
ok
Test

data1
ok
Test

data2
Test


Output:
Test
data1
data2
ok

Test
data1
ok

Test
data2




Hello kenshinhimura,

Following may help you in same.

awk '!/Test/ {a=0;V=V?V ORS $0:$0} /Test/ {a=1; V="Test" ORS V;print V ORS;V=a=""}' Input_file

Output will be as follows.

Test
data1
data2
ok
 
Test
data1
ok
 
Test
data2

Thanks,
R. Singh

1 Like

Hi sing, sorry, its working fine, but Test has another data in it

example

Test blah blah

Hello kenshinhimura,

Kindly try the following it may help.

awk '!/Test/ {a=0;V=V?V ORS $0:$0} /Test/ {a=1; V=$0 ORS V;print V ORS;V=a=""}' Input_file

Thanks,
R. Singh

1 Like

Sir Singh, thank you

1 Like

Try:

awk '{p=$NF; $NF=x; print p,$0}' RS= FS='\n' OFS='\n' file
1 Like

i made a script on my own., and i run it to all of my servers to get the hostname, please look at file2. Then i need some data in file3 as well,. i need to combine them

# cat file2

192.10.1.41
server.age.com
######

192.10.0.40
ssh cant connect
######

192.1.1.49 
server2.age.com
##########


#cat file 3
data3 192.10.1.41 stop 
data2 192.10.0.40 running
data1 192.1.1.49 idle      max




Output:
192.10.1.41  server.age.com  data3 stop
192.10.0.40  		     data2 running
192.1.1.49   server2.age.com data1 idle

---------- Post updated at 02:53 PM ---------- Previous update was at 02:51 PM ----------

thank you..i tried that.much simpler...

@kenshinhimura: Please open a new thread for a new problem..

1 Like

Noted sir..thank you