sam
sam

Reputation: 2486

How to insert string or newline before a pattern "Using SED" ( Without replacing the pattern) In MAC OS

I have a file with the following content:

aaaabbaaabbaa

and i need an output like:

aaaa
bbaaa
bbaa

I need a new line to be added before first occurrence of 'b'. I need only SED command to use in bash

I am using the following command. I know its now the perfect one..

Can anyone tell me a better command than this. Pl note only SED command i need to use in bash

sed -i.bak  -e 's/bb/qbb/g' input.txt  
sed -i.bak  -e 's/qbb/\'$'\nbb/g' input.txt

Upvotes: 8

Views: 12654

Answers (9)

devnull
devnull

Reputation: 123658

You could say:

$ echo aaaabbaaabbaa | sed 's/b\{1,\}/\'$'\n&/g'
aaaa
bbaaa
bbaa

or

$ echo aaaabbaaabbaa | sed $'s/b\{1,\}/\\\n&/g'
aaaa
bbaaa
bbaa

In order to make sed interpret the regex as extended regular expressions, you could use the -E option:

$ echo aaaabbaaabbaa | sed -E 's/b+/\'$'\n&/g'
aaaa
bbaaa
bbaa

Upvotes: 1

Jotne
Jotne

Reputation: 41460

An ugly awk version :)

echo "aaaabbaaabbaa" | awk '{for (i=1;i<=NF;i++) {printf ($i=="b" && f!="b" ?"\n":"")"%s",$i; f=$i}} END {print ""}' FS=
aaaa
bbaaa
bbaa

A gnu awk version

echo "aaaabbaaabbaa" | awk '{$1=$1} NR>1 {$0=RS $0;} 1' RS="bb"
aaaa
bbaaa
bbaa

Another awk. Replace any b or group of b with newline and itself &

echo "aaaabbaaabbaa" | awk 'gsub(/b+/,"\n&")'
aaaa
bbaaa
bbaa

Upvotes: 1

Digital Trauma
Digital Trauma

Reputation: 16016

If your input string really does contain only a and b characters, then I think the problem degenerates to a simple replacement of all instances of ab with a<newline>b. If this is the case then you can omit sed altogether and use the Shell Parameter Expansion feature in bash:

At the terminal:

$ str="aaaabbaaabbaa"
$ echo "${str//ab/a
> b}"
aaaa
bbaaa
bbaa
$ 

Or in a shell script:

$ cat ab.sh 
#!/bin/bash
echo "${1//ab/a
b}"
$ ./ab.sh "aaaabbaaabbaa"
aaaa
bbaaa
bbaa
$ 

This works for me on OSX 10.8.5.

This information is also available on the bash manpage hosted by apple.com. Search for "parameter/pattern" on that page.

Upvotes: 2

Hynek -Pichi- Vychodil
Hynek -Pichi- Vychodil

Reputation: 26141

When you want avoid new line when b occurs at beginning of line and all of this POSIX compliant.

$ echo -e "aaaabbaaabbaa\nbbaaaabbaaabbaa" | sed -e 's/\([^b]\)b/\1\nb/g'
aaaa
bbaaa
bbaa
bbaaaa
bbaaa
bbaa

Upvotes: 1

jeff
jeff

Reputation: 84

sed -e 's/bb/\ nn/g' input.txt

I got this to work. It is very similar to your original attempt. I am on an iMac, so I am pretty sure the same will work for you.

Upvotes: 1

potong
potong

Reputation: 58578

This might work for you:

sed -e :a -e '/ab\(.*\)\(.\)$/!b' -e G -e 's//a\2b\1/' -e ta file

This loops through the current line replacing any ab combinations with a\nb. It uses the hold space side-effect that a newline is always present when a new instance of sed is created.

Of course:

sed 's/bb*/\n&/g' file

or:

sed 's/bb*/'"\n"'&/g' file

Is a lot easier but probably depends on a GNU version of sed or bash.

Upvotes: 1

NeronLeVelu
NeronLeVelu

Reputation: 10039

echo "aaaabbaaabbaa\nbbaabba" | sed 's/\([^b]\)b/\1\
b/g'

aaaa
bbaaa
bbaa
bbaa
bba

posix compliant and does not make a new line if line start with a b

Upvotes: 1

fedorqui
fedorqui

Reputation: 290455

With sed:

$ echo "aaaabbaaabbaa" | sed -r 's/([b]+)/\n\1/g'
aaaa
bbaaa
bbaa

sed -r allows to catch blocks with () and print them back with \1. The block it catches it [b]+, meaning "one or more b's", and prints it back preceded by a new line.

As I see you are using sed -i, it is also good to do:

sed -i.bak -r 's/([b]+)/\n\1/g' input.txt

Also, easier (thanks Glenn Jackman!)

$ echo "aaaabbaaabbaa" | sed 's/b\+/\n&/g'
aaaa
bbaaa
bbaa

It replaces all sequences of "b" and replaces that with a newline followed by that same sequence of "b" (& represents whatever was matched on the left side of s///).

Upvotes: 11

anubhava
anubhava

Reputation: 786241

grep -oP with lookahead regex will be easier:

echo 'aaaabbaaabbaa' | grep -oP '.+?[^b](?=(b|$))'

aaaa
bbaaa
bbaa

Upvotes: 3

Related Questions