Questions tagged [sed]

"sed" ("stream editor") is a Unix utility that parses and transforms text files.

"sed" (stream editor) is a Unix utility that parses and transforms text files.

It implements a fairly basic programming language to perform the transformations, but it is in fact Turing-complete.

Resources

sed on Wikipedia

1041 questions
53
votes
4 answers

How to use sed to remove null bytes?

What is the sed incantation to remove null bytes from a file? I'm trying: s/\000//g but that is stripping out strings of zeroes. s/\x00//g seems to have no effect. I'm trying to do this in a sed script, so I'm not sure the echo trick will work.
Chris Curvey
  • 1,427
22
votes
3 answers

Delete the first known character in a string with sed

How does one delete the first known character in a string with sed? For example, say I want to delete the first character (which is @) in the string "@ABCDEFG1234" I want sed to verify whether "@" exists as the first character. If so, it should…
yael
  • 539
18
votes
4 answers

Using sed get substring between two double quotes

I have a file xyz... rsync: "/home/path/to/file": Permission denied (13) rsync: "/home/path/to/file1": Permission denied (13) rsync: "/home/path/to/file2": Permission denied (13) rsync: "/home/path/to/file3": Permission denied (13) Now I want to…
XemX
  • 507
12
votes
2 answers

How to use verbatim strings in sed?

E.g., sed 's/string/ /g' where string="a\c:ti]\']x""/\//:`~$%#^&"' That is how to pass the string to sed literally rather than as an interpreted regex pattern? I'm going to use this with cygwin and on ubuntu, so the solutions should be compatible…
user93200
  • 389
9
votes
2 answers

How greedy is sed in matching patterns?

I know sed is greedy when matching patterns. But, how greedy is it? Consider these examples. $ echo 'foobar123' | sed 's/[0-9]*/(&)/' ()foobar123 $ echo 'foobar123' | sed 's/[0-9][0-9]*/(&)/' foobar(123) Why doesn't the second example print…
8
votes
4 answers

sed: replace any number of occurrences of a certain pattern

Given this input: "hell -- 'this -- world --is'-- beautiful' --thanks-- we'-- are-- here" I want to replace every '--' in between single quotes with 'X-X-X' using sed. It should give the following output: output: "hell --'this X-X-X world…
shiva
  • 81
8
votes
3 answers

How to append to the first line of a file?

How can you use sed to replace the entire first line of a file with that first line plus some additional text? For example, how can I append -foo to a file that contains only one line. testfile contents start: some-text testfile contents…
tarabyte
  • 2,323
7
votes
1 answer

What does `-e` option in sed do?

I am trying to understand a shell script that uses sed. I found that there were some places where sed was invoked with the -e option. I tried to see the man page and it just mentions, -e command Append the editing commands specified by the command…
Sudar
  • 181
5
votes
4 answers

sed: delete all but the last X lines of a file

It can be done with other tools, but I am interested to know how can I delete all but the last X lines of the file with sed.
4
votes
1 answer

Remove characters from column 'n' until end-line

I have a huge text file. This file has many lines and most of them have more that N characters before end-line. How can I use sed to truncate lines from Nth character until the end of line? Note: Obviously after this operation maximum line length is…
masoud
  • 167
  • 1
  • 8
4
votes
1 answer

How can I use sed to modify a line of a configuration file?

My configuration file has the following format [general] setting1=true setting2=value2 [ssl] setting1=sadfsdf setting2=sdfadsfkljasdf How would I change setting 1's value to false? If I could just delete the line, I could use the response from…
4
votes
3 answers

How to remove comments in all files including in subdirectories?

to remove comments (start with # and /* comment */) in settings.php, below command is just works fine: sed '/^\#/d' settings.php > outp.txt mv outp.txt settings.php sed '/\/\*/,/*\//d; /^\/\//d; /^$/d;' settings.php > outp2.txt mv outp2.txt…
3
votes
1 answer

Replace special characters with set from Windows cmd

I need to replace the string "\\" (double quote backslash backslash double quote) with "" (double quote double quote). The following is an example of the data to be manipulated: ","","","\\","",0, Result needs to be: ","","","","",0, I cannot…
3
votes
1 answer

sed parsing data: fields (words) separated with whitespace

I'm using sed on Linux, trying to match data lines having three fields, tab separated (but the separation could be any whitespace), as in: 12.3 0a 1b 15.5 0v 1h 17.7 5k 3c ; right now I'm using this: sed -n…
sdaau
  • 5,568
2
votes
2 answers

Sed command with apostrophe

sed -e 's/cluster_name: 'Test Cluster'/cluster_name: 'Test Cluster1'/' I have this error message: sed: -e expression, char 20: unterminated `s' command Please, how can i correct the sed command ?
1
2 3 4 5 6