insert a line with variables using sed

Hi,
I have to insert a line having variables using sed. But the variables are not getting substituted within sed.

Ex:

n=2
sed $n' i\
hi' file

This works. But the below code does not work.

n=2
line=hello
sed $n' i\
$line' file

The above code inserts '$line' in the 2nd line of the file; instead I need to insert the value of $line, which is 'hello'

I even tried double quotes but it does not work.

n=2
line=hello
sed $n" i\
$line" file

Please help.

  1. Please, use
    ~~~
    text
    ~~~
    tags, not [quote] [/quote] tags for code and listings
  2. Did you read the explanation in your other thread on how the quotes work?

They are two different variable substitutions in sed. The first post was for inserting a line using sed at a specific line number, which was a variable.

Now, I am trying to insert a line, which has a variable in it, using sed. I did try the double quotes pointed out by you, but it did not work.

n=2
line=hi
sed $n" i\
$line" file

The error that I get is "sed: 0602-404 Function 2ihihihihi cannot be parsed."

Which sed on which platform?

AIX version 5.3
Even if you have any other solution other than sed, pls let me know

Ah, apparently AIX sed is more POSIX strict than GNU sed. Try escaping the backslash so that it gets passed literally, instead of escaping the newline:

n=2
line=hi
sed $n" i\\
$line" file

You are a UNIX Genius!!!
Thank you very much :slight_smile:

a. I try to attention use CODE tags.Sometimes I can make mistake like everybody.Couldnt it?
b. Explanation isnt unneccessary for my solution.
Please look at the page sed.sf.net - The SED $HOME

Thanks @ygemici
Regards

edit by bakunin: actually pludi wasn't talking to you but to sugan, so your complaint looks a bit off the mark. Furthermore, it might be my general dullness, but i can't discern any meaningful from the sentence "explanation isn't unnecessary for my solution". Care to explain that? I also don't understand how the link to the sourceforge project is going to help the thread opener.

I'll give you the benefit of doubt that maybe your post was intended for another thread, where it might make perfect sense and was just erroneously posted here. Otherwise i would have to classify it as bordering on spam. Thanks for clarifying.

---------- Post updated 04-02-10 at 11:39 AM ---------- Previous update was 04-01-10 at 05:39 PM ----------

Dear bakunin

I am in server world from fifteen years.I am computer and system engineer.
I dont understand to your approach to me?

I dont have to explain anybody especially to suspicious approach persons.

Anyway I think the other people wants to see my explanation :

sed ${linenumber}i\ "Insert Your Line" file
$\{linenumber\}  --> value of linenumber same as $linenumber and i use to \{\} for sed i option to isolated from the expanded shell variable that is only differerence ..

I hope my explanation is enough..
Thanks for your precision
Have a nice day
Regards

The note about the code tags wasn't targeted at you, but a note to sugan, a new user. Just like bakunin pointed out.

Which leads to the question: which solution? I've looked through the thread twice, and this is your very first post here. No solution given so far, so probably that's why you don't think an explanation is needed.

Kutlama, but on this forum it's not important how long you've been in the computing world, but how well you can solve a problem, communicate it, and maybe even educate inexperienced users. If you neglect to read a thread, and then start to post aggressive answers it is within our rights to reprimand you. And what are "suspicious approach persons"?

Nice solution, only that it only works with GNU sed, and the thread starter is on AIX and doesn't have that version installed.

If I have mistake any i am sorry

Have a nice day everybody