Sed command (Stream Editor)
We are giving expression inside single quote using sed command. I am using this example for all the use cases.
Eg: ubuntu@ip-172-31-25-118:~$ cat data
ID Name Salary Country
1 Pol 25000 India
2 Bont 45000 Belgium
3 Loki 55000 Germany
4 Hina 35000 India
Use Cases:-
- I want to change the name of first user Pol to Paul
Eg: ubuntu@ip-172-31-25-118:~$ sed 's/Pol/Paul/g' data
Output:
ID Name Salary Country
1 Paul 25000 India
2 Bont 45000 Belgium
3 Loki 55000 Germany
4 Hina 35000 India
Here is the breakdown of a command
s: substitute
Pol: Which pattern you are searching for
Paul: This is your replacement string
g: global, means it will replace all instances of ‘Pol’ in each line.
- I want to see the 2nd line from data file
Eg: ubuntu@ip-172-31-25-118:~$ sed -n '2p' data
Output: 1 Pol 25000 India
Here is the breakdown of a command
-n: It will print the data from 2nd line
2: second
p: printing
- If I remove -n then print the 2nd data after the second line of your data.
Eg: ubuntu@ip-172-31-25-118:~$ sed '2p' data
Output:
ID Name Salary Country
1 Pol 25000 India
1 Pol 25000 India
2 Bont 45000 Belgium
3 Loki 55000 Germany
4 Hina 35000 India
- I want to see the last line
Eg: ubuntu@ip-172-31-25-118:~$ sed -n '$p' data
Output: 4 Hina 35000 India
- I want to see the data from 2nd line to 4th line
Eg: ubuntu@ip-172-31-25-118:~$ sed -n '2,4p' data
Output:
1 Pol 25000 India
2 Bont 45000 Belgium
3 Loki 55000 Germany
- I want to print the users which are from India
a) Using awk command
Eg: ubuntu@ip-172-31-25-118:~$ awk '/India/' data
Output:
1 Pol 25000 India
4 Hina 35000 India
b) Using sed command
Eg: ubuntu@ip-172-31-25-118:~$ sed -n '/India/p' data
Output:
1 Pol 25000 India
4 Hina 35000 India
- Now we will using multiple expressions using -e flag. For example, if we want to see only 2nd and 4th line.
Eg: ubuntu@ip-172-31-25-118:~$ sed -n -e '2p' -e '4p' data
Output:
1 Pol 25000 India
3 Loki 55000 Germany
- If you want to see the users from India and Germany
Eg: ubuntu@ip-172-31-25-118:~$ sed -n -e '/India/p' -e '/Germany/p' data
Output:
1 Pol 25000 India
3 Loki 55000 Germany
4 Hina 35000 India
- Staring from 2nd line, I want to print next 2 lines
Eg ubuntu@ip-172-31-25-118:~$ sed -n '2,+2p' data
Output:
1 Pol 25000 India
2 Bont 45000 Belgium
3 Loki 55000 Germany
- I want to print even lines of data
Eg ubuntu@ip-172-31-25-118:~$ sed -n '1~2p' data
Output:
ID Name Salary Country
2 Bont 45000 Belgium
4 Hina 35000 India
- Now I am writing an expression on another file. Using sed command I want to execute the same expression and print the output.
This is my file
ubuntu@ip-172-31-25-118:~$ cat data1.txt
1p
2p
Eg: ubuntu@ip-172-31-25-118:~$ sed -n -f data1.txt data
Output:
ID Name Salary Country
1 Pol 25000 India
- Now there is a word ‘India’ on 2nd line and 5th line. I want to change from India to US on 2nd line only.
ubuntu@ip-172-31-25-118:~$ cat data ID Name Salary Country
1 Pol 25000 India
2 Bont 45000 Belgium
3 Loki 55000 Germany
4 Hina 35000 India
Alternative way:
You can replace the word by searching Pol.
Eg: ubuntu@ip-172-31-25-118:~$ sed '/Pol/ s/India/US/g' data
Output:
ID Name Salary Country
1 Pol 25000 US
2 Bont 45000 Belgium
3 Loki 55000 Germany
4 Hina 35000 US
Eg: ubuntu@ip-172-31-25-118:~$ sed '2 s/India/US/g' data
ID Name Salary Country
Output:
1 Pol 25000 US
2 Bont 45000 Belgium
3 Loki 55000 Germany
4 Hina 35000 India
- Wherever there is a word ‘India’, please change the word (except 2nd line)
Eg: ubuntu@ip-172-31-25-118:~$ sed '2! s/India/US/g'
Output:
data ID Name Salary Country
1 Pol 25000 India
2 Bont 45000 Belgium
3 Loki 55000 Germany
4 Hina 35000 US
- Till now we are performing the operations on terminal itself, not inside the actual file. I want to change inside my actual file. You need to use ‘i’ flag
Eg: ubuntu@ip-172-31-25-118:~$ sed -i '2! s/India/US/g' data
ubuntu@ip-172-31-25-118:~$ cat data ID Name Salary Country
1 Pol 25000 India
2 Bont 45000 Belgium
3 Loki 55000 Germany
4 Hina 35000 US
- I want to delete 1st line
Eg: ubuntu@ip-172-31-25-118:~$ sed '1d' data
Output:
1 Pol 25000 India
2 Bont 45000 Belgium
3 Loki 55000 Germany
4 Hina 35000 US
- Delete the last line of data
ubuntu@ip-172-31-25-118:~$ sed '$d' data
Output:
ID Name Salary Country
1 Pol 25000 India
2 Bont 45000 Belgium
3 Loki 55000 Germany
- Delete the data from 2nd line to 4th line
ubuntu@ip-172-31-25-118:~$ sed '2,4d' data ID
Name Salary Country
4 Hina 35000 US
- Delete the user whose country is ‘Belgium’
ubuntu@ip-172-31-25-118:~$ sed '/Belgium/d' data
Output:
ID Name Salary Country
1 Pol 25000 India
3 Loki 55000 Germany
4 Hina 35000 US
- Delete the empty line. Here is my actual data
ID Name Salary Country
1 Pol 25000 India
2 Bont 45000 Belgium
3 Loki 55000 Germany
4 Hina 35000 US
Eg: ubuntu@ip-172-31-25-118:~$ sed '/^$/d' data
this sed
command will delete all blank lines (empty lines) from the file data
when the command is run
Output:
ID Name Salary Country
1 Pol 25000 India
2 Bont 45000 Belgium
3 Loki 55000 Germany
4 Hina 35000 US
Now this is my file
ubuntu@ip-172-31-25-118:~$ cat > Country
India
Germany
I want to list the country which is Germany
ubuntu@ip-172-31-25-118:~$ sed -n '/^G/p' Country
Germany
I want to list the country which ends with character ‘a’.
ubuntu@ip-172-31-25-118:~$ sed -n '/a$/p' Country
India
Character Sets:-
I want to list country which starts from ‘I’ or ‘G’
ubuntu@ip-172-31-25-118:~$ sed -n '/[IG]/p' Country
India Germany
I want to list the country having range from A to I
ubuntu@ip-172-31-25-118:~$ sed -n '/[A-I]/p' Country
India
Germany
Brietta
- I want to add the user who are from India in a new file called ‘IndianUsers’
Eg: ubuntu@ip-172-31-25-118:~$ sed '/India/ w IndianUsers' data
Output:
ubuntu@ip-172-31-25-118:~$ cat IndianUsers
1 Pol 25000 India
- I want to append the data after 3rd line
Eg: ubuntu@ip-172-31-25-118:~$ sed '3 a Hello User' data
Output:
ID Name Salary Country
1 Pol 25000 India
2 Bont 45000 Belgium
Hello User
3 Loki 55000 Germany
4 Hina 35000 US
- I want to edit the 2nd line to ‘Hello User’
ubuntu@ip-172-31-25-118:~$ sed '2 c Hello User' data
Output:
ID Name Salary Country
Hello User
2 Bont 45000 Belgium
3 Loki 55000 Germany
4 Hina 35000 US
- I want to append or insert data before 1st line
ubuntu@ip-172-31-25-118:~$ sed '1 i Hello User' data
Output:
Hello User
ID Name Salary Country
1 Pol 25000 India
2 Bont 45000 Belgium
3 Loki 55000 Ger
- I want to check hidden character in my file
ubuntu@ip-172-31-25-118:~$ sed -n 'l' data
Output:
ID Name Salary Country$
1 Pol 25000 India$
2 Bont 45000 Belgium$
3 Loki 55000 Germany$
4 Hina 35000 US$
- We can wrap the content. Like if you want to display only 10 characters in a line.
Eg: ubuntu@ip-172-31-25-118:~$ sed -n 'l 10' data
ID Name S alary Cou ntry$ 1 Pol 250 00 India$ 2 Bont 45 000 Belgi um$ 3 Loki 55 000 Germa ny$ 4 Hina 35 000 US$
Suppose I have created new file
ubuntu@ip-172-31-25-118:~$ cat ExternalFile
Hello User, this is a external file
- I want to insert this data after 3rd line in my original data.
ubuntu@ip-172-31-25-118:~$ sed '3 r ExternalFile' data
→ Here r stands for Read
ID Name Salary Country
1 Pol 25000 India
2 Bont 45000 Belgium
Hello User, this is a external file
3 Loki 55000 Germany
4 Hina 35000 US
- It prints the 1st line of those user whose country is India and after that it stops the execution.
ubuntu@ip-172-31-25-118:~$ sed '/India/ q' data
ID Name Salary Country
1 Pol 25000 India
- To add the date in 3rd line
ubuntu@ip-172-31-25-118:~$ sed '3 e date' data
ID Name Salary Country
1 Pol 25000 India
Sat Sep 14 14:26:53 UTC 2024
2 Bont 45000 Belgium
3 Loki 55000 Germany
4 Hina 35000 India
- To know on which line number you have inserted which data
ubuntu@ip-172-31-25-118:~$ sed '=' data
1
ID Name Salary Country
2
1 Pol 25000 India
3
2 Bont 45000 Belgium
4
3 Loki 55000 Germany
5
4 Hina 35000 India
- If you want to list out the files ending with .txt
ubuntu@ip-172-31-25-118:~$ ls -ltr *.txt
-rw------- 1 ubuntu ubuntu 66 Sep 9 07:53 testing.txt
-rw-rw-r-- 1 ubuntu ubuntu 37 Sep 9 14:23 tester.txt
-rw-rw-r-- 1 ubuntu ubuntu 6 Sep 13 14:11 data1.txt
- ls -ltr: list the files in long format where
-t: display the newest modified files
-r: sorts the files in ascending order