SED also knowns as the Stream Editor’ when you want to search somethings in files particulary with regex, you can use sed like grep or awk.I gave some example about the this stream editor for, that want to using regex with sed.

sed -n 354p /var/log/messages
Jun 12 03:42:18 rhel6 kernel: pci 0000:00:15.7: BAR 13: can’t allocate I/O resource
[0x1000-0x1fff]

Prints the first line of the file

sed -n ‘$p’ /var/log/messages
Jun 13 21:37:20 rhel6 pulseaudio[2836]: alsa-sink.c: We were woken up with POLLUT set —
however a subsequent snd_pcm_avail() returned 0 or another value < mi_avail.
Prints the last line.

sed -n 4,390p /var/log/messages
Prints lines ’4′ with ’390′.

sed -n ’1!p’ ugur.txt
Prints all line but line ’1′.

sed -n ’1,3!p’ ugur.txt
Prints all line but lines ’1′ and ’3′.

sed -n -e ‘/kernel/p’ /var/log/messages
Prints lines containing ‘kernel’.

sed -n -e ‘/^kernel/p’ /var/log/messages
Prints lines beginning ‘kernel’.

sed -n -e ‘/kernel$/p’ /var/log/messages
Prints lines ending with ‘kernel’.

sed -n -e ‘/^linux$/p’ ugur.txt
Prints lines beginning and ending with ‘linux’.

sed -n -e ‘/[1-3]/p’ ugur.txt
LINUX  1
ugur 2
squid 3
architecture 10

sed -n -e ‘/^[1-3][1-3][1-3][1-3]$/p’ ugur.txt
Prints lines containing 4 adjacent numbers, also these numbers must be juxtaposed.

sed -n -e ‘/^[3-6]\{8\}$/p’ ugur.txt
Prints lines containing 4 numbers which begin and end the line.

sed -n -e ‘/^lin/,/ugur/p’ ugur.txt
Range of lines from these strings: ‘lin’ to ‘ugur’.

Share on Facebook

Leave a reply

required

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>