Swapping the 1st 4 lines only

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



Try

awk -vLNO=4 'NR<=LNO {TMP[NR]=$0;next} 1; NR==LNO*2 {for (i=1; i<=LNO; i++) print TMP}' file
5
6
7
8
1
2
3
4
9
10
1 Like

Hello invinzin21,

Following may help you in same.

 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
 

Output will be as follows.

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
 

Thanks,
R. Singh

1 Like

cat to explain? the syntax?

---------- Post updated at 11:01 PM ---------- Previous update was at 11:00 PM ----------

cat to explain? the syntax? so next time i wont ask here.. cahrcter by character

awk -vLNO=4 '                                   # supply desired line count into LNO variable
NR<=LNO         {TMP[NR]=$0;next}               # rows up to LNO stored in TMP array, don''t print
1                                               # default action: print
NR==LNO*2       {for (i=1; i<=LNO; i++)         # print the TMP array after another LNO rows (assumption, not specified)
                        print TMP}
' file

Hello invinzin21,

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.
 

Thanks,
R. Singh

Hi.

The basic line editor ed has a "batch" mode and a "move" instruction:

#!/usr/bin/env bash

# @(#) s1	Demonstrate move lines with line editor, ed.

# Infrastructure: scaffolding, functions, debugging, identification, etc.
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C ed

FILE=${1-data1}
cp sacred $FILE

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
ed $FILE 2>/dev/null <<EOF
1,4m8
w
q
EOF
cat $FILE

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
ed GNU Ed 0.7

-----
 Input data file data1:
1
2
3
4
5
6
7
8
9
10

-----
 Results:
5
6
7
8
1
2
3
4
9
10

Best wishes ... cheers, drl

Another awk:

awk -v n=4 'NR<=n{p=p $0 ORS; next}1; NR==2*n{printf "%s",p}' file

or sed with shell variables

n=4; sed -n "1,$n{H;d;}; $((2*n))G;s/\n//;p" file

You could also try:

awk 'NR<5{A=A"\n"$0; next} NR==8{$0=$0A} 1' file

NR<5{A=A"\n"$0; next} Build A up with first 4 lines, but dont print them
NR==8{$0=$0A} append A to end of line 8
1 print current line