LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-10-2012, 07:29 PM   #1
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Rep: Reputation: Disabled
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
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-10-2012, 07:39 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
Code:
${line}i
?
 
2 members found this post helpful.
Old 01-10-2012, 10:47 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,167

Rep: Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890
sed "$line"i
 
1 members found this post helpful.
Old 01-11-2012, 11:01 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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.

Last edited by David the H.; 01-11-2012 at 11:08 AM. Reason: expanded post
 
1 members found this post helpful.
Old 01-11-2012, 02:01 PM   #5
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Ended up using the multiquote method.


Thanks
 
Old 01-12-2012, 07:37 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make shell variable don't expand immediately linuxgentoo Programming 1 06-13-2009 09:36 AM
perl: how to insert numerical digit immediately after regexp backreference variable? chadwick Programming 8 05-19-2008 01:49 PM
seperating substring jkeertir Linux - Newbie 1 04-09-2008 01:37 AM
Script to move directories based on first letter to a new directory of that letter tworkemon Linux - Newbie 8 01-30-2007 08:18 PM
seperating values using perl pantera Programming 3 05-26-2004 01:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration