Most of example come from problem in Linux Shell - Hackerrank Text Processing Commands
Trip:
sed
in OSX may not working as example. You should trygsed
.
gsed
can install via brew and callgsed
instead ofsed
$ cat in
1234 5678 9101 1234
2999 5178 9101 2234
$ sed -re 's/([0-9]{4}\s){3}/**** **** **** /' in
**** **** **** 1234
**** **** **** 2234
$ cat in
1234 5678 9101 1234
2999 5178 9101 2234
$ sed -r 's/([0-9]{4}\s?)([0-9]{4}\s?)([0-9]{4}\s?)([0-9]{4})/\4
\3\2\1/' in
1234 9101 5678 1234
2234 9101 5178 2999
$ cat in
hello cat meow meow Cat Cat caT love cAt
$ sed -e 's/cat/{\0}/gi' in
hello {cat} meow meow {Cat} {Cat} {caT} love {cAt}
$ cat in
hello cat meow meow Cat Cat caT love cAt
$ sed -e 's/cat/dog/gi' in
hello dog meow meow dog dog dog love dog
$ cat in
A 25 27 50
B 35 75
C 75 78
D 99 88 76
$ awk '{ if ($4 =="") print "Not all scores are available for",$1; }' in
Not all scores are available for B
Not all scores are available for C
$ cat in
B 35 37 75
C 75 78 80
$ awk '{print $1,":",($4 >= 50 && $2 >= 50 && $3 >= 50)?"Pass":"Fail"}' in
B : Fail
C : Pass
$ cat in
B 35 37 75
C 75 78 80
$ awk '{ score=($2+$3+$3)/3; \
if (score >= 80) grade = "A"; \
else if (score >= 60) grade = "B"; \
else if(score >= 50) grade= "C"; \
else grade = "FAIL "; \
print $0,":",grade }'
B 35 37 75 : FAIL
C 75 78 80 : B
$ cat in
A 25 27 50
B 35 37 75
C 75 78 80
D 99 88 76
$ awk 'ORS=NR%2?";":"\n"' in
A 25 27 50;B 35 37 75
C 75 78 80;D 99 88 76
$ grep -o text # show only match
$ paste file1 file2
$ paste -d, file1 file2
$ paste -s file1
# $ {var::-1}
$ a=123
$ echo "${a::-1}"
12