Skip to main content

Linux Commands-3


// Invert Search in grep

$ cat < xyz
asd
anuj
anuj@anuj-G31M-ES2L:~$ grep -v "asd" xyz
anuj

//Some more results on grep

$ grep "a.." xyz
asd
anuj

anuj@anuj-G31M-ES2L:~$ grep "a..$" xyz
asd

anuj@anuj-G31M-ES2L:~$ grep "a*$" xyz
asd
anuj

// Use of brackets in grep command

grep [a-z]n xyz
anuj

$ grep [a-z][sn] xyz
asd
anuj

// To show the line number of lines that match the string or pattern.

$ grep -n [a-z]n xyz
2:anuj

//Count no.of lines that match the pattern
$ grep -c [a-z] xyz
2

//Display file names matching the pattern

$ grep -l [a] xy*
xy2
xy2_file
xyz
xyz2

//Match both uppercase and lowercase letters
$ grep -i [a-z] xy*
xy2:as
xy2_file:i want to append this to xy_file
xyz:asd
xyz:anuj
xyz2:asd
xyz2:anuj
xyz2:hello
xyz2:i am your teacher
$ grep -ic [a-z] xy*
xy2:1
xy2_file:1
xy3:0
xy_file:0
xyz:2
xyz2:4
xyzz:0

sed
stream editor for filtering and transforming text

Syntax: sed “s/original_word/replace_word” file_name


//Example 1: replacing characters

$ cat>abc
this is my file.
My mobile has idea's sim.

$ sed "s/my/your/" abc
this is your file.
My mobile has idea's sim.



Tr
translate or delete character.

$ cat>abc
this is my file.
My mobile has idea's sim.

$ tr "[a-j]" "[A-J]" <abc
tHIs Is my FIlE.
My moBIlE HAs IDEA's sIm.

$ tr "[a-j]" "[0-9]" <abc
t78s 8s my 58l4.
My mo18l4 70s 8340's s8m.

$ cat> abc
aaa
aa
a
w aa
anuj@anuj-G31M-ES2L:~$ tr -s "a" < abc
a
a
a
w a



Cut

Example:

$ cat > name_list
anuj 12 qwe 3
vibh 10 tis 2
mano 23 bdh 1

$ cut -c 2 name_list
n
i
a

Example:

$ cut -c 2- name_list
nuj 12 qwe 3
ibh 10 tis 2
ano 23 bdh 1

//Cutting out fields fields separated by space

$ cat> a
hasdas 87
asdfasf 787
saf 54

$ cut -f2 a
87
787
54

//Cutting out fields separated by comma
$ cat>a
a,213,123
r,324,12
qasdasd,34234,123213

$ cut -d "," -f 2 a
213
324
34234

//cut a file and send it into 3 different files
$ cut -d "," -f1 a >abc
anuj@anuj-G31M-ES2L:~$ cut -d "," -f2 a >abc2
anuj@anuj-G31M-ES2L:~$ cut -d "," -f3 a >abc3

$ cat abc abc2 abc3
a
r
qasdasd
213
324
34234
123
12
123213

Comments

Popular posts from this blog

unix commands