0

I'm trying to replace the space after a[6 digits] with a newline which I've tried doing with

sed 's/a\d{6}\s\+/\n/' old > new

For example:

a123456 this is a sentence

Would become:

a123456
this is a sentence

I am also open to any other programs in linux that would allow me to do this such as awk, perl, or bash.

beckah
  • 1,543
  • 6
  • 28
  • 61
  • possible duplicate of [Replace all whitespace with a line break/paragraph mark to make a word list](http://stackoverflow.com/questions/1853009/replace-all-whitespace-with-a-line-break-paragraph-mark-to-make-a-word-list) – Oleksandr Kravchuk Mar 27 '15 at 14:08
  • @beckah don't delete the question if you want an answer. – Avinash Raj Mar 27 '15 at 14:18

3 Answers3

1

First, I would suggest using POSIX extended regexes with sed -r here to simplify the pattern.


POSIX regexes or even POSIX extended regexes (with -r) don't supprt \d or \s for character classes as for example Perl. I would use the following pattern:

sed -r 's/(a[0-9]{6}) +/\1\n/' old > new

Note that I'm capturing everything before the space into a capturing group and use this group \1 in the replacement pattern.

However, there are indeed named character classes available in POSIX regexes but they are longer to type than in Perl. If you want to use named character classes, you can use:

sed -r 's/(a[[:digit:]]{6})[[:space:]]+/\1\n/' old > new

in this case. If you want to know more about the available character classes in POSIX regexes check the manpages of grep and/or egrep.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
1

You can use:

s='a123456 this is a sentence'
sed -r $'s/\<(a[[:digit:]]{6})[[:blank:]]+/\\1\\\n/' <<< "$s"
a123456
this is a sentence

On BSD sed use (OSX):

sed -E $'s/[[:<:]](a[[:digit:]]{6})[[:blank:]]+/\\1\\\n/' <<< "$s"
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Why are you using the `$` before the sed program? – hek2mgl Mar 27 '15 at 14:22
  • 1
    This is BASH syntax to get `\n` in sed substitution. Otherwise `sed -E 's/(a[[:digit:]]{6})[[:space:]]+/\1\n/'` places a literal `n` in replacement on OSX – anubhava Mar 27 '15 at 14:26
0

perl:

perl -pe 's/(?<=a\d{6})\s+/\n/'

Using a look-behind, replace the whitespace with a newline.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352