A regular expression (regex) is a string for describing a search pattern. Regular expression generally uses special characters called meta characters to represent something.
^ represents beginning of the line
$ represents end of the line
grep '^I' test3.txt
Here the lines starting with 'I ' letter from the file 'test3.txt' will be displayed in the screen
grep '6$' marks.txt
Here the lines ending with '6' from the file marks.txt will be displayed on the screen
To find the lines that starts with either letter 'a' or 'r' then use
grep -i '^[ar]' test.txt
To find the lines that doesn't starts with either letter 'a' or 'r' then use
grep -i '^[^[ar]]' test.txt
Also you can use '\<' and '\>' to search a particular word
Eg: If you want to replace the word 'in' with 'out' and if you use the following command
sed 's/in/out/gi' sample.txt
then this will replace all 'in' with 'out'. If the sample.txt has a word 'initial' it will be also replaced as 'outitial'. Here we wanted replace only 'in' word and so we have to alter the command
sed 's/\/out/gi' sample.txt
This will replace only the full words 'in' and not 'in' included in other words
^ represents beginning of the line
$ represents end of the line
grep '^I' test3.txt
Here the lines starting with 'I ' letter from the file 'test3.txt' will be displayed in the screen
grep '6$' marks.txt
Here the lines ending with '6' from the file marks.txt will be displayed on the screen
To find the lines that starts with either letter 'a' or 'r' then use
grep -i '^[ar]' test.txt
To find the lines that doesn't starts with either letter 'a' or 'r' then use
grep -i '^[^[ar]]' test.txt
Also you can use '\<' and '\>' to search a particular word
Eg: If you want to replace the word 'in' with 'out' and if you use the following command
sed 's/in/out/gi' sample.txt
then this will replace all 'in' with 'out'. If the sample.txt has a word 'initial' it will be also replaced as 'outitial'. Here we wanted replace only 'in' word and so we have to alter the command
sed 's/\
This will replace only the full words 'in' and not 'in' included in other words
No comments:
Post a Comment