1

I'm trying to figure out how to insert text before and after every line in a text file using sed on OS X, but something is obviously wrong with my approach.

This is what I have:

sed 's/\(^\).\($\)/A \1\2 Z/g' input

This is what I want:

input:

bbb

ccc
ddd

output:

A bbb Z

A ccc Z
A ddd Z

EDIT: Also, don't match blank lines (or those containing only spaces).

octosquidopus
  • 3,517
  • 8
  • 35
  • 53
  • `s/(.+)\n/A \1 Z\n/g` this will match an entire line and prepend `A` and append `Z` – x13 Oct 08 '15 at 13:53
  • 1
    `sed 's/^/A /;s/$/ Z/'` or `sed 's/.*/A & Z/'` – 123 Oct 08 '15 at 13:54
  • 1
    @ThisNameBetterBeAvailable I get `sed: 1: "s/(.+)\n/A \1 Z\n/g": \1 not defined in the RE` @999999999999999999999999999999Much better. – octosquidopus Oct 08 '15 at 13:56
  • @octosquidopus ow, sorry. i think it should be `$1` instead – x13 Oct 08 '15 at 13:57
  • @ThisNameBetterBeAvailable No it shouldn't, you need to escape the `+` and brackets. Also don't include `\n`'s as it won't match the first part. – 123 Oct 08 '15 at 14:00
  • GNU `sed` behaves somewhat differently from other versions of `sed` w.r.t whether you need to use `\(` or `(` to start captures, etc. It is partly controlled by the `-r` option — that definitively enables some simpler syntax; there may also be some 'intuition' (heuristics?) employed by GNU `sed` about which notation you want. – Jonathan Leffler Oct 08 '15 at 14:00
  • @JonathanLeffler Pretty sure you can use ERE in all sed's now can't you? It's just posix ones use `-E` instead of `-r`. – 123 Oct 08 '15 at 14:02
  • 1
    @999999999999999999999999999999: Most versions of `sed` don't recognize ERE notation unless the option is specified. GNU `sed` sometimes appears to recognize the ERE-ish (and even some PCRE-ish) notations even without being told to do so. I've not worked whether that's me being silly or GNU `sed` being clever. It complicates life because `sed` doesn't behave the same everywhere. (I also have to work on a platform where `grep` doesn't support `-r` (recursive) search — I was surprised to find recently, after several years away from it. Its `sed` won't have ERE support.) – Jonathan Leffler Oct 08 '15 at 14:08
  • @JonathanLeffler ah right. I've only ever seen it use ERE when a flag is used. Have to keep my eye out for that! – 123 Oct 08 '15 at 14:09
  • Possible duplicate of [how to insert a line using sed before a pattern and after a line number?](http://stackoverflow.com/questions/6284518/how-to-insert-a-line-using-sed-before-a-pattern-and-after-a-line-number) – midori Oct 08 '15 at 16:06

2 Answers2

2

You can use:

sed 's/.*/A & Z/' file
A bbb Z
A ccc Z
A ddd Z

& is back-reference of complete match in regex pattern.

Or using awk:

awk '{print "A", $0, "Z"}' file
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

You could use:

sed 's/.*/A \0 Z/' input
  • .* will match the full line
  • \0 will past the full thing from the first expression
UlfR
  • 4,175
  • 29
  • 45
  • The use of `\0` is intriguing. I've not seen that before (I use `&` for the same job), but it seems to work OK with Mac OS X (BSD) `sed` — effectively as a synonym for `&`. – Jonathan Leffler Oct 08 '15 at 14:01
  • 1
    @JonathanLeffler It's fine to use `\0`, just no one does as `&` does the same. – 123 Oct 08 '15 at 14:04
  • @JonathanLeffler: I am on OSX and `\0` doesn't work, I just get `0` in output. I don't think it is portable. (It worked with gnu-sed though). – anubhava Oct 08 '15 at 14:11
  • 1
    @anubhava: Curious. _[…time passes…]_ Oh, I'm using GNU `sed` after all; it just wasn't in the directory I thought it was installed in (newer machine, I've chosen to try a different convention for where non-Apple s/w is installed). Please note that my previous comment applies to GNU `sed`; Mac OS X `sed` does not interpret `\0` as equivalent to `&`. This amply justifies my other comments about GNU `sed` not behaving the same as other variants of `sed`. – Jonathan Leffler Oct 08 '15 at 14:15
  • I've learnt something as well :-) I've never used `&` before. But I could not find any ref for the `\0` in any `sed`-related pages. But I found it on the vim-regep page http://vimregex.com/#backreferences and that is where my regexp knowledge springs... – UlfR Oct 08 '15 at 14:16