add new awk one-liners
- signed-off-by: trimstray <trimstray@gmail.com>
This commit is contained in:
parent
b2d6885850
commit
35310ec674
117
README.md
117
README.md
@ -3685,10 +3685,25 @@ python -m base64 -d <<< "dGhpcyBpcyBlbmNvZGVkCg=="
|
||||
|
||||
##### Tool: [awk](http://www.grymoire.com/Unix/Awk.html)
|
||||
|
||||
###### Remove duplicate entries in a file without sorting
|
||||
###### Search for matching lines
|
||||
|
||||
```bash
|
||||
awk '!x[$0]++' filename
|
||||
# egrep foo
|
||||
awk '/foo/' filename
|
||||
```
|
||||
|
||||
###### Search non matching lines
|
||||
|
||||
```bash
|
||||
# egrep -v foo
|
||||
awk '!/foo/' filename
|
||||
```
|
||||
|
||||
###### Print matching lines with numbers
|
||||
|
||||
```bash
|
||||
# egrep -n foo
|
||||
awk '/foo/{print FNR,$0}' filename
|
||||
```
|
||||
|
||||
###### Print the last column
|
||||
@ -3697,6 +3712,55 @@ awk '!x[$0]++' filename
|
||||
awk '{print $NF}' filename
|
||||
```
|
||||
|
||||
###### Find all the lines longer than 80 characters
|
||||
|
||||
```bash
|
||||
awk 'length($0)>80{print FNR,$0}' filename
|
||||
```
|
||||
|
||||
###### Print only lines of less than 80 characters
|
||||
|
||||
```bash
|
||||
awk 'length < 80 filename
|
||||
```
|
||||
|
||||
###### Print double new lines a file
|
||||
|
||||
```bash
|
||||
awk '1; { print "" }' filename
|
||||
```
|
||||
|
||||
###### Print line numbers
|
||||
|
||||
```bash
|
||||
awk '{ print FNR "\t" $0 }' filename
|
||||
awk '{ printf("%5d : %s\n", NR, $0) }' filename # in a fancy manner
|
||||
```
|
||||
|
||||
###### Print line numbers for only non-blank lines
|
||||
|
||||
```bash
|
||||
awk 'NF { $0=++a " :" $0 }; { print }' filename
|
||||
```
|
||||
|
||||
###### Print the line and the next two (i=5) lines after the line matching regexp
|
||||
|
||||
```bash
|
||||
awk '/foo/{i=5+1;}{if(i){i--; print;}}' filename
|
||||
```
|
||||
|
||||
###### Print the lines starting at the line matching 'server {' until the line matching '}'
|
||||
|
||||
```bash
|
||||
awk '/server {/,/}/' filename
|
||||
```
|
||||
|
||||
###### Print multiple columns with separators
|
||||
|
||||
```bash
|
||||
awk -F' ' '{print "ip:\t" $2 "\n port:\t" $3' filename
|
||||
```
|
||||
|
||||
###### Remove empty lines
|
||||
|
||||
```bash
|
||||
@ -3706,10 +3770,29 @@ awk 'NF > 0' filename
|
||||
awk NF filename
|
||||
```
|
||||
|
||||
###### Print multiple columns with separators
|
||||
###### Delete trailing white space (spaces, tabs)
|
||||
|
||||
```bash
|
||||
awk -F' ' '{print "ip:\t" $2 "\n port:\t" $3' filename
|
||||
awk '{sub(/[ \t]*$/, "");print}' filename
|
||||
```
|
||||
|
||||
###### Delete leading white space
|
||||
|
||||
```bash
|
||||
awk '{sub(/^[ \t]+/, ""); print}' filename
|
||||
```
|
||||
|
||||
###### Remove duplicate consecutive lines
|
||||
|
||||
```bash
|
||||
# uniq
|
||||
awk 'a !~ $0{print}; {a=$0}' filename
|
||||
```
|
||||
|
||||
###### Remove duplicate entries in a file without sorting
|
||||
|
||||
```bash
|
||||
awk '!x[$0]++' filename
|
||||
```
|
||||
|
||||
###### Exclude multiple columns
|
||||
@ -3718,6 +3801,18 @@ awk -F' ' '{print "ip:\t" $2 "\n port:\t" $3' filename
|
||||
awk '{$1=$3=""}1' filename
|
||||
```
|
||||
|
||||
###### Substitute foo for bar on lines matching regexp
|
||||
|
||||
```bash
|
||||
awk '/regexp/{gsub(/foo/, "bar")};{print}' filename
|
||||
```
|
||||
|
||||
###### Add some characters at the beginning of matching lines
|
||||
|
||||
```bash
|
||||
awk '/regexp/{sub(/^/, "++++"); print;next;}{print}' filename
|
||||
```
|
||||
|
||||
###### Get the last hour of Apache logs
|
||||
|
||||
```bash
|
||||
@ -3729,7 +3824,7 @@ ___
|
||||
|
||||
##### Tool: [sed](http://www.grymoire.com/Unix/Sed.html)
|
||||
|
||||
###### To print a specific line from a file
|
||||
###### Print a specific line from a file
|
||||
|
||||
```bash
|
||||
sed -n 10p /path/to/file
|
||||
@ -3790,12 +3885,6 @@ grep -RnisI "pattern" *
|
||||
fgrep "pattern" * -R
|
||||
```
|
||||
|
||||
###### Remove blank lines from a file and save output to new file
|
||||
|
||||
```bash
|
||||
grep . filename > newfilename
|
||||
```
|
||||
|
||||
###### Show only for multiple patterns
|
||||
|
||||
```bash
|
||||
@ -3832,6 +3921,12 @@ grep -- -- filename
|
||||
grep "\-\-" filename
|
||||
```
|
||||
|
||||
###### Remove blank lines from a file and save output to new file
|
||||
|
||||
```bash
|
||||
grep . filename > newfilename
|
||||
```
|
||||
|
||||
##### Tool: [perl](https://www.perl.org/)
|
||||
|
||||
###### Search and replace (in place)
|
||||
|
Loading…
Reference in New Issue
Block a user