how to extract some lines from a text file using unix shell script awk.
e.g.
1) input: file_name_test.txt
**<header> asdfdsafdsf**
11 asd sad
12 sadf asdf
13 asdfsa asdf
14 asd sdaf
**15 asd asdfsdf
16 sadfsadfsaf sdfsdf
17 asdf sdaf
18 asfd saf
19 sadf asdf
10 asf asf**
2) expected output:
**<header> asdfdsafdsf
15 asd asdfsdf
16 sadfsadfsaf sdfsdf
17 asdf sdaf
18 asfd saf
19 sadf asdf
10 asf asf**
3) code for test.sh:
FILENAME=$1
threshold=$2
awk '{line_count++;
if (line_count==1 || (line_count>$threshold))
print $0;
}' $FILENAME > overflow_new2
4)
sh test.sh file_name_test.txt 5
5) It only prints the first line which is:
<header> asdfdsafdsf
in the output file overflow_new2. and return these lines in putty:
awk: Field $() is not correct.
The input line number is 2. The file is file_name_test.txt
The source line number is 2.
Any idea? Thank you.
You need to pass the shell variables to
awkusing-vflag:When run like:
Results/contents of
overflow_new2:Also, to reproduce the desired results exactly, here’s the way I’d do it: