user1872384
user1872384

Reputation: 7147

Shell Script (awk)

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.

Upvotes: 0

Views: 1866

Answers (3)

Chris Koknat
Chris Koknat

Reputation: 3451

Here is Perl code similar to glenn jackman's solution:

perl -slne 'print if $. == 1 or $. >= $n' -- -n=15 

$. is the line number

Upvotes: 0

Steve
Steve

Reputation: 54572

You need to pass the shell variables to awk using -v flag:

filename=$1
threshold=$2

awk -v thres="$threshold" '
    { line_count++ }
    line_count==1 || line_count > thres { print }
' $filename > overflow_new2

When run like:

./script.sh file_name_test.txt 5

Results/contents of overflow_new2:

**<header> asdfdsafdsf**  
**15 asd asdfsdf
16 sadfsadfsaf sdfsdf
17 asdf sdaf
18 asfd saf
19 sadf asdf
10 asf asf**

Also, to reproduce the desired results exactly, here's the way I'd do it:

filename=$1
threshold=$2

awk -v thres="$threshold" '
    FNR == 1 {
        sub(/**\s*$/,"")
        print
    }
    FNR > thres {
        sub(/^**/,"")
        print
    }
' $filename > overflow_new2

Upvotes: 0

Ken Cheung
Ken Cheung

Reputation: 1808

Let me fix your script first:

#!/bin/bash
FILENAME=$1
THRESHOLD=$2

awk -v t=$THRESHOLD '{
        lc++;
        if (lc == 1 || lc > t) {
                print $0;
        }
}' $FILENAME

Upvotes: 1

Related Questions