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
5
votes
2 answers

Expanding environment variables with sed

I am trying to write a sed command to replace tokens in a file with values from environment variables like so: export my_var=foo echo 'something {{my_var}} bar' | sed -r "s/\{\{(.*?)\}\}/$\1/g" I want to grab the name of the token (my_var in this…
toby
  • 683
  • 2
  • 6
  • 16
5
votes
1 answer

$ in regular expression not maching against end of line

I have a problem with the following regular expression: var s = "http://www.google.com/dir/file\r\nhello" var re = new RegExp("http://([^/]+).*/([^/\r\n]+)$"); var arr = re.exec(s); alert(arr[2]); Above, I expect arr[2] (i.e. capture group 2) to be…
5
votes
4 answers

Delete lines by pattern in specific range of lines

I want to remove lines from file by regex pattern using sed just like in this question Delete lines in a text file that containing a specific string, but only inside a range of lines (not in the whole file). I want to do it starting from some line…
ks1322
  • 33,961
  • 14
  • 109
  • 164
5
votes
3 answers

Remove line break every nth line using sed

Example: Is there a way to use sed to remove/subsitute a pattern in a file for every 3n + 1 and 3n+ 2 line? For example, turn Line 1n/ Line 2n/ Line 3n/ Line 4n/ Line 5n/ Line 6n/ Line 7n/ ... To Line 1 Line 2 Line 3n/ Line 4 Line 5 Line 6n/ ... I…
user3805884
  • 125
  • 2
  • 8
5
votes
3 answers

sed substitution including newlines

I want to change a text file so that any line beginning with "Length:" is appended to the previous line. I'm aware that sed '/\nLength:/ Length:/' isn't going to work because sed is line based. Googling for "How to match newlines in sed" did turn up…
Dave Rove
  • 913
  • 1
  • 12
  • 18
5
votes
1 answer

Error with sed unterminated s command in bash

I'm having a problem with using sed in a bash script. Here is the line: sed -i "s/"$name"/"$input"/g" ~/input.script I'm getting this error: sed: -e expression #1, char 24: unterminated `s' command Weirdly enough it was working in an…
Kev
  • 51
  • 2
5
votes
2 answers

Replace a double backslash followed by quote (\\') using sed?

I am unable to replace double backslash followed by quote \\' in sed. This is my current command echo file.txt | sed "s:\\\\':\\':g" The above command not only replaces \\' with \' it also replaces \' with ' How could I just replace exact…
Jardalu
  • 231
  • 3
  • 9
5
votes
2 answers

Edit huge sql data file

I have a 23GB file and I would like to edit the 23rd line, but I have only 200 MB RAM available on the server. I do not want to open the file entirely because I have left only 20GB available disk space. How can I do this. I tried to use head, tail…
Amrida D
  • 329
  • 1
  • 5
  • 17
5
votes
3 answers

Replace JSON Object Programmatically

I'm looking for the most effective way to replace a JSON Object in a file. 20150628 - Update at the bottom of this post Here's the scenario: I have a bunch of JSON files (many) and in these files are large chunks of JSON (sometimes 20-30K lines of…
Donn Felker
  • 9,553
  • 7
  • 48
  • 66
5
votes
2 answers

sed (in bash) works with [ \t] but not with \s?

I want to search-replace something containing whitespace on a bash command line, and I assumed sed would be the easiest way to go. Using [ \t] denoting either tab or space, to match the whitespace, works as intended: echo "abc xyz" | sed "s/[…
RocketNuts
  • 9,958
  • 11
  • 47
  • 88
5
votes
2 answers

Select matrix first row based on 1st, 8th and 9th column value with awk or sed

I have some rows where the 1st, 8th and 9th columns are mostly the same. The total number of rows is well over 60K. Now I want to simplify keeping only the first rows for which the 1st,8th and 9th column are same. Input file: chr exon_start …
ivivek_ngs
  • 917
  • 3
  • 10
  • 28
5
votes
2 answers

sed 's/this/that/' -- ignoring g but still replace entire file

as title said, Im trying to change only the first occurrence of word.By using sed 's/this/that/' file.txt though i'm not using g option it replace entire file. How to fix this.? UPDATE: $ cat file.txt first line this this this this $…
webminal.org
  • 44,948
  • 37
  • 94
  • 125
5
votes
2 answers

sed not working from within bash script

I have read all the similar questions on this topic, but didn't find a matching question to what I am experiencing. I apologise if this has been answered already. Inside a bash script I wrote, there is a very simple sed command, which does not seem…
asimovwasright
  • 838
  • 1
  • 11
  • 28
5
votes
6 answers

sed remove first pattern match only

I would like to match a set of data between two patterns and remove this data and the start/end patterns but only for the first occurrence of the pattern. So if this is the test…
SnazzyBootMan
  • 669
  • 2
  • 15
  • 30
5
votes
2 answers

Using sed to delete a string between parentheses

I'm having trouble figuring how to delete a string in between parentheses only when it is in parentheses. For example, I want to delete the string "(Laughter)", but only when it's in parenthesis and not just make it a case sensitive deletion since a…
Teddy T
  • 51
  • 1
  • 1
  • 3
1 2 3
99
100