1

Currently, the file a.txt has:

#define ABC

I want to change a.txt to:

#define ABC
#define DEF

And I tried

sed 's/#define ABC/#define ABC&\n#define DEF/g' a.txt > tmp/test.txt
mv tmp/test.txt > a.txt

but it's giving me #define ABC#define ABCn#define EDF instead. Tried multiple solutions online but it's not working. What is wrong with my solution? Thanks!!!

Working on Solaris 11

Dao Lam
  • 2,837
  • 11
  • 38
  • 44
  • 1
    http://stackoverflow.com/a/16246806/632407 – clt60 Jul 02 '13 at 21:37
  • @jm666 i don't see how that answered my question. Believe me when I said I already tried many solutions and couldn't get it to work. This one was closest to my case: http://stackoverflow.com/questions/682984/shell-adding-a-new-line-between-a-given-line-of-text and it didnt work for me, possible because I have # or something, I don't know. – Dao Lam Jul 02 '13 at 21:45
  • 1
    Why not append a line? `echo '#define DEF' >>a.txt` – potong Jul 03 '13 at 05:50

5 Answers5

3

Try something like:

$ echo '#define ABC' | sed '/ABC$/ a\
> #define DEF'
#define ABC
#define DEF

Note: Tested on Oracle Solaris 10 9/10 s10s_u9wos_14a SPARC

jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • Thank you! This works. I essentially did something similar to your solution to get it to edit my file: `sed 's/#define ABC/#define ABC \ >#define DEF /' a.txt > new.txt ` – Dao Lam Jul 02 '13 at 23:24
2

Code for GNU :


$echo #define ABC|sed 's/.*/&\n#define DEF/'
#define ABC
#define DEF
captcha
  • 3,756
  • 12
  • 21
  • Thanks for the answer. I'm using sed on Solaris so the above command gave me back nothing. Any ideas? – Dao Lam Jul 02 '13 at 22:31
2

Nothing but your '&' was the problem.

try this

sed 's/#define ABC/#define ABC \n#define DEF/g' a.txt

#define ABC
#define DEF

I tested in my unix box and it's working fine

So your final solution should be like this

sed 's/#define ABC/#define ABC \n#define DEF/g' a.txt
a.txt > tmp/test.txt
mv tmp/test.txt > a.txt
Mariappan Subramanian
  • 9,527
  • 8
  • 32
  • 33
  • Thank you. I tried the same thing before but it didn't work. In the end, I figured that it's because I was using bash on Solaris. Apparently, the above code works for bash on Linux but not Solaris. Same as to sed \a (to append). Thanks though, your solution should work if I was on Linux. – Dao Lam Jul 03 '13 at 16:19
1

The second example on the cited link is a solution for you

< a.txt sed "/ABC$/a \\
#define DEF
"

will produce from the a.txt

#define ABC

the output

#define ABC
#define DEF

Or the 5th example from the above link

< a.txt sed "/ABC$/s/$/\\
#define DEF/"

so, READ!

clt60
  • 62,119
  • 17
  • 107
  • 194
  • Could you please explain? I tried both ways and they didn't work. a.txt remains the same. – Dao Lam Jul 02 '13 at 22:07
  • Care the downvoter tell me what is wrong with the code? both example add the wanted line to the output. – clt60 Jul 03 '13 at 16:53
0

Standard Solaris sed needs a line break set as below:

echo "#define ABC" | sed 's/#define ABC/#define ABC\
#define DEF/'

\n or \x0A don't work on SunOS.

access_granted
  • 1,807
  • 20
  • 25