Sed Command in Unix and Linux Examples
Sed Command in Unix and Linux Examples
Sed is a Stream Editor used for modifying the files in unix (or linux). Whenever you want to
make changes to the file automatically, sed comes in handy to do this. Most people never learn
its power; they just simply use sed to replace text. You can do many things apart from replacing
text with sed. Here I will describe the features of sed with examples.
Consider the below text file as an input.
>cat file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.
Here the "s" specifies the substitution operation. The "/" are delimiters. The "unix" is the search
pattern and the "linux" is the replacement string.
By default, the sed command replaces the first occurrence of the pattern in each line and it won't
replace the second, third...occurrence in the line.
2. Replacing the nth occurrence of a pattern in a line.
Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below
command replaces the second occurrence of the word "unix" with "linux" in a line.
>sed 's/unix/linux/2' file.txt
unix is great os. linux is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.
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
and so on. The \1,\2 can be used in the replacement string to make changes to the source string.
As an example, if you want to replace the word "unix" in a line with twice as the word like
"unixunix" use the sed command as below.
>sed 's/\(unix\)/\1\1/' file.txt
unixunix is great os. unix is opensource. unix is free os.
learn operating system.
unixunixlinux which one you choose.
The parenthesis needs to be escaped with the backslash character. Another example is if you
want to switch the words "unixlinux" as "linuxunix", the sed command is
>sed 's/\(unix\)\(linux\)/\2\1/' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
linuxunix which one you choose.
If you use -n alone without /p, then the sed does not print anything.
10. Running multiple sed commands.
You can run multiple sed commands by piping the output of one sed command as input to
another sed command.
>sed 's/unix/linux/' file.txt| sed 's/os/system/'
linux is great system. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you chosysteme.
Sed provides -e option to run multiple sed commands in a single sed command. The above output
can be achieved in a single sed command as shown below.
>sed -e 's/unix/linux/' -e 's/os/system/' file.txt
linux is great system. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you chosysteme.
The above sed command replaces the string only on the third line.
12. Replacing string on a range of lines.
You can specify a range of line numbers to the sed command for replacing a string.
>sed '1,3 s/unix/linux/' file.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
Here the sed command replaces the lines with range from 1 to 3. Another example is
>sed '2,$ s/unix/linux/' file.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
Here $ indicates the last line in the file. So the sed command replaces the text from second line to
last line in the file.
13. Replace on a lines which matches a pattern.
You can specify a pattern to the sed command to match in a line. If the pattern match occurs,
then only the sed command looks for the string to be replaced and if it finds, then the sed
command replaces the string.
Here the sed command first looks for the lines which has the pattern "linux" and then replaces
the word "unix" with "centos".
14. Deleting lines.
You can delete the lines a file by specifying the line number or a range or numbers.
>sed '2 d' file.txt
>sed '5,$ d' file.txt
Here the sed command looks for the pattern "unix" in each line of a file and prints those lines
that has the pattern.
You can also make the sed command to work as grep -v, just by using the reversing the sed with
NOT (!).
>grep -v 'unix' file.txt
>sed -n '/unix/ !p' file.txt
Here the sed command transforms the alphabets "ul" into their uppercase format "UL"
This displays the last executed grep command and also prints the result set of the command on
the terminal.
2. Search for a string in a file
This is the basic usage of grep command. It searches for the given string in the specified file.
grep "Error" logfile.txt
This searches for the string "Error" in the log file and prints all the lines that has the word
"Error".
3. Searching for a string in multiple files.
grep "string" file1 file2
grep "string" file_pattern
This is also the basic usage of the grep command. You can manually specify the list of files you
want to search or you can specify a file pattern (use regular expressions) to search for.
4. Case insensitive search
The -i option enables to search for a string case insensitively in the give file. It matches the
words like "UNIX", "Unix", "unix".
grep -i "UNix" file.txt
This will search for the lines which starts with a number. Regular expressions is huge topic and I
am not covering it here. This example is just for providing the usage of regular expressions.
6. Checking for the whole words in a file.
By default, grep matches the given string/pattern even if it found as a substring in a file. The -w
option to grep makes it match only the whole words.
grep -w "world" file.txt
This will prints the matched lines along with the two lines before the matched lines.
8. Displaying the lines after the match.
grep -A 3 "Error" file.txt
This will display the matched lines along with the three lines after the matched lines.
9. Displaying the lines around the match
grep -C 5 "Error" file.txt
This will display the matched lines and also five lines before and after the matched lines.
10. Searching for a sting in all files recursively
You can search for a string in all the files under the current directory and sub-directories with the
help -r option.
grep -r "string" *
15. Display the file names that do not contain the pattern.
We can display the files which do not contain the matched string/pattern.
grep -l "string" file.txt
If you like this post, please share it on google by clicking on the +1 button.
This will execute the last find command. It also displays the last find command executed along
with the result on the terminal.
2. How to find for a file using name?
find -name "sum.java"
./bkp/sum.java
./sum.java
This will find all the files with name "sum.java" in the current directory and sub-directories.
3. How to find for files using name and ignoring case?
find -iname "sum.java"
./SUM.java
./bkp/sum.java
./sum.java
This will find all the files with name "sum.java" while ignoring the case in the current directory
and sub-directories.
4. How to find for a file in the current directory only?
find -maxdepth 1 -name "sum.java"
./sum.java
This will find for the file "sum.java" in the current directory only
5. How to find for files containing a specific word in its name?
find -name "*java*"
./SUM.java
./bkp/sum.java
./sum.java
./multiply.java
It displayed all the files which have the word "java" in the filename
6. How to find for files in a specific directory?
find /etc -name "*java*"
This will look for the files in the /etc directory with "java" in the filename
7. How to find the files whose name are not "sum.java"?
find -not -name "sum.java"
.
./SUM.java
./bkp
./multiply.java
This is like inverting the match. It prints all the files except the given file "sum.java".
8. How to limit the file searches to specific directories?
find -name "sum.java"
./tmp/sum.java
./bkp/var/tmp/files/sum.java
./bkp/var/tmp/sum.java
./bkp/var/sum.java
./bkp/sum.java
./sum.java
You can see here the find command displayed all the files with name "sum.java" in the current
directory and sub-directories.
a. How to print the files in the current directory and one level down to the current directory?
find -maxdepth 2 -name "sum.java"
./tmp/sum.java
./bkp/sum.java
./sum.java
b. How to print the files in the current directory and two levels down to the current directory?
find -maxdepth 3 -name "sum.java"
./tmp/sum.java
./bkp/var/sum.java
./bkp/sum.java
./sum.java
10. How to find the largest file in the current directory and sub directories
find . -type f -exec ls -s {} \; | sort -n -r | head -1
The find command "find . -type f -exec ls -s {} \;" will list all the files along with the size of the
file. Then the sort command will sort the files based on the size. The head command will pick
only the first line from the output of sort.
11. How to find the smallest file in the current directory and sub directories
find . -type f -exec ls -s {} \; | sort -n -r | tail -1
| head -1
b. Finding directories
find . -type d
14. How to find the files which are modified after the modification of a give file.
find -newer "sum.java"
This will display all the files which are modified after the file "sum.java"
15. Display the files which are accessed after the modification of a give file.
find -anewer "sum.java"
16. Display the files which are changed after the modification of a give file.
This will display the files which have read, write, and execute permissions. To know the
permissions of files and directories use the command "ls -l".
18. Find the files which are modified within 30 minutes.
find . -mmin -30
20. How to find the files which are modified 30 minutes back
find . -not -mmin -30
21. How to find the files which are modified 1 day back.
find . -not -mtime -1
26. How to find the files which are created between two files.
find . -cnewer f1 -and ! -cnewer f2
So far we have just find the files and displayed on the terminal. Now we will see how to perform
some operations on the files.
1. How to find the permissions of the files which contain the name "java"?
Alternate method is
find -name "*java*" -exec ls -l {} \;
2. Find the files which have the name "java" in it and then display only the files which have
"class" word in them?
find -name "*java*" -exec grep -H class {} \;
This will delete all the files which have the word java" in the file name in the current directory
and sub-directories.
Similarly you can apply other Unix commands on the files found using the find command. I will
add more examples as and when i found.
Now we will see how to remove the lines from the above file in unix / linux
1. Remove lines using unix sed command
The d command in sed can be used to delete the empty lines in a file.
sed '/^$/d' file.txt
Here the ^ specifies the start of the line and $ specifies the end of the line. You can redirect the
output of above command and write it into a new file.
sed '/^$/d' file.txt > no_empty_lines.txt
Now we will use the -v option to the grep command to reverse the pattern matching
grep -v '^$' file.txt
The output of both sed and grep commands after deleting the empty lines from the file is
Remove line using unix grep command
Delete lines using unix sed command
How it works