Match a String That Contains a Newline Using Sed

Match a string that contains a newline using sed

try this line with gawk:

awk -v RS="\0" -v ORS="" '{gsub(/\t#\n\tpap/,"yourNEwString")}7' file

if you want to let sed handle new lines, you have to read the whole file first:

sed ':a;N;$!ba;s/\t#\n\tpap/NewString/g' file

sed to match pattern across a newline

Join the lines and tear them apart:

tr -d "\n" < file| grep -o "<string>[^<]*</string>"|sed 's/<string>\(.*\)<\/string>/\1/'

Match any character (including newlines) in sed

sed goes over the input file line by line which means, as I understand, what you want is not possible in sed.

You could use the following Perl script (untested), though:

#!/usr/bin/perl

use strict;
use warnings;

{
local $/; # slurp mode
my $html = <>;
$html =~ s/ style='[^']*'//g;
print $html;
}

__END__

A one liner would be:

$ perl -e 'local $/; $_ = <>; s/ style=\047[^\047]*\047//g; print' fileA > fileB

SED - replace string newline anything with string newline varable

By default, sed works line by line (using newline character to distinguish newlines)

$ cat ip.txt
foo baz
dhcp_option_domain:
- test.domain
123
dhcp_option_domain:
$ dhcp_domain='something.com'
$ sed '/^ dhcp_option_domain:/{n; s/.*/ - '"$dhcp_domain"'/}' ip.txt
foo baz
dhcp_option_domain:
- something.com
123
dhcp_option_domain:
  • /^ dhcp_option_domain:/ condition to match

    • {} to group more than one command to be executed when this condition is satisfied
  • n get next line
  • s/.*/ - '"$dhcp_domain"'/ replace it as required - note that shell variables won't be expanded inside single quotes, see sed substitution with bash variables
    for details
  • note that last line in the file didn't trigger the change as there was no further line
  • tested on GNU sed, syntax might vary for other implementations

From GNU sed manual

n

If auto-print is not disabled, print the pattern space, then,
regardless, replace the pattern space with the next line of input. If
there is no more input then sed exits without processing any more
commands.

How to insert a newline in front of a pattern?

Some of the other answers didn't work for my version of sed.
Switching the position of & and \n did work.

sed 's/regexp/\n&/g' 

Edit: This doesn't seem to work on OS X, unless you install gnu-sed.

How to match )\n{ using sed?

Perl can be good for this:

$ cat file.c
void some_function_declaration(char var1, char var2)
{
if (a)
{ b; }
}
void func2()
{
//
}
$ perl -0777 -pe 's {\)\n\{} {$& some other stuff;\n}g' file.c
void some_function_declaration(char var1, char var2)
{ some other stuff;

if (a)
{ b; }
}
void func2()
{ some other stuff;

//
}

Same caveat's as Aaron's answer due to reading the whole file into memory.

RegEx for replacing new lines

Given this input file:

$ cat file
1
2
foo
bar
3
etc
4
5

With any awk:

$ awk '{printf "%s%s", (/^[0-9]/ ? ors: ""), $0; ors=ORS} END{print ""}' file
1
2foobar
3etc
4
5

With GNU sed for -z to read the whole file as one string, -E to enable EREs, and to accept \n as meaning newline in the regexp:

$ sed -Ez 's/\n([^0-9])/\1/g' file
1
2foobar
3etc
4
5

Note that the awk solution, in addition to working portably with any awk in any shell on any UNIX box, only reads/stores one input line at a time and so will work efficiently and robustly for arbitrarily large files (as opposed to the sed solution which reads the whole file into memory and so YMMV for large files).

Insert newline (\n) using sed

The sed on BSD does not support the \n representation of a new line (turning it into a literal n):

$ echo "123." | sed -E 's/([[:digit:]]*)\./\1\n next line/'
123n next line

GNU sed does support the \n representation:

$ echo "123." | gsed -E 's/([[:digit:]]*)\./\1\nnext line/'
123
next line

Alternatives are:

Use a single character delimiter that you then use tr translate into a new line:

$ echo "123." | sed -E 's/([[:digit:]]*)\./\1|next line/' | tr '|' '\n'
123
next line

Or use an escaped literal new line in your sed script:

$ echo "123." | sed -E 's/([[:digit:]]*)\./\1\
next line/'
123
next line

Or define a new line:

POSIX:

nl='
'

BASH / zsh / others that support ANSI C quoting:

nl=$'\n'

And then use sed with appropriate quoting and escapes to insert the literal \n:

echo "123." | sed 's/\./'"\\${nl}"'next line/'
123
next line

Or use awk:

$ echo "123." | awk '/^[[:digit:]]+\./{sub(/\./,"\nnext line")} 1'
123
next line

Or use GNU sed which supports \n

sed won't match next line

By default, sed works on a per line basis. There is a special construct that allows "slurping" the whole file into a single string, similar to Get-Content -Raw in powershell, but it makes sense to use sed special language here.

Use

sed -r -i.bak '/^\[GUI]$/{N;s/\nVersion.*/\nVersion="3.02.108"/}' file
# Or
sed -r -i.bak '/^\[GUI]$/{n;s/Version.*/Version="3.02.108"/}' file

Here,

  • /^\[GUI]$/ - finds a line that is equal to [GUI]

and then

  • N; - reads the next line, appends \n and the line to the pattern space
  • s/\nVersion.*/\nVersion="3.02.108"/ - replaces the newline (that is added by the N command) and the following line starting with Version with a newline and Version="3.02.108"

Or,

  • n; - prints the current pattern space, empties the current pattern space, and reads in the next line of input (so, the difference with above is that the first "context" line is dropped from the string the substitution command will work on, and there will be no need matching \n that does not work across all sed versions)
  • s/Version.*/Version="3.02.108"/ - replaces the substring starting with Version and up to the string end with a Version="3.02.108" text.

See the online demo



Related Topics



Leave a reply



Submit