sed command is used to search and replace the word.
eg:
sed "s/old/new/g" filename
This command find and replace the word old with new globally.
Search and Replace specific lines only.
sed '1,3s/a/A/g' filename
Above command replace the 'a' with 'A' in first three lines in the file.
Input a square brackets.
sed 's/[A-Z]/[&]/g' filename
To print '*' at the beginning of files those contain capital letters
sed '/[A-Z]/s/^/*/' filename
Saturday, August 30, 2008
Search and Replace in vi editor
Replace the word at the beginning of the sentence. Press ecs in vi editor and type
:%s/^step/more/g
This command replace the sentence starting with 'step' with the word 'more'.
'g' option is used to replace everything (globle search). Other wise it replaces the first sentence.
:%s/end$/first/g
This command replaces the word of end at the end of sentence with first.
Serch by word
:%s/\
if you want to replace " character with someother word or charecter use
:%s/\"/word/g or :%s/["]/word/g
:%s/^step/more/g
This command replace the sentence starting with 'step' with the word 'more'.
'g' option is used to replace everything (globle search). Other wise it replaces the first sentence.
:%s/end$/first/g
This command replaces the word of end at the end of sentence with first.
Serch by word
:%s/\
/new/g
if you want to replace " character with someother word or charecter use
:%s/\"/word/g or :%s/["]/word/g
Thursday, August 28, 2008
xargs
xargs command take the output from one command and send certain number of lines to another command.
Lets look at the following example
ls > list.txt
cat list.txt | xargs -i file {}
file command use to get the type of the file.
xargs -i command takes one whole line as a one input and ingore the space infront of the lines.
xargs -t command writes each generated command to the standard error.
xargs -l command execute for each nonempty line. Line is considered to be end with the first new line character.
Lets look at the following example
ls > list.txt
cat list.txt | xargs -i file {}
file command use to get the type of the file.
xargs -i command takes one whole line as a one input and ingore the space infront of the lines.
xargs -t command writes each generated command to the standard error.
xargs -l command execute for each nonempty line. Line is considered to be end with the first new line character.
Wednesday, August 27, 2008
grep
grep command is used to search patterns in files.
eg: when we want to find lines with the word 'apple' we can use
grep apple filename
This can also do using
cat filename | grep apple
grep -l gives the file name that has the required pattern. As example if we want to find the name of text file that contains apple,
grep -l apple *.txt
If we use
cat filename | grep -l apple
command gives "stdin", because pipe doesn't know anything about where input is coming from.
grep -i makes grep command case insensitive.
grep -w command only give the matching of whole word.
grep -c gives the number of lines those matches with required pattern.
If we want to do invert of particular command, we can use
grep -v apple filename
Two grep commands can make logical and operator.
eg:
grep apple filename | grep banana filename
this command matches both apple and banana
If we want to find the sentences start with "My"
grep ^My filename
The command
grep me$ filename gives the sentences end with me in the file.
Make Logical OR operator
egrep operator is used to expand the capabilities of grep operator. egrep operator can work with logical OR using '|'.
eg:
egrep 'apple | banana' filename
eg: when we want to find lines with the word 'apple' we can use
grep apple filename
This can also do using
cat filename | grep apple
grep -l gives the file name that has the required pattern. As example if we want to find the name of text file that contains apple,
grep -l apple *.txt
If we use
cat filename | grep -l apple
command gives "stdin", because pipe doesn't know anything about where input is coming from.
grep -i makes grep command case insensitive.
grep -w command only give the matching of whole word.
grep -c gives the number of lines those matches with required pattern.
If we want to do invert of particular command, we can use
grep -v apple filename
Two grep commands can make logical and operator.
eg:
grep apple filename | grep banana filename
this command matches both apple and banana
If we want to find the sentences start with "My"
grep ^My filename
The command
grep me$ filename gives the sentences end with me in the file.
Make Logical OR operator
egrep operator is used to expand the capabilities of grep operator. egrep operator can work with logical OR using '|'.
eg:
egrep 'apple | banana' filename
echo $?
echo $? have the return value of the command. Therefore this is used to check that the command properly worked or not. If command properly worked this returns 0.
#!/bin/bash
cat $1 >/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
echo "Cat worked"
else
echo "File Not Found"
fi
The above program use the command line argument as a file name.
* In unix /dev/null folder is always empty. If we want to delete something we can move that in to /dev/null folder
eg:
ls -l /etc >/dev/null 2> /dev/null
This command doesn't output anything to the screen.
The purpose of this is to make sure that the command is properly working. For that we have to type echo $? immediately after executed that command.
#!/bin/bash
cat $1 >/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
echo "Cat worked"
else
echo "File Not Found"
fi
The above program use the command line argument as a file name.
* In unix /dev/null folder is always empty. If we want to delete something we can move that in to /dev/null folder
eg:
ls -l /etc >/dev/null 2> /dev/null
This command doesn't output anything to the screen.
The purpose of this is to make sure that the command is properly working. For that we have to type echo $? immediately after executed that command.
Monday, August 25, 2008
Redirect Vs Pipe
Redirect use to put the output of a command into to a file or take input from a file.
But in pipe output of first command further process in second command.
But in pipe output of first command further process in second command.
Subscribe to:
Posts (Atom)