Questions tagged [sed]

sed is a command line editor for POSIX environments. It processes one or more files according to an editing script and writes the results to standard output. Created at Bell Labs, it has been around since the mid-70s.

sed (acronym for stream editor) was created at Bell Labs by Lee McMahon in 1973. It is one of the basic tools in the POSIX environment—it processes one or more files according to an editing script and writes the results to standard output.

Generally, sed is used when a text file needs to be edited programmatically; i.e. without using an interactive text editor (such as ed, vi, nano). For example, this command

sed "s/bar/42/g" file

will print the contents of file where all occurrences of bar are replaced by 42. By default, sed prints to stdout rather than overwriting the input file.

Perhaps notice that the regular expression dialect supported by sed is very "traditional", and lacks many of the facilities offered by more modern dialects. Generally, sed does not support greedy matching .*? or lookarounds (?!...), (?<=...)etc; indeed, plain sed traditionally does not even support + for "one or more" repetition, though modern BRE has it in the form \+ (and also \? for optional, etc), and some sed implementation support extended regex with a nonstandard option like -E or -r, where the backslashes are not required for these constructs. Shorthands like \s for space, \d for digits, etc are also not portable, though some sed implementations do support them.

sed commands consist of an optional address, a single-letter function, and the function's arguments. A script is made up of one or more commands separated by semicolons. Some of the most commonly used functions are:

Function Arguments Description
s
Substitute
<search pattern>/<replacement text>/<flags> Performs text replacement
d
Delete
none Delete the current line
a
Append
<text to be appended> Append text to the current line
i
Insert
<text to be inserted> Insert text at the current location
p
Print
none Print the current line
q
Quit
none Quit the script

Popular questions

Some frequently asked Bash questions include:

Resources

External Resources

Free Sed Books

Other Stack Exchange sites

See also

  • a kindred tool often mentioned in the same breath
  • the notation used by sed and other commands to perform search (and replace) operations
  • a command line text search utility
  • a command line utility for translating or deleting characters
28570 questions
391
votes
12 answers

How can I output only captured groups with sed?

Is there a way to tell sed to output only captured groups? For example, given the input: This is a sample 123 text and some 987 numbers And pattern: /([\d]+)/ Could I get only 123 and 987 output in the way formatted by back references?
Pablo
  • 28,133
  • 34
  • 125
  • 215
345
votes
14 answers

Replace comma with newline in sed on MacOS?

I have a file of strings that are comma separated. I'm trying to replace the commas with a new line. I've tried: sed 's/,/\n/g' file but it is not working. What am I missing?
WildBill
  • 9,143
  • 15
  • 63
  • 87
342
votes
8 answers

Delete specific line number(s) from a text file using sed?

I want to delete one or more specific line numbers from a file. How would I do this using sed?
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
338
votes
13 answers

How to swap text based on patterns at once with sed?

Suppose I have 'abbc' string and I want to replace: ab -> bc bc -> ab If I try two replaces the result is not what I want: echo 'abbc' | sed 's/ab/bc/g;s/bc/ab/g' abab So what sed command can I use to replace like below? echo abbc | sed…
DaniloNC
  • 3,555
  • 2
  • 16
  • 8
331
votes
6 answers

Error when using 'sed' with 'find' command on OS X: "invalid command code ."

Being forced to use CVS for a current client and the address changed for the remote repo. The only way I can find to change the remote address in my local code is a recursive search and replace. However, with the sed command I'd expect to work: find…
helion3
  • 34,737
  • 15
  • 57
  • 100
330
votes
15 answers

sed in-place flag that works both on Mac (BSD) and Linux

Is there an invocation of sed todo in-place editing without backups that works both on Linux and Mac? While the BSD sed shipped with OS X seems to need sed -i '' …, the GNU sed Linux distributions usually come with interprets the quotes as empty…
dnadlinger
  • 5,883
  • 4
  • 21
  • 24
323
votes
25 answers

How to use sed to replace only the first occurrence in a file?

I would like to update a large number of C++ source files with an extra include directive before any existing #includes. For this sort of task, I normally use a small bash script with sed to re-write the file. How do I get sed to replace just the…
David Dibben
  • 18,460
  • 6
  • 41
  • 41
321
votes
8 answers

Find and replace with sed in directory and sub directories

I run this command to find and replace all occurrences of 'apple' with 'orange' in all files in root of my site: find ./ -exec sed -i 's/apple/orange/g' {} \; But it doesn't go through sub directories. What is wrong with this command? Here are some…
hd.
  • 17,596
  • 46
  • 115
  • 165
321
votes
8 answers

Insert line after match using sed

For some reason I can't seem to find a straightforward answer to this and I'm on a bit of a time crunch at the moment. How would I go about inserting a choice line of text after the first line matching a specific string using the sed command. I…
user2150250
  • 4,797
  • 11
  • 36
  • 44
312
votes
20 answers

How to insert a text at the beginning of a file?

So far I've been able to find out how to add a line at the beginning of a file but that's not exactly what I want. I'll show it with an example: File content some text at the beginning Result some text at the beginning It's similar…
user219882
  • 15,274
  • 23
  • 93
  • 138
312
votes
12 answers

Environment variable substitution in sed

If I run these commands from a script: #my.sh PWD=bla sed 's/xxx/'$PWD'/' ... $ ./my.sh xxx bla it is fine. But, if I run: #my.sh sed 's/xxx/'$PWD'/' ... $ ./my.sh $ sed: -e expression #1, char 8: Unknown option to `s' I read in tutorials that to…
RomanM
  • 6,363
  • 9
  • 33
  • 41
301
votes
7 answers

Retrieve last 100 lines logs

I need to retrieve last 100 lines of logs from the log file. I tried the sed command sed -n -e '100,$p' logfilename Please let me know how can I change this command to specifically retrieve the last 100 lines.
Surabhi
  • 3,508
  • 2
  • 17
  • 12
299
votes
11 answers

Change multiple files

The following command is correctly changing the contents of 2 files. sed -i 's/abc/xyz/g' xaa1 xab1 But what I need to do is to change several such files dynamically and I do not know the file names. I want to write a command that will read all…
shantanuo
  • 31,689
  • 78
  • 245
  • 403
287
votes
5 answers

What are the differences between Perl, Python, AWK and sed?

What are the main differences among them? And in which typical scenarios is it better to use each language?
Khaled Al Hourani
  • 3,277
  • 3
  • 20
  • 17
273
votes
12 answers

Command to get nth line of STDOUT

Is there any bash command that will let you get the nth line of STDOUT? That is to say, something that would take this $ ls -l -rw-r--r--@ 1 root wheel my.txt -rw-r--r--@ 1 root wheel files.txt -rw-r--r--@ 1 root wheel here.txt and do something…
Alana Storm
  • 164,128
  • 91
  • 395
  • 599