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
2301
votes
21 answers

How to delete from a text file, all lines that contain a specific string?

How would I use sed to delete all lines in a text file that contain a specific string?
A Clockwork Orange
  • 23,913
  • 7
  • 25
  • 28
1703
votes
43 answers

How can I replace each newline (\n) with a space using sed?

How can I replace a newline ("\n") with a space ("") using the sed command? I unsuccessfully tried: sed 's#\n# #g' file sed 's#^$# #g' file How do I fix it?
hhh
  • 50,788
  • 62
  • 179
  • 282
906
votes
37 answers

How can I do a recursive find/replace of a string with awk or sed?

How do I find and replace every occurrence of: subdomainA.example.com with subdomainB.example.com in every text file under the /home/www/ directory tree recursively?
Tedd
  • 9,105
  • 3
  • 17
  • 5
844
votes
23 answers

Bash tool to get nth line from a file

Is there a "canonical" way of doing that? I've been using head -n | tail -1 which does the trick, but I've been wondering if there's a Bash tool that specifically extracts a line (or a range of lines) from a file. By "canonical" I mean a program…
Vlad Vivdovitch
  • 9,295
  • 8
  • 22
  • 21
765
votes
20 answers

How can I remove the first line of a text file using bash/sed script?

I need to repeatedly remove the first line from a huge text file using a bash script. Right now I am using sed -i -e "1d" $FILE - but it takes around a minute to do the deletion. Is there a more efficient way to accomplish this?
Brent
  • 16,259
  • 12
  • 42
  • 42
635
votes
12 answers

Find and replace in file and overwrite file doesn't work, it empties the file

I would like to run a find and replace on an HTML file through the command line. My command looks something like this: sed -e s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html > index.html When I run this and look at the file afterward, it is…
BBales
  • 6,588
  • 3
  • 16
  • 19
635
votes
2 answers

How to remove double-quotes in jq output for parsing json files in bash?

I'm using jq to parse a JSON file as shown here. However, the results for string values contain the "double-quotes" as expected, as shown below: $ cat json.txt | jq '.name' "Google" How can I pipe this into another command to remove the ""? so I…
Chris F
  • 14,337
  • 30
  • 94
  • 192
578
votes
3 answers

What is the difference between sed and awk?

What is the difference between awk and sed ? What kind of application are best use cases for sed and awk tools ?
Rachel
  • 100,387
  • 116
  • 269
  • 365
502
votes
27 answers

Non greedy (reluctant) regex matching in sed?

I'm trying to use sed to clean up lines of URLs to extract just the domain. So from: http://www.suepearson.co.uk/product/174/71/3816/ I want: http://www.suepearson.co.uk/ (either with or without the trailing slash, it doesn't matter) I have…
Joel
  • 29,538
  • 35
  • 110
  • 138
500
votes
15 answers

sed edit file in-place

How do I edit a file in a single sed command? Currently, I have to manually stream the edited content into a new file and then rename the new file to the original file name. I tried sed -i, but my Solaris system said that -i is an illegal option. …
amphibient
  • 29,770
  • 54
  • 146
  • 240
496
votes
17 answers

Delete empty lines using sed

I am trying to delete empty lines using sed: sed '/^$/d' but I have no luck with it. For example, I have these lines: xxxxxx yyyyyy zzzzzz and I want it to be like: xxxxxx yyyyyy zzzzzz What should be the code for this?
jonas
  • 5,259
  • 4
  • 16
  • 11
438
votes
19 answers

Shell script - remove first and last quote (") from a variable

Below is the snippet of a shell script from a larger script. It removes the quotes from the string that is held by a variable. I am doing it using sed, but is it efficient? If not, then what is the efficient…
user1263746
  • 5,788
  • 4
  • 24
  • 28
438
votes
16 answers

Replace whole line containing a string using Sed

I have a text file which has a particular line something like sometext sometext sometext TEXT_TO_BE_REPLACED sometext sometext sometext I need to replace the whole line above with This line is removed by the admin. The search keyword is…
Rahul
  • 5,493
  • 6
  • 18
  • 12
435
votes
13 answers

sed command with -i option failing on Mac, but works on Linux

I've successfully used the following sed command to search/replace text in Linux: sed -i 's/old_link/new_link/g' * However, when I try it on my Mac OS X, I get: "command c expects \ followed by text" I thought my Mac runs a normal BASH shell. …
Yarin
  • 173,523
  • 149
  • 402
  • 512
420
votes
17 answers

Escape a string for a sed replace pattern

In my bash script I have an external (received from user) string, which I should use in sed pattern. REPLACE="" sed "s/KEYWORD/$REPLACE/g" How can I escape the $REPLACE string so it would be safely accepted by sed as a…
Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160
1
2 3
99 100