Questions tagged [sed]

Sed (Stream EDitor) is a command line editor for POSIX environment. Sed 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 (Stream EDitor) was created at Bell Labs by Lee McMahon in 1973 or 1974. 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.

Sed is normally required when the existing text file must be modified without user interactions by some script (bash script, for instance). For instance, the commands

export ARG=17
sed "s/b/${ARG}/g" s.sh

will print the text of s.sh where all occurrences of b are replaced by the value of the environment variable ARG, 17.

By default, sed prints to stdout rather than overwriting the input file.

Popular questions

Some frequently asked Bash questions include:

Resources

External Resources

Free Sed Book

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
25779 questions
1982
votes
19 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
  • 20,481
  • 6
  • 21
  • 23
1496
votes
42 answers

How can I replace a newline (\n) 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
  • 44,388
  • 56
  • 154
  • 251
759
votes
35 answers

How to 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
  • 7,635
  • 3
  • 15
  • 5
696
votes
19 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
  • 7,727
  • 6
  • 18
  • 20
645
votes
16 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
  • 14,219
  • 12
  • 38
  • 41
616
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,378
  • 3
  • 13
  • 18
579
votes
25 answers

How can I extract a predetermined range of lines from a text file on Unix?

I have a ~23000 line SQL dump containing several databases worth of data. I need to extract a certain section of this file (i.e. the data for a single database) and place it in a new file. I know both the start and end line numbers of the data that…
Adam J. Forster
  • 14,831
  • 9
  • 23
  • 20
520
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
  • 91,207
  • 112
  • 255
  • 361
440
votes
22 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
  • 27,478
  • 33
  • 104
  • 136
397
votes
15 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
  • 4,269
  • 4
  • 14
  • 11
366
votes
14 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
  • 4,643
  • 4
  • 16
  • 12
356
votes
13 answers

sed edit file in place

I am trying to find out if it is possible to edit a file in a single sed command without manually streaming the edited content into a new file and then renaming the new file to the original file name. I tried the -i option but my Solaris system…
amphibient
  • 25,192
  • 44
  • 130
  • 215
349
votes
15 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
  • 34,198
  • 31
  • 94
  • 153
342
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
  • 144,097
  • 139
  • 361
  • 489
319
votes
10 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
  • 24,270
  • 32
  • 112
  • 196
1
2 3
99 100