Stream editing using sed command in Linux

sed stands for stream editor. We can extract data from files or streams as well as edit the stream contents.

Here are some of the examples on sed command.

  1. sed ‘1d’ f1.txt – This command can be used to delete line number 1 from file f1.txt
  2. sed ‘/^a/d’ f1.txt -This command can be used to delete the lines starting with a
  3. sed -n ‘1,4p’ f1.txt – This command can be used to print lines 1,2,3 and 4
  4. sed -n ‘/abc/p’ f1.txt – This command can be used to print lines matching the pattern specified between / /
  5. sed ‘s/abc/xyz/gi f1.txt – This command can be used to substitute the occurrences of abc by xyz in file f1.txt. Here s stands for substitute, g stands for global (all occurrences) and i stands for case-insensitive replacement.
  6. sed ‘1s/abc/xyz/g’ f1.txt – This command is similar to above. The difference is that it will do case sensitive replacement in line number 1 only.

 

You may also like...