awk 的基本使用举例

$ cat test.txt 
Frank
Testing 1
Hello World
TesT
123 Testing
 
bogon:awk-test lihex$ awk '{ print $1 }' test.txt 
Frank
Testing
Hello
TesT
123

bogon:awk-test lihex$ awk '{ print $1.$2 }' test.txt 
Frank
Testing1
HelloWorld
TesT
123Testing

bogon:awk-test lihex$ awk '{ print $1.$2.txt }' test.txt 
Frank
Testing1
HelloWorld
TesT
123Testing

bogon:awk-test lihex$ awk '/Test/{ print}' test.txt 
Testing 1
123 Testing
bogon:awk-test lihex$ awk '/[a-z]/{ print}' test.txt 
Frank
Testing 1
Hello World
TesT
123 Testing
bogon:awk-test lihex$ awk '/[0-9]/{ print}' test.txt 
bogon:awk-test lihex$ awk '{ print $1 }' test.txt 
Frank
Testing
Hello
TesT
123

bogon:awk-test lihex$ awk '{ print $1.$2 }' test.txt 
Frank
Testing1
HelloWorld
TesT
123Testing

bogon:awk-test lihex$ awk '{ print $1.$2.txt }' test.txt 
Frank
Testing1
HelloWorld
TesT
123Testing

bogon:awk-test lihex$ awk '/Test/{ print}' test.txt 
Testing 1
123 Testing
bogon:awk-test lihex$ awk '/[a-z]/{ print}' test.txt 
Frank
Testing 1
Hello World
TesT
123 Testing
bogon:awk-test lihex$ awk '/[0-9]/{ print}' test.txt 
Testing 1
123 Testing
bogon:awk-test lihex$ awk '/[0-9]$/{ print}' test.txt 
Testing 1
bogon:awk-test lihex$ awk '{ if($1 ~ /123/) print }' test.txt 
123 Testing
bogon:awk-test lihex$ awk '{ if($2 ~ /[0-9]/) print }' test.txt 
Testing 1
bogon:awk-test lihex$ grep -i test test.txt 
Testing 1
TesT
123 Testing

bogon:awk-test lihex$ vim test.txt 
bogon:awk-test lihex$ cat test.txt 
Frank:Perez
Testing 1:Testing 2
Hello World:Hi World
TesT:OMG
123 Testing:456 Testing
bogon:awk-test lihex$ awk -F: '{ print $1 }' test.txt 
Frank
Testing 1
Hello World
TesT
123 Testing
Testing 1
123 Testing
bogon:awk-test lihex$ awk '/[0-9]$/{ print}' test.txt 
Testing 1
bogon:awk-test lihex$ awk '{ if($1 ~ /123/) print }' test.txt 
123 Testing
bogon:awk-test lihex$ awk '{ if($2 ~ /[0-9]/) print }' test.txt 
Testing 1
bogon:awk-test lihex$ grep -i test test.txt 
Testing 1
TesT
123 Testing
bogon:awk-test lihex$ cat test.txt 
Frank:Perez
Testing 1:Testing 2
Hello World:Hi World
TesT:OMG
123 Testing:456 Testing
bogon:awk-test lihex$ awk -F: '{ print $1 }' test.txt 
Frank
Testing 1
Hello World
TesT
123 Testing

猜你喜欢

转载自hexlee.iteye.com/blog/1709195