poniedziałek, 10 kwietnia 2017

awk handy examples

Testing input:
Marek
Testing
Hello
TesT
123

1. All lines include string 
marek-> awk '/Test/ {print}' test.txt
Testing 1
123 Testing

2. All lines include numbers
marek-> awk '/[0-9]/ {print}' test.txt
Testing 1
123 Testing

3. All lines started with numbers

marek-> awk '/^[0-9]/ {print}' test.txt
123 Testing

4. All lines ended with numbers

marek-> awk '/[0-9]$/ {print}' test.txt
Testing 1


5. Print if in first column 123 will be found

marek-> awk '{ if($1 ~ /123/) print }' test.txt
123 Testing
123 Marek

6. Print if in second column awk found numbers

marek-> awk '{ if($2 ~/[0-9]/) print }' test.txt
Testing 1
Marek 1983


7. Grep Lines in file consist string

marek-> grep -i test test.txt
Testing 1
TesT
123 Testing

[~/dev/bash/awk]
marek-> grep -i test test.txt | awk '/[0-9]/{ print }'
Testing 1
123 Testing


8. Delete duplicate lines from file

[~/dev/bash/awk]
ansible-> cat file.txt
1
1
1
1
1
2
2
2
2
2
13
[~/dev/bash/awk]
ansible-> awk '!x[$0]++' file.txt
1
2
13
[~/dev/bash/awk]


9. print lines number - like wc

marek-> awk '{print NR-1 " " $0}' /etc/passwd
0 root:x:0:0:root:/root:/bin/bash
1 daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
2 bin:x:2:2:bin:/bin:/usr/sbin/nologin


10. sed vs. awk string replacement

marek-> sed 's/Marek/Wiktor/g' test.txt
Wiktor
Testing 1
Hello:World
TesT
123 Testing
123 Wiktor
Wiktor:1983
OMG:Test

marek-> cat test.txt | awk '{gsub("Wiktor","Marek"); system("echo " $0) }'
Marek
Testing 1
Hello:World
TesT
123 Testing
123 Marek
Marek:1983
OMG:Test



Brak komentarzy:

Prześlij komentarz