Awk shell cmds
10 May 2018 11:33
Gvim Commands’ Tricks:
Shifttg --> end of file
Esc then ? --> search
se: wrap --> wrap the lines
gtf --> open any files from a given line
:b+w --> goes back the previous file
opened in gvim.
:n --> goes forward to the next file
opened in gvim.
/pattern1\|pattern2\|pattern3 --> for
searching/highlighting multiple patterns
1. Print If value of some column is
greater than:
cat file1 | awk ‘{if( $1 > = 100) print
$0}”
If ($5 !~ “.*us”)
2. Sum all the numbers in any
particular column:
cat file1 | awk ‘{n += $1} END{print n}
3. PRINTING with custom delimiters
awk -F “[0-9]” {print $1}
4. For searching/printing using line
numbers
awk ‘{ if(NR == 20 ) print $2}
awk ‘NR > 20 && NR < 30’
awk ‘NR == 20’ file-name
awk ‘{print NR}
5. For awk printing selective, multiple
columns
awk ‘{$1=$3=”" ”; print $0} // print all
except 1 & 3 column
awk ‘{printf “ %s”, $1}
6. Searching Patterns column specific:
awk ‘$2 ~ /ME3/ || $1 ~ /ME3/
awk ‘$1 ! /fill’ — // everything except
this pattern
Awk removing newl lines to spaces in
single line:
awk ‘{print} ORS=’‘ // any char can
be inserted in between ”.
7. awk: search patterns in multiple
runs,
awk ‘/PIN/,/RECT /’ //'ll print all lines
between these two matches
awk ‘/PIN|END/’ //'Il print only the
lines with these matches
8. Translating lower to upper cases,
spaces to tabs, ( to { etc.
tr [a-z] [A-Z]
tr-s ‘[:space:]
tr-d‘t // removes t
tr -s ‘\n’ ‘’// joins all lines in a file
p66
9. Sorting with count, By column
sort -n // for sorting by value
unig -c_ // for getting unique with
count
sort -n -k2 //for sorting on the basis of
2nd column
sort -r. // to reverse the order of sort
sort -u -t, -k1,1 file
e -u for unique
e -t, so comma is the
delimiter
¢ -k1,1 for the key field 1
10.Sed: Very useful in removing or
substituting patterns in places where
there are no columns or easier pattern.
sed -e ‘s/\V(//g‘ to replace or delete a
pattern
sed ‘/\s*&/d’ will delete this
pattern --> Remove Blank-Lines.
Delete lines between two line-numbers
sed '234,345d'
Extract a specific line:
sed -n ‘231p’ < file-name >
Replace a Pattern on a particular line-
number:
sed '34s/AAA/BBB/'
Delete lines between two patterns:
* sed ‘/pattern1/,/pattern2/d’
¢ sed ‘/pattern1/,/pattern2/{//!d}’ >
deletes the lines bt, excluding the
pattern
* sed ‘2,4d’ — deletes
between these line-numbers
* sed ‘/pattern1/,$d’ —
deletes all lines after the pattern
Replace lines between pattern:
sed '/# Annotate parasitics/r
insert_lines.txt' file1 > file2 —> add the
lines to rpelace in the file
11. xargs: xargs -n2 -d’\n’ // joins
alternate lines via the newline
paste file1 file2. // for pasting files side
by side
The columns can be formatted later
using awk.
12. grep:
-w_ search whole words
-i Ignore case
-| give only the filenames having the
pattern
-2a or -3b: gives 2 / 3 lines up/down of
the matched line.
Better is -A 1 or -B 2 for later & before
respectively.
-oh matches only words in the files not
lines.
13. Head & tail:
Delete the last two lines of a big
file
| head -n -2
Delete the first two lines of a big
file
| tail -n -2