LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 06: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, 06:39 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Code:
${line}i
?
 
2 members found this post helpful.
Old 01-10-2012, 09:47 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
sed "$line"i
 
1 members found this post helpful.
Old 01-11-2012, 10:01 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 10:08 AM. Reason: expanded post
 
1 members found this post helpful.
Old 01-11-2012, 01: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, 06:37 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

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



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 08:36 AM
perl: how to insert numerical digit immediately after regexp backreference variable? chadwick Programming 8 05-19-2008 12:49 PM
seperating substring jkeertir Linux - Newbie 1 04-09-2008 12:37 AM
Script to move directories based on first letter to a new directory of that letter tworkemon Linux - Newbie 8 01-30-2007 07:18 PM
seperating values using perl pantera Programming 3 05-26-2004 12:31 AM

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

All times are GMT -5. The time now is 07:06 PM.

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
Open Source Consulting | Domain Registration