1

Sort of different request here: I have a text file in which I need to place a @ character at the beginning of each paragraph (paragraphs=separated by blank lines) that contains a certain search string. For example

hello

this
is
a
test

hi

Should yield:

hello

@this
@is
@a
@test

hi

I have the following for selective printing of a paragraph, but it won't insert the # character at the beginning of each line:

sed -e '/./{H;$!d;}' -e 'x;/SEARCH_STRING/!d;'

Any ideas? Thanks in advance!

  • Do you have acces to GNU sed? – Cyrus Jul 28 '15 at 19:02
  • i don't believe so, no. This is the sed v4.1.4 SUSE Linux, i'm open to other ways to do it also – user125343 Jul 28 '15 at 19:04
  • Why is your example gone? – Cyrus Jul 28 '15 at 19:18
  • it edited it out because it didn't come out the way i needed it to, some issue with the line feeds – user125343 Jul 28 '15 at 19:26
  • 1
    Try to prefix lines of your example with four whitespaces. – Cyrus Jul 28 '15 at 19:58
0

Isn't it largely a matter of replacing the final !d ('don't delete') with s/\n/\n@/g; ('substitute newlines by newlines plus an additional character')?

$ cat file
hello

this
is
a
test

hi


$ sed -e '1{x;$!d}' -e '/./{H;$!d;}' -e 'x;/test/ s/\n/\n@/g;' file
hello

@this
@is
@a
@test

hi

The extra expression 1{x;$!d} just prevents an additional blank line that would otherwise result from appending the first line to the (empty) hold space.

  • one last thing- is it possible to designate either a blank space OR a colon (:) to separate paragraphs? – user125343 Jul 29 '15 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.