Tikejhya: Ashish Nepal

Knowledgebase

Category: GREP

grep split string and get last field

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Get me last value from delimeter
Grek way

[root@ip-10-31-4-69 inventories]# echo "kube_node_1_111" | grep -o '[^_]*$'
111

OR

Awk way

[root@ip-10-31-4-69 inventories]# echo "kube_node_1123" | awk -F "_" '{print $NF}'
1123

diff between two files

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

diff -u file1 file2 | grep -E “^\+”

print all but first two column

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

will print all but very first column:

cat somefile | awk '{$1=""; print $0}'

will print all but two first columns:

cat somefile | awk '{$1=$2=""; print $0}'

Split content of file using blank line as seperator

# split content of file using blank line as seperator

perl -ne 'if (($s .= $_) =~ s/(.*)n{2}//s) {open F, sprintf(">request_%04d", $n++); print F $1; close F}' test_goto

SED Remove All leading Blank spaces

Remove All leading Blank spaces

sed 's/^[ t]*//'

e.g.

[root@localhost /]# rpm -qa --last | tail -n 1 | cut -f2- -d' ' | sed 's/^[ t]*//'

Remove All leading and trailing Blank Spaces

cat input.txt | sed 's/^[ t]*//;s/[ t]*$//' > output.txt

Grep ipaddress

Grep ipaddress,
grep ‘[0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*’ file.txt
grep ‘^[^.][^.]*.[^.][^.]*.[^.][^.]*.[^.][^.]*$’ file.txt

Escape blank line with cat

Escape blank line with cat

grep -v "^$"

Cat / Grep numeric number delimited by comma values stored in third column only numeric and , seperated. 😀
This would be ideal to get the ID’S or any numeric value from log.

cat filetocat.txt | awk '{print $3}' | grep -v [a-z] | grep -v "^$" | tr ',' 'n' | wc -l

Grep Pattern matching

tail -100 /var/log/httpd/access_log | grep -E ’20:[0-9]|22:[0-9]’

this will show only logs from 20:00 hour and 22:00 hours.. 😀

Powered by WordPress & Theme by Anders Norén