Sunday, November 21, 2010

grep grep grep

Yeah, we all use google to "grep" for information. Well, recently I came to know about one option of grep which I never knew before.

grep -C[some number]

Check this out

Code:
root@bt> cat file
Apple
Bat
Cat
Dog
Elephant
Fan
Goat
Hat
Ink
Joker
King

root@bt> grep -n Fan file
6:Fan

root@bt> grep -n -C1 Fan file
5-Elephant
6:Fan
7-Goat

root@bt> grep -n -C2 Fan file
4-Dog
5-Elephant
6:Fan
7-Goat
8-Hat


As you can see,
-C1 will give you the one line before and after the match.
-C2 will give you two line before and after the match and so on.
-n option with grep will print the line number.
The -C[number] option will be handy if you want to see something before or after the line you are looking for.


Good Day!

Tuesday, November 16, 2010

"which" command wasn't working in Linux

That was weird, or is that how it works?

I had this executable "db_linux" in "/usr/local/bin" and this was included in the env variable PATH. But there was no execute permission for this executable.And when I did a "which db_linux", it didn't show me anything. I was checking the file, the path everything was proper. The I thought fine lets execute it with absolute path. In the process I gave execute permission for the file and executed it. Later when I tried "which db_linux" again, it was showing. May be that is how it works.

BTW, I use Fedora Core 13 v 2.6.33.3

Code:
[root@ahamed bin]# ls -lrt
total 4
-rw-r--r--. 1 root root 63 Nov 16 16:33 db_linux

[root@ahamed bin]# which db_linux
/usr/bin/which: no db_linux in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:
/usr/loca/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

[root@ahamed bin]# chmod 744 db_linux
[root@ahamed bin]# ls -lrt
total 4
-rwxr--r--. 1 root root 63 Nov 16 16:33 db_linux

[root@ahamed bin]# which db_linux
/usr/local/bin/db_linux

Have a good day!

Monday, November 15, 2010

sed -i option

We can use the -i option in sed to edit the file within the same command rather than directing it to another file and then renaming/moving it...

Code:
[ahamed@ahamed ~]$ cat textfile
# Check this line
The quick brown ajith jumps over the lazy dog

[ahamed@ahamed ~]$ sed -i '2 i \# Wow! I am the new guy' textfile

[ahamed@ahamed ~]$ cat textfile
# Check this line
# Wow! I am the new guy
The quick brown ajith jumps over the lazy dog

As you can see, after the -i option, the number is the line number to be inserted. The rest is the usual sed syntax. I'll break it up

sed - The command
i - For insertion
2 - Line Number
\#... - The line to be inserted
textfile - file being edited

If you don't use the option "-i", the file will not be edited. The changes will be displayed on the standard display.
Check this...

Code:
[ahamed@ahamed ~]$ cat textfile
# Check this line
# Wow! I am the new guy
The quick brown ajith jumps over the lazy dog

[ahamed@ahamed ~]$ sed '2 i # Wow! I am the NEXT new guy' textfile
# Check this line
# Wow! I am the NEXT new guy
# Wow! I am the new guy
The quick brown ajith jumps over the lazy dog

[ahamed@ahamed ~]$ cat textfile
# Check this line
# Wow! I am the new guy
The quick brown ajith jumps over the lazy dog

Good Day...