How to insert a line or a new line before the "Using SED" pattern (without changing the pattern) In MAC OS

I have a file with the following contents:

aaaabbaaabbaa 

and I need a conclusion like:

 aaaa bbaaa bbaa 

I need a new line to add before the first appearance of 'b' . I only need a SED command for use in bash

I am using the following command. I know this is perfect now ...

Can anyone tell me a better team than that. Pl mark only the SED command that 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 
+7
bash shell sed macos
source share
9 answers

With sed :

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

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

As I can see, you are using sed -i , it is also useful 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 "b" and replaces them with a new line followed by the same sequence "b" ( & represents everything that was matched on the left side of s/// ).

+9
source share

grep -oP with a lookahead regex would be simpler:

 echo 'aaaabbaaabbaa' | grep -oP '.+?[^b](?=(b|$))' aaaa bbaaa bbaa 
+3
source share

If your input line really only contains the characters a and b , then I think the problem degenerates to the simple replacement of all instances of ab with a<newline>b . If so, you can completely omit sed and use the shell parameter extension function 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 on apple.com. Find the "parameter / pattern" on this page.

+2
source share

The ugly version of awk :)

 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 b with a new line and & itself

 echo "aaaabbaaabbaa" | awk 'gsub(/b+/,"\n&")' aaaa bbaaa bbaa 
+1
source share
 echo "aaaabbaaabbaa\nbbaabba" | sed 's/\([^b]\)b/\1\ b/g' aaaa bbaaa bbaa bbaa bba 

posix is โ€‹โ€‹compatible and does not create a new line if the line starts with b

+1
source share

This might work for you:

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

This goes through the current line, replacing any ab a\nb combinations. It uses the side effect of holding that a new line is always present when a new sed instance is created.

Of course:

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

or

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

This is much simpler, but probably depends on GNU's sed or bash version.

+1
source share

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

I got it to work. This is very similar to your initial attempt. I'm on iMac, so I'm sure the same will work for you.

+1
source share

If you want to avoid a new line when b occurs at the beginning of the line and all of these are POSIX compatible.

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

You can say:

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

or

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

To make sed interpret regular expressions as extended regular expressions, you can use the -E option:

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

All Articles