2020年8月18日星期二

Shell Command: awk. For getting substring from origin string

入门参考:  ref: https://www.tohoho-web.com/ex/awk.html


awk: for getting substring from origin string

$ cat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.

With no FILE, or when FILE is -, read standard input.

  -A, --show-all           equivalent to -vET
  -b, --number-nonblank    number nonempty output lines, overrides -n
  -e                       equivalent to -vE
  -E, --show-ends          display $ at end of each line
  -n, --number             number all output lines
  -s, --squeeze-blank      suppress repeated empty output lines
  -t                       equivalent to -vT
  -T, --show-tabs          display TAB characters as ^I
  -u                       (ignored)
  -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
      --help     display this help and exit
      --version  output version information and exit
-F       set delimiter
Examples:
  cat f - g  Output f's contents, then standard input, then g's contents.
  cat        Copy standard input to standard output.
========================================
Examples for using:
$cat test.tsv
111 222 333 444 555
111 222 333 444 555
111 222 333 444 555

$cat test.tsv | awk '{print $2}'  #print column2
222
222
222
$cat test.tsv | awk '{print $3,$4,$2}'  #print format column3,column4,column2
333 444 222
333 444 222
333 444 222

$ cat test2.csv
111 111 111
222 222 222
333 333 333
444 444 444
555 555 555
$cat test2.csv | awk 'NR==2 {print}'  #print all columns of row2.  NR==2 
222 222 222
ls -l
total 11232
drwxrwxr-x  7 usr usr      107 Jun 25  2019 cmd
-rw-rw-r--  1 usr usr      102 Aug 17 11:20 compile.log
-rwxrw-r--  1 usr usr      726 Aug  5 15:53 compile.sh
drwxrwxr-x  3 usr usr       74 Jun 25  2019 doc
-rw-rw-r--  1 usr usr      620 Jan 20  2020 go.mod
-rw-rw-r--  1 usr usr     6550 Jun 25  2019 Gopkg.lock
-rw-rw-r--  1 usr usr      655 Jun 25  2019 Gopkg.toml
...
$ls -l |awk '$5<=100 {print $9}' #print filename on size <=100.
doc
...

$ cat testdata
aa bb test@mail.local 001 002
cc dd test2@test.local 003 004
$ cat testdata | awk -F@ '{print $2}'   # -F: set splitword as @, print col2
mail.local 001 002
test.local 003 004
$ cat testdata | awk -F@ '{print $2}' | awk '{print $1}'   #print col1
mail.local
test.local

$cat var_list.txt
./test.sh: line 1: echo: No such file or directory
echo; var_count:0; var:
./duplicate_error_page.html; var_count:0; var:
./main.html; var_count:3; var:,scre_cd,fileName,ajaxParameter
./main.html; var_count:3; var:,scre_cd,parameter,toPlanForm
./main.html; var_count:1; var:,scre_cd

$cat var_list.txt | awk -F '[;:]' '{print $1,$3,$5}'
echo; var_count:0; var:
./duplicate_error_page.html  0 
./main.html 3 ,scre_cd,fileName,ajaxParameter
./main.html 3 ,scre_cd,parameter,toPlanForm
./main.html 1 ,scre_cd

#get lines between including pattern1 and including pattern2. ex.
$cap tmp.html | awk '/<ul class="list-main"/,/<\ul>/' 
<ul class="list-main"> <li>aaaaa</li> <li>bbbbb</li> </ul>

$ awk 'BEGIN {
   num = 5; if (num >= 0 && num <= 7) printf "%d is in octal format\n", num 
}'
$ awk 'BEGIN {
   ch = "\n"; if (ch == " " || ch == "\t" || ch == "\n") 
   print "Current character is whitespace." 
}'
'BEGIN { name = ""; if (! length(name)) print "name is empty string." }'
ref:https://www.tutorialspoint.com/awk/awk_logical_operators.htm

没有评论: