Post

Bash

Tenth Line

1
2
3
4
5
6
i=0
while (( i++ < 10 ))
do
  read line
done < file.txt
echo $line
1
tail -n +10 file.txt | head -1
1
sed -n 10p file.txt

AWK

Predefined Variables

awk divides the input into records and fields.

  • NR: total number of input records read so far from all data files. It starts at zero and is never automatically reset to zero
  • FNR: number of records that have been read so far from input. It’s reset to zero every time a new file is started
  • NF: number of fields in the current record

Tenth Line

1
awk 'NR == 10 {print; exit}' file.txt
1
awk 'FNR == 10 {print; exit}' file.txt

END rule is executed once only, after all the input is read. Cleanup actions.

Transpose File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
awk '
{
    for (i = 1; i <= NF; i++) {
        if (NR == 1) {
            s[i] = $i;
        } else {
            s[i] = s[i] " " $i;
        }
    }
}
END {
    for (i = 1; s[i] != ""; i++) {
        print s[i];
    }
}' file.txt

Print

Word Frequency

1
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{ print $2, $1 }'
This post is licensed under CC BY 4.0 by the author.