Tikejhya: Ashish Nepal

Knowledgebase

Category: SED (Page 1 of 2)

sed replace epoc to timestamp

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.

e.g files:
-rw-r--r-- 1 root root 500000684 Jul 22 08:21 my_log.log.01_1500707421_500000684

ls -lrt *.log* | sed 's/^/echo "/; s/\([0-9]\{10\}\)/`date -d @\1`/; s/$/"/;'

echo "-rw-r--r-- 1 root root 500000684 Jul 22 08:21 my_log.log.01_`date -d @1500707421`_500000684"

s/\([0-9]\{10\}\)/`date -d @\1`/

\1 brings your reference from LHS to RHS

in this case; you could run eval for instance ..

eval $(ls -lrt *.log* | sed 's/^/echo "/; s/\([0-9]\{10\}\)/`date -d @\1`/; s/$/"/;') | sed 's/echo/\n/g'

tail with color

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.

multitail -c /path/to/log

multitail configuration are located at ~/.multitailrc

colorscheme:logname
cs_re:green:TEST
cs_re:red:ERROR

multitail -cS logname /path/to/log

AWK Method:
tail -f /path/to/log | awk ‘/TEST/ {print “\033[32m” $0 “\033[39m”} /ERROR/ {print “\033[31m” $0 “\033[39m”}’

SED Method:
tail -f /path/to/log | sed -e ‘s/\(.*TEST.*\)/\o033[32m\1\o033[39m/’ -e ‘s/\(.*ERROR.*\)/\o033[31m\1\o033[39m/’

print lines between start and end using awk and sed

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.

AWK print pattern

[root@spark ~]# cat testfile
shit lot
of crap
in here
lets see

Awk print line starting with:
[root@spark ~]# awk '$1 ~ /^shit/' testfile
shit lot

sed print line between pattern
[root@spark ~]# sed -n '/shit/,/crap/p' testfile
shit lot
of crap

print lines between start and end using sed

print lines between start and end using sed

sed: Print line starting with 201 and ending with 2015
sed -n '/^201/{:start /2015$/!{N;b start};//p}'

How it works:
sed -n '/^STARTPATTERN/{:start /ENDPATTERN$/!{N;b start};//p}'

The “-n” option will not print anything unless an explicit request to print is found. I mentioned the “/p” flag to the substitute command as one way to turn printing back on. Let me clarify this. The command

sed ‘s/PATTERN/&/p’ file
acts like the cat program if PATTERN is not in the file: e.g. nothing is changed. If PATTERN is in the file, then each line that has this is printed twice. Add the “-n” option and the example acts like grep:

sed -n ‘s/PATTERN/&/p’ file
Nothing is printed, except those lines with PATTERN included.

The long argument of the -n command is either

sed –quiet ‘s/PATTERN/&/p’ file
or

sed –silent ‘s/PATTERN/&/p’ file

bash: set with variables inside file

sed -i "s#string-to-change.#${replacestring}#g" file.txt

Remove All duplicate lines

SED
Remove All duplicate line.

:g/%(^1>.*$n)@<=(k+).*$/d Replace Newline To space :%s/n/ /g

Bash Pattern Match

Print before and after Pattern

$ var="abcdefghij0123456789"

$ echo $var
abcdefghij0123456789

Now to print 3 characters before the pattern “h” in $var
$ echo $var | awk 'match($0,"h"){print substr($0,RSTART-3,3)}'

Output:
efg

And to print 4 characters after the pattern “h” in $var
$ echo $var | awk 'match($0,"h"){print substr($0,RSTART+1,4)}'

Output:
ij01

Combining, i.e. to print 3 characters before “h” and 4 characters after “h”
$ echo $var | awk 'match($0,"h"){print substr($0,RSTART-3,3),substr($0,RSTART+1,4)}'

Output:
efg ij01

SED [Regex]

remove all line starting with string < ^<.*$ SED - remove last four characters from string echo "hello tikejhya whzz" |sed 's/.{4}$//' Sed Delete Empty Line Syntax $ sed '/^$/d' /tmp/data.txt

Sed SRC from custom log

Mar 16 20:00:00 web1 kernel: *WEB_ATTEMPT*IN=eth1 OUT= MAC=00:208:e3:ff:08:00 SRC=111.111.111.111 DST=222.222.222.222 LEN=52 TOS=0x00 PREC=0x00 TTL=41 ID=19880 DF PROTO=TCP SPT=25499 DPT=3306 WINDOW=65453 RES=0x00 ACK URGP=0

sed ‘s/.*SRC=(.*)DST=.*/1/’ /var/log/custom | grep -v kernel | uniq -c

Cut last x character leaving the rest

cut last x character leaving the rest.
Cut last 8 character leaving the rest

sed -e's/........$//g'

Here . represents the number of character (from behind LOL)

Page 1 of 2

Powered by WordPress & Theme by Anders Norén