Unix Basics Part 11 | sed |Frequently used commands | searching, find & replace

2024 ж. 10 Мам.
55 Рет қаралды

SED - Stands for stream editor.
It perform lots of functions on a file like searching, find & replace, insertion or deletion.
We can edit files even without opening them.
sed OPTIONS... [SCRIPT] [INPUTFILE...]
1) Replacing or substituting string.
$sed 's/yellow/green/' test_sed.dat
“s” specifies the substitution operation.
By default, the sed command replaces the first occurrence of the pattern in each line.
2) Replacing the nth occurrence.
$sed 's/yellow/green/2' test_sed.dat
3) Replacing all the occurrence.
$sed 's/yellow/green/g' test_sed.dat
4) Replacing from nth occurrence to all occurrences.
$sed 's/yellow/green/2g' test_sed.dat
5) Replacing string on a specific line number.
$sed '4 s/yellow/green/' test_sed.dat
6) Printing only the replaced lines.
$sed -n 's/yellow/green/p' test_sed.dat
7) Replacing string on a range of lines.
$sed '1,3 s/yellow/green/' test_sed.dat
$sed '2,$ s/yellow/green/' test_sed.dat
$ last of the line
8) Deleting lines from a particular file.
Delete 2nd line
$ sed '2d' test_sed.dat
To Delete a last line
$sed '$d' test_sed.dat
To Delete line from range x to y
$ sed '2,4d' test_sed.dat
To Delete pattern matching line
$sed '/gre/d' test_sed.dat
9) Inserting blank lines.
Insert one blank line after each line -
Inserting blank lines
Insert one blank line after each line -
$sed G test_sed.dat
To insert two blank lines -
$sed 'G;G' test_sed.dat
10) Regular Expression with SED.
1 ^ Matches the beginning of lines
2 $ Matches the end of lines
3 . Matches any single character
4 * Matches zero or more occurrences of the previous character
5 [chars] Matches any one of the characters given in chars, where chars is a sequence of characters. You can use the - character to indicate a range of characters.
$sed G test_sed.dat
To insert two blank lines -
$sed 'G;G' test_sed.dat
Regular Expression with SED
1 ^ Matches the beginning of lines
2 $ Matches the end of lines
3 . Matches any single character
4 * Matches zero or more occurrences of the previous character
5 [chars] Matches any one of the characters given in chars, where chars is a sequence of characters. You can use the - character to indicate a range of characters.
/g.e/ Matches lines that contain strings such as g+e, g-e, gre, and g3e etc.
/g*n/ Matches the same strings along with strings such as german, green, gre1n etc…
/[tT]he/ Matches the string The and the
/^$/ Matches blank lines
/^.*$/ Matches an entire line whatever it is
/ */ Matches one or more spaces
/^$/ Matches blank lines
[a-z] Matches a single lowercase letter
[A-Z] Matches a single uppercase letter
[a-zA-Z] Matches a single letter
[0-9] Matches a single number
[a-zA-Z0-9] Matches a single letter or number
[[:alnum:]] Alphanumeric [a-z A-Z 0-9]
[[:alpha:]] Alphabetic [a-z A-Z]
[[:blank:]] Blank characters (spaces or tabs)
[[:cntrl:]] Control characters
[[:digit:]] Numbers [0-9]
sed -n '/g.e/p' test_sed.dat
sed -n '/[A-Z]/p' test_sed.dat
sed -n '/[a-zA-Z0-9]/p' test_sed.dat
sed -n '/[0-9]/p' test_sed.dat
sed -n '/[[:digit:]]/p' test_sed.dat
11) Options.
The '-e' option allows us to execute the multiple sed commands at once
1. sed -e 's/red/green/; s/yellow/green/' test_sed.dat
write the command in the file ending with ; and then use it in below command
1. sed -f sed_command.dat test_sed.dat
Sample file used for testing: test_sed.dat
This is a test file.
We need to eat healthy fruits daily. Apples can be yellow or green.yellow yellow
Traffic light is turning yellow, you need to slow down in yellow.yellow yellow
You need to give a fine, if you are not slowing down in yellow and stopping in red.
Using RGB - red, green and blue any color can be formed. Color12345
Thank you!
#unix #unixtutorial #unixcommands #linux #linuxcommands #learning #coding #sed #streameditor
#unixbasics #reporting #unix #unixtutorial #reporting #unix #unixtutorial #sed#awkcommand #text #learning #coding #programming

KZhead