• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Search

sed

Delete lines using sed

December 1, 2021

How to delete a line containing a pattern using sed.

Contents of test.txt.

$ cat test.txt
one
two
three

$ cat test.txt one two three

Delete a line matching the string “one.”

sed -i '/one/d' test.txt

sed -i '/one/d' test.txt

Display test.txt

$ cat test.txt
two
three

$ cat test.txt two three

Filed Under: Linux Tagged With: delete, line, matching, remove, sed

Modify a line using sed

May 11, 2021

Here’s how to modify a line in a file using sed.

sed -i 's/replace me.*/with this text/' /path/to/a/file.txt

sed -i 's/replace me.*/with this text/' /path/to/a/file.txt

Filed Under: Linux Tagged With: line, modify, replace, sed

Replace spaces with comma

February 4, 2020

Here’s a simple sed command to replace multiple spaces into a comma delimited file.

sample.txt

one       two
three     four

one two three four

The sed command.

sed 's/ \+ /,/g' sample.txt > sample2.txt

sed 's/ \+ /,/g' sample.txt > sample2.txt

sample2.txt

one,two
three,four

one,two three,four

Filed Under: Linux Tagged With: comma, find, replace, sed, spaces, tab

  • Home
  • About
  • Search

Copyright © 2023