LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Seperating variable from immediately following letter (https://www.linuxquestions.org/questions/linux-newbie-8/seperating-variable-from-immediately-following-letter-923141/)

mp85 01-10-2012 06:29 PM

Seperating variable from immediately following letter
 
I have a variable $line which i determine earlier in my script.

Now im trying to use sed to insert some text at that specific line number.

This works
Code:

sed '7i\
      blah blah' in.txt' > out.txt

But how do I do this when the 7 is a variable?

Code:

sed "$linei\
      blah blah' in.txt" > out.txt

How do I tell it that the i is not part of the variable name?


Thanks

unSpawn 01-10-2012 06:39 PM

Code:

${line}i
?

schneidz 01-10-2012 09:47 PM

sed "$line"i

David the H. 01-11-2012 10:01 AM

Unspawn's answer is the best for general use, and schneidz's post leads to a good point; that you can concatenate multiple quoting patterns together:
Code:

sed "$line"'i\
      blah blah' in.txt > out.txt


But there's also simply this:

Code:

sed "$line i\
      blah blah" in.txt > out.txt

sed doesn't care about spaces between commands.

mp85 01-11-2012 01:01 PM

Ended up using the multiquote method.


Thanks

David the H. 01-12-2012 06:37 AM

Good. I would've gone with either of the other options myself, as they tend to be easier to read, but it's your choice. If you're satisfied, please mark the thread as solved.

Check out these three links for more on how the shell parses arguments and whitespace. It's a vitally important concept when it comes to scripting.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


All times are GMT -5. The time now is 08:53 PM.