cli tricks
1.0.0
Often in our daily work we encounter the need to run stuff in CLI - and too often this proves to be trickier than one would expect. In the spirit of saving time for others, we've decided to compile a list of the ones we've found to be useful and not-so-obvious.
grep -o '".*"' | tr -d '"'grep -oP "(?<=').*?(?=')"grep -oP '(?<=()[^)]+'
grep -oP '(K[^)]+'grep -o "<MATCH>.*"grep -E -o "([0-9]{1,3}[.]){3}[0-9]{1,3}"grep -oh "w*<MATCH>w*"grep --only-matching '[[:digit:]]+'grep -e '^[^/]*/[^/]*$'grep -E -o '<MATCH>w+'grep -E "MATCH" | cut -d "," -f2 | awk '{print $1}'grep -oh "w*<STRING>w*"grep -o -P '(?<=PATTERN1).*(?=PATTERN2)'awk 'NR>1{print $1}' RS=[ FS=]awk -F'[()]' '{print $2}'awk '{gsub("[^[:digit:]]+"," ")}1'awk '/regex/ { print x }; { x=$0 }'awk '/regex/ { print (x=="" ? "match on line 1" : x) }; { x=$0 }'awk -v srch="<PATTERN>" 'BEGIN{l=length(srch)}{t=match($0,srch);if(!t){next}$0=substr($0,t+l);print srch" "$2}' <filename> | awk '{print $1}'awk '{ print substr($0, index($0,$3)) }'awk '/AAA|BBB|CCC/'awk '{$<COL_NUMBER> = "<VALUE>"; print}'cat FILENAME.txt | awk 'BEGIN { print "<table>" }
{ print "<tr><td>" $1 "</td><td>" $2 "</td><tr>" }
END { print "</table>" }'
cat toTable | awk 'BEGIN { print "<tbody>" }
{ print "<tr><td><strong>" $1 "</strong></td>" }
{ print "<td>" $2 "</td></tr>" }
END { print "</tbody>" }'awk -v FS="(PATTERN1|PATTERN2)" '{print $2}'s/ <-- this means it should perform a substitution
.* <-- this means match zero or more characters
[ <-- this means match a literal [ character
( <-- this starts saving the pattern for later use
[^]]* <-- this means match any character that is not a [ character
the outer [ and ] signify that this is a character class
having the ^ character as the first character in the class means "not"
) <-- this closes the saving of the pattern match for later use
] <-- this means match a literal ] character
/1 <-- this means replace everything matched with the first saved pattern
(the match between "(" and ")" )
/g <-- this means the substitution is global (all occurrences on the line)
< EXACT MATCH >
sed '/regex/{x;p;x;}'sed '/regex/G'sed '/regex/{x;p;x;G;}'sed '/./=' filename | sed '/./N; s/n/ /'sed '/baz/s/foo/bar/g'sed '/baz/!s/foo/bar/g'sed '1!G;h;$!d'
sed -n '1!G;h;$p' sed -e :a -e '/\$/N; s/\n//; ta' sed -e :a -e '$!N;s/nMATCH/ /;ta' -e 'P;D'sed '/n/!G;s/(.)(.*n)/&21/;//D;s/.//'sed 's/witch/red/g;s/gem/red/g;s/puss/red/g' # most seds
gsed 's/witch|gem|puss/red/g' # GNU sed onlysed 's/foo/bar/' # replaces only 1st instance in a line
sed 's/foo/bar/4' # replaces only 4th instance in a line
sed 's/foo/bar/g' # replaces ALL instances in a line
sed 's/(.*)foo(.*foo)/1bar2/' # replace the next-to-last case
sed 's/(.*)foo/1bar/' # replace only the last casesed 's/^[ t]*//' # see note on 't' at end of filesed 's/[ t]*$//' # see note on 't' at end of filesed -n '/regexp/p' # method 1
sed '/regexp/!d' # method 2sed -n '/regexp/{g;1!p;};h'sed -n '/regexp/{n;p;}'sed '/^s*$/d' filesed '/MATCH/aADD_THIS' filesed '/MATCH/i<INSERT>' filesed 's/^/<STRING>/'sed '/AAA/!d; /BBB/!d; /CCC/!d'sed '/<MATCH>/s/^/<STRING>/'sed -e '/./{H;$!d;}' -e 'x;/MATCH/!d;'sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e dsed -n '/regexp/,$p'sed '/s/$/<string>/'sed '/<MATCH>/s/$/ myalias/'sed '/match/ s/$/ anotherthing/' filesed -e :a -e '/\$/N; s/\n//; ta'sed 's/^[ t]*//;s/[ t]*$//'sed 's/[ t]*$//'sed '/X/{$!N;/n.*Y/!P;D}'sed 's/<MATCH>/,/<MATCH>//g'sed '/<MATCH>/,/<MATCH>/!d;/;/p'sed Gsed '/^$/d;G'sed '/<MATCH>/s/<STR>/<REPLACE>/g'echo '345,0m0.047s' | sed -n -r 's/^(.*),.*[^0-9]([0-9]*).(.*)s$/1,23/p'
345,0047sed 's/[0-9][0-9];[0-9][0-9]H//g' | egrep -o '[^][]+'sed '/regex/{x;p;x;G;}'sed '/regex/{x;p;x;}'sed '/regex/G'sed -n '/ABC/,+1p' infilesed 's/<STRING1>.*<STRING2>//'sed -e 's|[<THIS><THIS>]||g'sed 's|/|:|g'sed 's/[!@#$%^&*<>"()]//g'sed -i "/aaa=/caaa=xxx" your_file_heresed 'G;G'sed 'n;d':%s/unix/Linux/gi:%s/UNIX/bar/gI:%s/<UNIX>/Linux/gc:%s/UNIX/Linux/gcalias ..='cd ..'
alias c='clear'
alias cls='clear;ls'
# Grabs the disk usage in the current directory
alias usage='du -ch | grep total'
alias ksh='du -ksh *'
# Gives you what is using the most space. Both directories and files. Varies on
# current directory
alias most='du -hsx * | sort -rh | head -10'
# ls aliases
alias lf='ls -alF --color=auto'
alias la='ls -al --color=auto'
alias ll='ls -l --color=auto'
alias l='ls -l --color=auto'
alias lh='ls -lh --color=auto'
# create directory
alias md='mkdir -p'
alias t='tail -f '
alias network='service network restart'
alias f='find / -name'
alias fhere='find . -name'
alias iptables='service iptables restart'lsof | grep "(deleted)$" | sed -re 's/^S+s+(S+)s+S+s+([0-9]+).*/1/fd/2/' | while read file; do bash -c ": > /proc/$file"; doneecho "<XML>" or cat file | xml_ppcut -d "<MATCH>" -f1perl -MURI::Escape -lne 'print uri_escape($_)'
alias hashpass='echo $PASS | awk -F : '"'"'{for (i=1;i<=NF;i++) {print $i}}'"'"perl -lne '/(K[^)]+/ and print $&'comm -13 <(sort file1) <(sort file2)perl -pe 's/(?<!^)(?=<STRING>)/n/g' <filename>diff -a --suppress-common-lines -y File1 File2rpm -qa --qf "%{NAME}n"^w{0,10}$ # allows words of up to 10 characters.
^w{5,}$ # allows words of more than 4 characters.
^w{5,10}$ # allows words of between 5 and 10 characters.tcpdump -Dtcpdump -i eth0tcpdump -i anytcpdump -vtcpdump -vvtcpdump -vvvtcpdump -v -Xtcpdump -v -XXtcpdump -qtcpdump -c 100tcpdump -w capture.captcpdump -v -w capture.captcpdump -r capture.captcpdump -vvv -r capture.captcpdump -ntcpdump -n dst host 192.168.1.1tcpdump -n src host 192.168.1.1tcpdump -n host 192.168.1.1tcpdump -n dst net 192.168.1.0/24tcpdump -n src net 192.168.1.0/24tcpdump -n net 192.168.1.0/24tcpdump -n dst port 23tcpdump -n dst portrange 1-1023tcpdump -n tcp dst portrange 1-1023tcpdump -n udp dst portrange 1-1023tcpdump -n "dst host 192.168.1.1 and dst port 23"tcpdump -n "dst host 192.168.1.1 and (dst port 80 or dst port 443)"tcpdump -v icmptcpdump -v arptcpdump -v "icmp or arp"tcpdump -n "broadcast or multicast"tcpdump -s 500tcpdump -s 0/backbox/backbox-3.0/bin/sendEmail -f [email protected] -t [email protected] -s SMTP_ADDRESS -u MailTest -o message-file=[Expert@Checkpoint]# cplic print > cplic.txt
[Expert@Checkpoint]# cat cplic | grep -o -P '..?Jan.*?....|..?Feb.*?....|..?Mar.*?....|..?Apr.*?....|..?May.*?....|..?Jun.*?....|..?Jul.*?....|..?Aug.*?....|..?Sep.*?....|..?Oct.*?....|..?Nov.*?....|..?Dec.*?....'echo -e "print <table_name> <object_name>n-qn" | dbedit -local
echo -e "printxml <table_name> <object_name>n-qn" | dbedit -localfw="xxx"; cpmiquerybin object "" network_objects "name='$fw'" |grep anti_spoofcpmiquerybin attr "" network_objects "type='gateway_cluster'" -a __name__,ipaddrcpmiquerybin attr "" network_objects "type='cluster_member'" -a __name__cpmiquerybin object "" network_objects "" |grep -A 12 cluster_members |grep Name | awk -F "(" '{printf $2}' | sed -e 's/)/|/g'
cpmiquerybin attr "" network_objects "name='cluster_name'" -a cluster_memberscpmiquerybin object "" network_objects "name='group_name_goes_here'" | grep ":Name"cpmiquerybin attr "" policies_collections "" -a __name__cpmiquerybin attr "" fw_policies "" -a __name__cpmiquerybin attr "" policies_collections "name='Standar'" -a __name__,installable_targetscpmiquerybin attr "mdsdb" network_objects "name='Cluster1'" -a __name__,ipaddrcpmiquerybin attr "" network_objects "(primary_management='false') & (management='true')" -a __name__cpmiquerybin attr "mdsdb" mdss "" -a __name__cpmiquerybin attr "mdsdb" network_objects "management='true'" -a __name__,ipaddrcpmiquerybin attr "" network_objects "management='true'" -a __name__,ipaddr cpmiquerybin attr "mdsdb" mdss "primary='true'" -a __name__cpmiquerybin attr "" services "include_in_any='true'" -a __name__cpmiquerybin attr "" network_objects "ipaddr='<IP>'" -a __name__,ipaddrGATEWAYS=( `cpmiquerybin attr "" network_objects "(type='gateway') & (location='internal')" -a __name__ | tr 'n' ' '` )
CLUSTERS=( `cpmiquerybin attr "" network_objects "(type='gateway_cluster') & (location='internal')" -a __name__ | tr 'n' ' '` )
CLUSTER MEMBERS=( `cpmiquerybin attr "" network_objects "(type='cluster_member') | (type='gateway') & (location='internal')" -a __name__ | tr 'n'cpmiquerybin attr "" network_objects "type='gateway'|type='cluster_member'|type='gateway_cluster'" -a __name__,ipaddr,svn_version_name,appliance_typeNAME="Variable"
echo $NAME
echo "$NAME"
echo "${NAME}!"$# Number of arguments
$* All arguments
$@ All arguments, starting from first
$1 First argument[[ -z STRING ]] Empty string
[[ -n STRING ]] Not empty string
[[ STRING == STRING ]] Equal
[[ STRING != STRING ]] Not Equal
[[ NUM -eq NUM ]] Equal
[[ NUM -ne NUM ]] Not equal
[[ NUM -lt NUM ]] Less than
[[ NUM -le NUM ]] Less than or equal
[[ NUM -gt NUM ]] Greater than
[[ NUM -ge NUM ]] Greater than or equal
[[ STRING =~ STRING ]] Regexp
(( NUM < NUM )) Numeric conditions
[[ -o noclobber ]] If OPTIONNAME is enabled
[[ ! EXPR ]] Not
[[ X ]] && [[ Y ]] And
[[ X ]] || [[ Y ]] Or[[ -e FILE ]] Exists
[[ -r FILE ]] Readable
[[ -h FILE ]] Symlink
[[ -d FILE ]] Directory
[[ -w FILE ]] Writable
[[ -s FILE ]] Size is > 0 bytes
[[ -f FILE ]] File
[[ -x FILE ]] Executable
[[ FILE1 -nt FILE2 ]] 1 is more recent than 2
[[ FILE1 -ot FILE2 ]] 2 is more recent than 1
[[ FILE1 -ef FILE2 ]] Same files!$ Expand last parameter of most recent command
!* Expand all parameters of most recent command
!-n Expand nth most recent command
!n Expand nth command in history
!<command> Expand most recent invocation of command <command>date -d@$(echo $(($(date +"%s")-86400))) +"%Y-%m-%d"echo -n "STRING" | perl -MURI::Escape -wlne 'print uri_escape $_'¯_(ツ)_/¯