Questions tagged [gnu-sed]

Use this tag for questions specific to the GNU version of the stream editor sed. Notice that questions should not be about general (interactive) usage of GNU sed, but programming using GNU sed. For sed questions not specific to GNU sed, use the "sed" tag.

GNU sed is the GNU version of sed, a "non-interactive command-line text editor". It features several extensions to POSIX sed such as support of extended regular expressions (ERE), in-place editing, and case modifiers for substitutions.

The current version is GNU sed 4.4, released on 2017-02-04.

Links of interest

33 questions
82
votes
4 answers

Sed gives: sed: can't read : No such file or directory

I have the following bash script which repeats for each image found. It needs to iterated over all html, css and js files, and replace all occurrences of an image within that file. for image in app/www/images-theme-dark/*.png do echo…
Bas van Dijk
  • 9,933
  • 10
  • 55
  • 91
7
votes
1 answer

Why does "sed -n -i" delete existing file contents?

Running Fedora 25 server edition. sed --version gives me sed (GNU sed) 4.2.2 along with the usual copyright and contact info. I've create a text file sudo vi ./potential_sed_bug. Vi shows the contents of this file (with :set list enabled) as: …
AMemberofDollars
  • 331
  • 2
  • 14
5
votes
1 answer

Explanation of difference between GNU sed and BSD sed

I wrote the following command echo -en 'uno\ndue\n' | sed -E 's/^.*(uno|$)/\1/' expecting the following output uno This is indeed the case with my GNU Sed 4.8. However, I've verified that BSD Sed outputs Why is that the case?
Enlico
  • 23,259
  • 6
  • 48
  • 102
5
votes
1 answer

Tiebreaker for same-length regex alternatives with same starting position

Using GNU sed (with the -r flag for clarity), the following two substitutions on the input string ab give the same result: s/(.)(.)|(.)(.)$/\2\1\3\4/ and s/(.)(.)$|(.)(.)/\1\2\4\3/ both give ba. It would appear that the alternative (.)(.) (the one…
user41805
  • 523
  • 1
  • 9
  • 21
4
votes
4 answers

sed : Add newline after specific number of spaces and not characters

I want to add newline to my txt file after 48 spaces. This is the text line 151 150 147 155 148 133 111 140 170 174 182 154 153 164 173 178 185 185 189 187 186 193 194 185 183 186 180 173 166 161 147 133 172 151 114 161 161 146 131 104 95 132 163…
Mejdi Dallel
  • 573
  • 9
  • 24
3
votes
1 answer

How to end the `i` command in GNU sed?

I'm trying to use sed to make several substitutions and insertions of an input string. However, I recently noticed that the insert command i doesn't terminate on ; like others, and instead prints the rest of the string. $ sed "s/^foo/bar/; 1i foo…
Martín Fixman
  • 9,055
  • 9
  • 38
  • 46
2
votes
2 answers

Regex for whitespace delemiter except for [ and ] characters

I consider my self pretty good with Regular Expressions, but this one is appearing to be surprisingly tricky. I want to trim all whitespace, except the ones between "" and [] characters. I used this regex ("[^"]*"|\S+)\s+ but did split the…
Bek
  • 261
  • 3
  • 11
2
votes
2 answers

GNU sed 4.2.1 matching second occurence

With this command I am trying to insert M09 between two patterns of M502 and the second line since M502 that looks like X41.5Y251.5T201. Specifically I am trying to insert M09 before the last line. sed -i…
2
votes
1 answer

Replace First Occurrence Only of Pipe Character in String

I need to remove an extra pipe character at the end of header row of a pipe delimited csv file with sed. The literal string that I am trying to find is COLNAME| Working on a GCP Windows server. The command I am trying to use: "C:\Program Files…
2
votes
3 answers

Re-index two digit strings based on occurrence of a common string

I have a urlwatch .yaml file that has this format: name: 01_urlwatch update released url: "https://github.com/thp/urlwatch/releases" filter: - xpath: path: '(//div[contains(@class,"release-timeline-tags")]//h4)[1]/a' - html2text:…
John
  • 469
  • 1
  • 4
  • 17
2
votes
1 answer

How can I append to holdspace from patternspace without inserting a new line into the holdspace using sed?

The sed command 'H' appends from patternspace to holdspace after appending a new line into the holdspace first. How can I append without inserting a new line into holdspace?
MoN
  • 53
  • 4
2
votes
3 answers

sed: remember capture in second expression

I am trying to find occurence of captured pattern and pre-pend next line with captured pattern. For example: ... [line 10] #---------- SOLID: tank_phys.0 [line 11] Shape { ... [line 22] #---------- SOLID: head_phys.0 [line 23] Shape { …
user1301295
  • 674
  • 1
  • 6
  • 15
2
votes
1 answer

GNU sed, ^ and $ with | when first/last character matches

When doing a substitution that includes something like ^|. in the REGEXP sed doesn't match the null string at beginning of the pattern space if the first character matches. It also doesn't match the end if the last character matches. Why is…
Riley
  • 698
  • 6
  • 11
2
votes
3 answers

Sed command does not recognise apostrophes

In my bash script, I am trying to replace all apostrophes(') with two apostrophes('') in a csv file in order to commit it to a postgres database. As you know all single quotes in a postgres query need to be escaped with an apostrophe. I can't use…
1
vote
3 answers

sed if pattern match copy pattern line along with n number of lines after this to another file

I am looking for a sed command to match pattern and copy pattern line and n number of lines after this line. pattern is 01, 02, 03 sequence. Example: group-title=01- line1 http://www.yahoo.com group-title=02-…
Rizwan.A
  • 21
  • 7
1
2 3