snippets icon indicating copy to clipboard operation
snippets copied to clipboard

`sed`, `grep`, `awk`

Open keidarcy opened this issue 3 years ago • 5 comments

  • sed
  • grep
  • awk

keidarcy avatar Apr 24 '22 14:04 keidarcy

grep -n -i 'root' /tmp/test_grep.txt --color=auto 

keidarcy avatar Apr 28 '22 13:04 keidarcy

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 avatar Apr 28 '22 13:04 keidarcy

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 avatar Apr 28 '22 15:04 keidarcy

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 avatar Apr 28 '22 15:04 keidarcy

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

keidarcy avatar Apr 29 '22 03:04 keidarcy