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

6906 questions
921
votes
10 answers

How can I replace a string in a file(s)?

Replacing strings in files based on certain search criteria is a very common task. How can I replace string foo with bar in all files in the current directory? do the same recursively for sub directories? replace only if the file name matches…
terdon
  • 220,769
  • 58
  • 415
  • 622
417
votes
2 answers

Using 'sed' to find and replace

I know this question has probably been answered before. I have seen many threads about this in various places, but the answers are usually hard to extract for me. I am looking for help with an example usage of the 'sed' command. Say I wanted to…
roo
  • 4,331
  • 4
  • 11
  • 7
400
votes
18 answers

How can I use sed to replace a multi-line string?

I've noticed that, if I add \n to a pattern for substituting using sed, it does not match. Example: $ cat > alpha.txt This is a test Please do not be alarmed $ sed -i'.original' 's/a test\nPlease do not/not a test\nBe/' alpha.txt $ diff…
Belmin Fernandez
  • 8,767
  • 14
  • 44
  • 49
378
votes
4 answers

What characters do I need to escape when using sed in a sh script?

Take the following script: #!/bin/sh sed 's/(127\.0\.1\.1)\s/\1/' [some file] If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't need to escape the backslashes themselves (between…
detly
  • 4,710
  • 5
  • 22
  • 28
202
votes
4 answers

What does <<< mean?

What does <<< mean? Here is an example: $ sed 's/a/b/g' <<< "aaa" bbb Is it something general that works with more Linux commands? It looks like it's feeding the sed program with the string aaa, but isn't << or < usually used for that?
Daniel Jonsson
  • 2,165
  • 3
  • 12
  • 9
195
votes
7 answers

Using sed to find and replace complex string (preferrably with regex)

I have a file with the following contents: and I need to make a script that changes the "name" in the first line to "something", the…
Harry Muscle
  • 2,187
  • 2
  • 12
  • 7
183
votes
6 answers

Return only the portion of a line after a matching pattern

So pulling open a file with cat and then using grep to get matching lines only gets me so far when I am working with the particular log set that I am dealing with. It need a way to match lines to a pattern, but only to return the portion of the line…
MaQleod
  • 2,424
  • 4
  • 21
  • 18
161
votes
11 answers

Delete First line of a file

How can I delete the first line of a file and keep the changes? I tried this but it erases the whole content of the file. $sed 1d file.txt > file.txt
kickass13
  • 1,683
  • 3
  • 12
  • 5
143
votes
16 answers

Decoding URL encoding (percent encoding)

I want to decode URL encoding, is there any built-in tool for doing this or could anyone provide me with a sed code that will do this? I did search a bit through unix.stackexchange.com and on the internet but I couldn't find any command line tool…
DisplayName
  • 10,868
  • 18
  • 69
  • 108
141
votes
15 answers

How to insert text before the first line of a file?

I've been looking around sed command to add text into a file in a specific line. This works adding text after line 1: sed '1 a\ But I want to add it before line 1. It would be: sed '0 a\ but I get this error: invalid usage of line address 0. Any…
Manolo
  • 1,547
  • 2
  • 10
  • 11
133
votes
14 answers

Replace string in a huge (70GB), one line, text file

I have a huge (70GB), one line, text file and I want to replace a string (token) in it. I want to replace the token , with another dummy token (glove issue). I tried sed: sed 's///g' < corpus.txt > corpus.txt.new but the output…
Christos Baziotis
  • 1,457
  • 3
  • 13
  • 10
129
votes
3 answers

How to insert text after a certain string in a file?

Right now I'm using echo "Hello World" >> file.txt to append some text to a file but I also need to add text below a certain string let's say [option], is it possible with sed? EG: Input file Some text Random [option] Some stuff Output file Some…
Javier Villanueva
  • 1,587
  • 2
  • 14
  • 16
114
votes
7 answers

Convert file contents to lower case

I have temp file with some lower-case and upper-case contents. Input Contents of my temp file: hi Jigar GANDHI jiga I want to convert all upper to lower. Command I tried the following command: sed -e "s/[A-Z]/[a-z]/g" temp but got wrong…
JigarGandhi
  • 4,520
  • 9
  • 25
  • 38
113
votes
11 answers

how to rename multiple files by replacing string in file name? this string contains a "#"

https://serverfault.com/questions/70939/how-to-replace-a-text-string-in-multiple-files-in-linux https://serverfault.com/questions/228733/how-to-rename-multiple-files-by-replacing-word-in-file-name https://serverfault.com/questions/212153/replace-stri…
Leon Francis Shelhamer
110
votes
7 answers

Is there a basic tutorial for grep, awk and sed?

I've been a Linux user for a while, and I've a pretty decent understanding of most the common command line utilities. However, ones that come up and up again in relation to programming are grep, awk, and sed. About the only thing I've used grep for…
Macha
  • 3,680
  • 7
  • 29
  • 34
1
2 3
99 100