How to undo everything after the second line with AWK

I am looking for a way to ignore the first two lines of a file and flip ips / dns in everything after the second line. Please note that I am the sed removefirst line (which is the header).

bash-4.4$ less test
  1 #remove
  2 #comment 1
  3 #comment 2
  4 foo 127.0.0.1
  5 bar 127.0.0.1

the results I'm looking for

bash-4.4$ less test-fixed
  1 #comment 1
  2 #comment 2
  3 127.0.0.1 foo
  4 127.0.0.1 bar

command channel I tried:

FILE=/tmp/test ; sed '1d' $FILE | awk 'NR>2 { t = $1; $1 = $2; $2 = t; print; } ' >| /tmp/test-fixed

obviously the NR>2sequence number of the current record is skipped to line 3, so I think I need an iteration loop to print, but it doesn't work until N3it is reached? Not sure...

+4
source share
3 answers

You can use awkas follows:

awk 'NR>3{s=$NF; $NF = $(NF-1); $(NF-1) = s} 1' file

1 #remove
2 #comment 1
3 #comment 2
4 127.0.0.1 foo
5 127.0.0.1 bar
+4
source
awk 'NR > 1 && NR <= 3; NR > 3 {print $2, $1}' input.txt

Output

#comment 1
#comment 2
127.0.0.1 foo
127.0.0.1 bar
+3
source

$ awk 'NR==1 {next} 
       NR>3  {t=$3; $3=$2; $2=t} 
             {print NR-1,$2,$3}' file

1 #comment 1
2 #comment 2
3 127.0.0.1 foo
4 127.0.0.1 bar

$ awk 'NR>3{t=$3;$3=$2;$2=t} --$1' file
+2

Source: https://habr.com/ru/post/1693041/


All Articles