就是一篇普普通通 bash, awk, seed 等程序的学习笔记。

Bash

Variables

Assignment:

var-name=$(command)
tmp=$(echo "hello world"); echo $tmp

For Loop

Basic syntax:

for var in directory/*; do [code to run]; done

Also, nested for loop:

for i in {A..Z}; do for j in {1..5}; do touch $i$j.dat; done; done

While loop

This time, take standard output of cat as an input:

cat fileName | while read var; do [code]; done

grep

A program searches input that match a paritular pattern or regular expression.

cat fileName | grep 'Something'

sed

A text stream editor.

sed [options] command [file...]

Example:

echo Hello World | sed "s/Hello/Hi/l"
# substitute the first Hello with Hi. flag l specifies case sensitive mode

cat smallfile.csv | sed 's/,/\t/g'
# subsitute every , with \t. flag g allows substitute globally, not just the first instance of match.

Inserts line following detection of pattern:

sed '/pattern/a\line-to-append' input

Deletes from line number n to end of file:

sed 'n,$d' input
# where $ indicates the last line, and d specifies operation mode to delete.

When we have two patterns:

sed -e '/pattern/a\line-to-append' -e 'n,$d'

awk

A ultility for processing structured text files. It regards each line in a file as a record and broken into fields. Good to parse tables.

Usage: pattern {action}

awk 'BEGIN'{print "File\tOwner"} {print $9, "\t", $3} END{print "done"}'

Note:

BEGIN and END forms some sort of loop.
Use ( and ) for selection.
FNR refers to the record number(the line number)
NR refers to the total record number
When working across file, FNR resets back to 1 but NR keeps increasing.

Wildcard

  • any character zero or more times
    ? any single character
    [] a range of characters
    {hello, world} exactly one word

Wilcard can be combined.

除另有声明外,本博客文章均采用 知识共享(Creative Commons) 署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。