text

How do you pull three lines out of a log without opening an editor

That is the entire reason these tools exist

The sample

printf 'error boom\nok fine\nerror again\n' > log.txt

Every command below ran against that file

grep

grep -c '^error' log.txt

Output:

2

-c counts matching lines ^ anchors at the start of a line

sed

sed -n '1p' log.txt

Output:

error boom

-n suppresses automatic printing 1p prints line one For substitutions, sed 's/old/new/' file is the shape to remember

awk

awk 'END {print NR}' log.txt

Output:

3

NR is the record number awk sees the file as rows and fields $1 is the first field

cut and sort

cut -d' ' -f1 log.txt | sort -u

Output:

error
ok

-d' ' sets the delimiter -f1 takes the first field sort -u removes duplicates

Next

Pipes make these tools talk in 05 streams

Posted