Monday, 6 August 2018

Linux: sed - Command



1) Replace a string from content

sed 's/India/Bharat' test_file.txt

Output:-

Bharat is great country. India has a great PM.
Mr. Narendra Modi is The Great Prime Minister.
IndiaBharat Everyone should support him.

## Here the "s" specifies the substitution operation.
The "/" are delimiters. The "India" is the search pattern and the "Bharat" is the replacement string.
And Sed command will only replace the first occurance of the string not for second, third or further till Nth occurance.

2) Replace Nth or Global occurance of the string in a file

sed 's/India/Bharat/2' test_file.txt (#This will replace the 2nd occurance of this string "India". Same as this you can mention any line number)

Output:-

India is great country. Bharat has a great PM.
Mr. Narendra Modi is The Great Prime Minister.
IndiaBharat Everyone should support him.


Global Replacement :-

sed 's/India/Bharat/g' test_file.txt

Output:-

Bharat is great country. Bharat has a great PM.
Mr. Narendra Modi is The Great Prime Minister.
BharatBharat Everyone should support him.

And if you want to start from any specific occurance of the string then you can use below mentioned syntax with "g" global option

sed 's/India/Bharat/3g' test_file.txt (## It will replace all the strings starts from 3rd occurance and will not change the first and second occurance of the string)

3) Using & character

Whenever we want to add some special characters to some string then we use "&" in sed command.

sed 's/India/{&}/' test_file.txt

Output:-

{India} is great country. Bharat has a great PM.
Mr. Narendra Modi is The Great Prime Minister.
{India}Bharat Everyone should support him.

4) Change web url to another url

sed 's/http:\/\//www/' test_file.txt

In this case the url consists the delimiter character which we used. In that case you have to escape the slash with backslash character, otherwise the substitution won't work.

Using too many backslashes makes the sed command look awkward. In this case we can change the delimiter to another character as shown in the below example.

sed 's_http://_www_' file.txt
sed 's|http://|www|' file.txt

5) Another example is switching the first three characters in a line

sed 's/^\(.\)\(.\)\(.\)/\3\2\1/' test_file.txt

Output:-

ndia is great country. Bharat has a great PM.
r. Narendra Modi is The Great Prime Minister.
ndiaBharat Everyone should support him.

6) Running multiple sed commands

sed 's/India/Bharat/' test_file.txt| sed 's/Prime/Indian/'

OR

sed -e 's/India/Bharat/' -e 's/Prime/Indian/' test_file.txt

7) Deleting lines using sed

sed '2 d' test_file.txt (Will delete the second line- particular line number)
sed '5,$ d' test_file.txt (Will delete the lines from 5th to end- range of lines)

8) Sed command to search a string

sed -n '/India/ p' test_file.txt - It will search for the string "India"

9) Ignore specific content while searching

sed -n '/India/ !p' test_file.txt - It will search the full content except the string "India".

10) Replace on the line where the particular string matches

sed '/India/ s/PM/People/' file.txt - It will search for "India" and in the matching line it will replace the string "PM" to "People"

That's it.

Friday, 3 August 2018

Linux: sed - Replace / Substitution command

Command to replace the keyword unix with linux in sample.txt file.
sed 's/unix/linux/' sample.txt 

Output: Sample file for learning SED command in linux. As we know unix is an free os.
Explanation of above SED command:
‘s’ is used to replace keyword where the above keyword is used to replace the unix keyword in the first occurrence only.
  
 Command to replace all the occurrence of unix keyword in sample.txt file.


sed 's/unix/linux/g' sample.txt 

Output: Sample file for learning SED command in linux. As we know linux is an free os

Explanation of above SED command:
‘g’ is used to replace keyword globally (i.e) it is used to replace all the occurrence of the keyword in a file.
            Command to replace 2nd occurrence of a string
 sed 's/linux/solaris/2' sample.txt

Output: Sample file for learning SED command in unix. As we know unix is an free os
linux is also free os and solaris is also free source.
Explanation of above SED command:
‘2’ is used to replace second in the second occurrence in a file.

Note: ‘2’ can be replace with ‘n’ to replace any occurrence. In our example I took ‘2’ for second second replacement.

Replace every occurrence of Nick with John in report.txt
sed 's/Nick/John/g' report.txt

Replace every occurrence of Nick or nick with John.
sed 's/Nick|nick/John/g' report.txt

Replace ham with cheese in file.txt except in the 5th line
sed '5!s/ham/cheese/' file.txt

Unless boom is found replace aaa with bb
sed '/boom/!s/aaa/bb/' file.txt

Replaces one with unos in a case-insensitive manner, so it will print "unos TWO"
echo ONE TWO | sed "s/one/unos/I"

Replace foo with bar only for the first instance in a line.
sed 's/foo/bar/' file.txt

Replace foo with bar only for the 4th instance in a line.
sed 's/foo/bar/4' file.txt

Replace foo with bar for all instances in a line.
sed 's/foo/bar/g' file.txt


Do replacement of Johnson with White only on lines between 1 and 20
sed '1,20 s/Johnson/White/g' file.txt

The above reversed (match all except lines 1-20)
sed '1,20 !s/Johnson/White/g' file.txt

Replace only between "from" and "until"
sed '/from/,/until/ { s/\<red\>/magenta/g; \ s/\<blue\>/cyan/g; }' file.txt

