keidarcy / snippets

personal notes
18 stars 8 forks source link

`sed`, `grep`, `awk` #5

Open keidarcy opened 2 years ago

keidarcy commented 2 years ago
keidarcy commented 2 years ago
grep -n -i 'root' /tmp/test_grep.txt --color=auto 
keidarcy commented 2 years ago
sed "2,+3p" test.txt -n
sed "/with/p" test.txt -n
sed '2,$d' test.txt
sed -E -e "s/(Y|y)ou/Me/" test.txt -e "s/may/might/g"
sed "2i hello" test.txt -i
sed "2a hello" test.txt -i
sed "a -------------------------------" test.txt
ifconfig lo0 | sed "2p" -n | sed -e "s/^.*=//" -e "s/<.*//g"
keidarcy commented 2 years ago
awk '{print $1}' test.txt
awk '{print $1,$2,$3}' test.txt
awk '{print "Hello: "$1,"World: "$2}' test.txt
awk 'NR==2,NR==3{print $1}' test.txt
awk '{print NR,$1}' test.txt
awk '{print $1, $(NF-1)}' test.txt
keidarcy commented 2 years ago
echo $PATH | awk -F ":" '{print $1,$NF}'
echo $PATH | awk -v FS=":" '{print $1}'
echo $PATH | awk -v FS=":" '{print $1,"--------------",$2}'
echo $PATH | awk -v FS=":" -v OFS="\n" '{print $1,$2}'
keidarcy commented 2 years ago
awk 'BEGIN{print "something here"}{print $1}' test.txt
awk 'BEGIN{print "-----------something before start-----------"}{print $1}END{print "--------------something after end--------------"}' test.txt
awk 'NR<4{print $0}' test.txt
awk 'NR<4{print $0}' test.txt
awk '/\/usr\/bin\/false$/{print NR,$0}' test.txt
awk '/^_game/,/^_dist/{print $0}' test.txt