How can you swap the first 4 line only, the rest will stay the same.
thanks
#!/bin/sh
line=4
awk -v var="$line" 'NR==var {
s=$0
getline;s=$0"\n"s
getline;print;print s
next
}1' fileko.tx
.
desired output:
this is line5
this is line6
this is line7
this is line8
this is line1
this is line2
this is line3
this is line4
this is line9
this is line10
this is line11
this is line5
this is line6
this is line7
this is line8
this is line1
this is line2
this is line3
this is line4
this is line9
this is line10
this is line11
Following is explanation for same code, hope this will be helpful.
awk '{if(NR==1 || NR==2 || NR==3 || NR==4){A=A?A ORS $0:$0;next};if(NR==8){print $0 ORS A;} else {print}}' Input_file
if(NR==1 || NR==2 || NR==3 || NR==4) #### Looking for condition if line number is equal to 1 or 2 or 3 or 4
{A=A?A ORS $0:$0;next} #### If above condition is TRUE then I am creating a variable which will hold the
current line's value with it's previouls value character ? is to perform a action when condition before will be TRUE and : is used to perform action when condition is FALSE.
next #### After above variable's creation leave next statements and move next
if(NR==8) #### Looking for condition when line number is 8
{print $0 ORS A;} #### if above condition is TRUE then print the 8th line and print variable A, which has now value of lines from 1 to 4 in it
else #### Apart of any line pther than 8 it will do simple print operation.