Replace only from the word "ENDNOTES:" until EOF
sed '/ENDNOTES:/,$ { s/Schaff/Herzog/g; \s/Kraft/Ebbing/g; }' file.txt





Linux: sed - Delete a line

As we discussed in the above section about the power of SED command, here we discuss about the how to use SED command to delete a line in a file.
How to delete first line using SED command
sed 'Nd' sample1.txt
Note: ‘N’should be replaced with the line number which we want to delete 
           ‘d’ is the command used to delete.
How to delete a particular line using SED command
sed '4d' sample1.txt

How delete last line in a file using SED command
sed '$d' sample1.txt
Note: ‘$ ‘indicates the last line of a file
In the next article will learn more about SED commands.

Delete all spaces in front of every line of file.txt
sed 's/^[ ^t]*//' file.txt


Delete all spaces at the end of every line of file.txt
sed 's/[ ^t]*$//' file.txt
Delete all spaces in front and at the end of every line of file.txt
sed 's/^[ ^t]*//;s/[ ^]*$//' file.txt

Delete all consecutive blank lines except for EOF
sed '/./,/^$/!d' file.txt
Delete all consecutive blank lines, but allows only top blank line
sed '/^$/N;/\n$/D' file.txt

Delete all leading blank lines
sed '/./,$!d' file.txt

Delete all trailing blank lines
sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' \file.txt

Delete every third line, starting with the first
sed '1~3d' file.txt

Delete only the first match
sed '0,/RE/{//d;}' file.txt
Delete lines matching pattern
sed '/pattern/d' file.txt

Delete all blank lines from a file
sed '/./!d' file.txt

Delete all consecutive blank lines except for the first two
sed '/^$/N;/\n$/N;//D' file.txt

Delete the last line of each paragraph
sed -n '/^$/{p;h;};/./{x;/./p;}'\ file.txt








Linux: sed - Delete a line

As we discussed in the above section about the power of SED command, here we discuss about the how to use SED command to delete a line in a file.
How to delete first line using SED command
sed 'Nd' sample1.txt
Note: ‘N’should be replaced with the line number which we want to delete 
           ‘d’ is the command used to delete.
 
How to delete a particular line using SED command
sed '4d' sample1.txt
How delete last line in a file using SED command
sed '$d' sample1.txt
Note: ‘$ ‘indicates the last line of a file
In the next article will learn more about SED commands.

Linux: sed - Matching regular expressions

Linux: sed - Setting input and output files

By default, sed will operate on standard input. However, that’s not too useful. To run sed on a file, specify the filename at the end of the command, like so:

This will run sed on “oldfile.txt” and save the command’s output to “newfile.txt.” If “newfile.txt” does not exist, it will be created by the command. If you don’t specify an output file, sed will echo the file’s new text into standard input.
If you want to overwrite the contents of a file, you’ll need to use the -i flag. This flag makes the edits “in place.”
This command overwrites the contents of the target file immediately, which is slightly dangerous. To stay on the safe side, you can create a backup in-situ.
To create a backup of the file, put an extension after the -i. It doesn’t need to be a functional extension: “.bu” or “.bak” work fine. This will create a backup file with an extension.
This creates “oldfile.txt.bak,” which contains the unedited text data from the original version of “oldfile.txt.”

A Quick Guide to Using Sed Commands in Linux

The Linux operating system makes use of terminal commands to work with a computer's file system. The primary use of the Linux command sed, which is short for stream editor, is to modify each line of a file or stream by replacing specified parts of the line. It makes basic text changes to a file or input from a pipeline. For example, say you have a file named "songs.text" that contains these lines:

1, Justin Timberlake, Title 545, Price $6.30
2, Taylor Swift, Title 723, Price $7.90
3, Mick Jagger, Title 610, Price $7.90
4, Lady Gaga, Title 118, Price $6.30
5, Johnny Cash, Title 482, Price $6.50
6, Elvis Presley, Title 335, Price $6.30
7, John Lennon, Title 271, Price $7.90

Making Text Substitutions With Sed
If you want to change all price occurrences of $6.30 to $7.30, you can make the changes using the sed command in this way:
sed 's/6.30/7.30/' songs.txt > songs2.txt
This code makes the change and writes the modified file to "songs2.txt". The output file contains:
1, Justin Timberlake, Title 545, Price $7.30
2, Taylor Swift, Title 723, Price $7.90
3, Mick Jagger, Title 610, Price $7.90
4, Lady Gaga, Title 118, Price $7.30
5, Johnny Cash, Title 482, Price $6.50
6, Elvis Presley, Title 335, Price $7.30
7, John Lennon, Title 271, Price $7.90
If you want to replace all occurrences of "Cash" with "Trash" you use:
sed 's/Cash/Trash/' songs.txt > songs2.txt
which creates a file with content:
1, Justin Timberlake, Title 545, Price $7:30
2, Taylor Swift, Title 723, Price $7.90
3, Mick Jagger, Title 610, Price $7.90
4, Lady Gaga, Title 118, Price $7:30
5, Johnny Trash, Title 482, Price $6.50
6, Elvis Presley, Title 335, Price $7:30
7, John Lennon, Title 271, Price $7.90

Filtering With the Sed Command

Sed is also frequently used to filter lines in a file or stream. For example, if you only want to see the lines containing "John," you use:
sed -n '/John/p' songs.txt > johns.txt
which writes the following lines to file johns.txt:

5, Johnny Trash, Title 482, Price $6.50
7, John Lennon, Title 271, Price $7.90