LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-02-2015, 03:07 PM   #1
bishop2001
Member
 
Registered: Apr 2015
Posts: 58

Rep: Reputation: Disabled
insert variable top of file


greetings,
I'm trying to insert a variable to the top a file like below, although the variable isnt being interpolated. Suggestions please? Thanks,

STRING="Top Line Of The File"
sed -i '1i"$STRING" Today\' file
 
Old 12-02-2015, 03:33 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Code ...
Code:
echo; echo "InFile" ...; cat $InFile; echo "End Of File($(wc -l <$InFile) lines)"
STRING="New Top Line"
sed -i "1i$STRING Today\\" $InFile 
echo; echo "InFile" ...; cat $InFile; echo "End Of File($(wc -l <$InFile) lines)"
Result ...
Code:
InFile ...
apple
banana
cherry
End Of File(3 lines)

InFile ...
New Top Line Today
apple
banana
cherry
End Of File(4 lines)
Daniel B. Martin
 
Old 12-03-2015, 04:41 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
INSERT_THIS='-n Anything $ \ can be `here/ so forget sed(1)'

(printf "%s\n" "$INSERT_THIS"; cat "file") >tmpfile.$$
mv tmpfile.$$ "file"
 
Old 12-03-2015, 05:55 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by bishop2001 View Post
STRING="Top Line Of The File"
sed -i '1i"$STRING" Today\' file
Code:
sed -i '1i"$STRING" Today\' <= you must not escape the closing '
# between ' ' the variables will not be expanded, you ought to use " instead:
sed -i "1i\"$STRING\"" file
# but probably you will not need that \" pair at all.
Finally as it was mentioned, you do not need sed, just something like this:
Code:
(echo $FIRST_LINE; cat file) > new_file
 
Old 12-03-2015, 06:28 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
(Finally, as it was mentioned, echo(1) should be avoided, use printf(1))
 
  


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
Insert lines on top of a text file micronemo Linux - Newbie 10 08-09-2012 05:14 PM
Insert message contain "!?><$%" to variable or file DoME69 Programming 15 05-19-2011 04:22 AM
insert lines into a file after calculating where to insert xonar Programming 13 12-24-2009 04:37 AM
insert character into a variable jadeddog Programming 4 11-04-2008 03:39 PM
insert at the top of a file saavik Programming 7 08-29-2008 06:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:42 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