Questions tagged [sed]

sed is a command-line stream editor for filtering and transforming text.

Sed is a stream editor: it reads one line of input, performs editing commands, and moves on to the next line. It is inspired by ed, Unix's first text editor and was developed as an enhanced replacement of . sed is a standard utility.

By far the most common usage of sed is to perform a simple pattern replacement:

sed -e 's/pattern/replacement/g'

The small command set and tools like "addressing" (line filtering) allow for easy and compact, yet powerful manipulations. While sed is Turing-complete and could theoretically be used for any complex tasks, it is not meant to be a programming tool. As soon as tasks like counting or column manipulation are involved, tools like or are likely a better choice.

External references

Further reading

7288 questions
117
votes
8 answers

SED: insert text after the last line?

This sed command inserts a tag to the beginning of a file: sed -i "1s/^/
user2362
92
votes
9 answers

Can sed replace new line characters?

Is there an issue with sed and new line character? I have a file test.txt with the following contents aaaaa bbbbb ccccc ddddd The following does not work: sed -r -i 's/\n/,/g' test.txt I know that I can use tr for this but my question is…
Jim
  • 10,120
71
votes
1 answer

Only return the matched string in sed

Possible Duplicate: Extracting a regex matched with 'sed' without printing the surrounding characters How do I make this only print test: echo "atestb" | sed -n 's/\(test\)/\1/p'
Tyilo
  • 5,981
67
votes
9 answers

How can I use variables in the LHS and RHS of a sed substitution?

I want to do: cat update_via_sed.sh | sed 's/old_name/new_name/' > new_update_via_sed.sh in my program. But I want to use variables, e.g. old_run='old_name_952' new_run='old_name_953' I have tried using them but the substitution doesn't happen (no…
64
votes
3 answers

sed capture groups not working

I have a string of the format [0-9]+\.[0-9]+\.[0-9]. I need to extract the first, second, and third numbers separately. As I understand it, capture groups should be capable of this. I should be able to use sed "s/\([0-9]*\)/\1/g to get the first…
Melab
  • 4,048
61
votes
3 answers

Why does sed outputs "char 53: unterminated `s' command"

Why this bash script ssh $SERVER bash < sed: -e expression #1, char 53: unterminated `s' command
BntMrx
  • 721
58
votes
3 answers

sed - how to do several consecutive substitutions but process file only once?

If I am doing several substitutions which need to be consecutive, e.g. sed -i '/^[[:space:]]*browser.*\.should/s/browser/expect(browser/' t1_spec.rb sed -i '/expect(browser.*\.should/s/\.should/).should/' t1_spec.rb sed -i 's/\.should/\.to/'…
53
votes
1 answer

How to replace a left parenthesis with sed?

I have to replace ( with some character in my file, and I can't do it. \( is used for grouping in sed and when I used \\( with sed, it treated it as \( character not as just (. It seems like a tricky case to me.
dwwdw
  • 965
52
votes
4 answers

How to use Sed to replace all characters before colon?

How do I replace the following string hd_ma_prod_customer_ro:*:123456789:john.doe with john.doe Basically I need to look for the last colon (:) and delete everything before and including it.
yihyoon
  • 521
48
votes
3 answers

Make multiple edits with a single call to sed

I'm trying to use sed to edit a config file. There are a few lines I'd like to change. I know that under Linux sed -i allows for in place edits but it requires you save to a backup file. However I would like to avoid having multiple backup files and…
rclark
  • 605
44
votes
3 answers

What does sed -i '1d' do?

I understand that sed is a command to manipulate text file. From my Googling, it seems -i means perform the operation on the file itself, is this correct? What about '1d'?
44
votes
10 answers

How do I substitute only first occurence with sed?

Original file claudio antonio claudio michele I want to change only the first occurrence of "claudio" with "claudia" so that I would get the following: claudia antonio claudio michele I have tried the following: sed -e…
elbarna
  • 12,695
42
votes
3 answers

How can I get my sed command to make permanent changes to a file?

Is this possible? I read somewhere that the following command would do it: sed -e [command] [file] but it appeared to do the same thing as just sed [command] [file] (it did not save the changes). Is there any way to do this using sed?
John
  • 3,599
41
votes
4 answers

Sed --- replace a character in a matched line in place?

In a file containing lines like this one: # lorem ipsum blah variable I would like to remove # (comment) character in the same line that contains a specific string, in place. Is sed good for this? I'm struggling to get this conditional working. I…
Kamil
  • 1,461
39
votes
2 answers

Using sed to get rid of characters < > ,

I have a file with lines as follows: ... ... <230948203[234]>, ... ... <234[24]>, ... .. I would like to use sed to remove the characters < , and > from every line I tried using sed 's/<>,//g' but it didn't work (it didn't change anything). Do I…
1
2 3
48 49