LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-16-2012, 08:01 AM   #1
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Rep: Reputation: 18
how to pass this as a variable


Hi all,

Assume that i am having the below

"/*
* ProGuard -- shrinking, optimization, obfuscation, and preverification
* of Java bytecode.
*
* Copyright (c) 2002-2011 Eric Lafortune (eric@graphics.cornell.edu)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/"

I just want all these in to a single variable.Your suggestions please.

Thanks in advance,
Dinesh.
 
Old 01-16-2012, 08:08 AM   #2
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 528

Rep: Reputation: 78
If you are using bash you can do this:
Code:
#!/bin/bash

VAR='/*
* ProGuard -- shrinking, optimization, obfuscation, and preverification
* of Java bytecode.
*
* Copyright (c) 2002-2011 Eric Lafortune (eric@graphics.cornell.edu)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/'

echo $VAR

Last edited by angel115; 01-16-2012 at 08:11 AM.
 
1 members found this post helpful.
Old 01-16-2012, 08:17 AM   #3
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Original Poster
Rep: Reputation: 18
Hi,

But this didn't work for me.

while doing echo i am geeting the files of / directory.
 
Old 01-16-2012, 08:22 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

The echo statement needs double quotes.
Code:
echo "$VAR"
Hope this helps.
 
1 members found this post helpful.
Old 01-16-2012, 08:52 AM   #5
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Original Poster
Rep: Reputation: 18
Hi Thanks,

That works. I want this variable to be inserted at the top of a file.

I tried using sed -i 1i\"$VAR" file

But this does not work.At present i am trying to use this varaiable in sed.Your suggestions also would be helpful and ofcourse i am trying from my side as well.
 
Old 01-16-2012, 08:52 AM   #6
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 528

Rep: Reputation: 78
Sorry, I've forgot the quote.
 
1 members found this post helpful.
Old 01-16-2012, 09:29 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

I'm not sure if this can be done using sed and a variable that holds multiple lines.

Here's an alternative:
Code:
echo "$VAR" | cat - file > /tmp/outfile && mv /tmp/outfile file
Hope this helps.
 
2 members found this post helpful.
Old 01-16-2012, 10:03 AM   #8
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Original Poster
Rep: Reputation: 18
Thanks druna,

But will this create any problem for files which contains large number of lines.
 
Old 01-16-2012, 10:10 AM   #9
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
This one liner works just fine when using sed

Quote:
sed -e "1 i \ $VAR" file
 
Old 01-16-2012, 10:14 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,
Quote:
Originally Posted by dinakumar12 View Post
But will this create any problem for files which contains large number of lines.
Not likely. It might get problematic if the file is several gigabytes large and you don't have sufficient memory (incl. swap).

Hope this helps.

@cbtshare: Doesn't work on my side:
Code:
$ sed -e "1 i \ $VAR" file
sed: -e expression #1, char 10: unknown command: `*'
 
Old 01-16-2012, 10:19 AM   #11
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
what version of sed do you have?
 
Old 01-16-2012, 10:19 AM   #12
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Original Poster
Rep: Reputation: 18
oh,

Thanks druna,but sed needs to have some sort of option right or whether it is possible through awk.

@cbtshare,

My sed version is 4.1.5.

---------- Post added 01-16-12 at 08:50 PM ----------

Last edited by dinakumar12; 01-16-2012 at 10:21 AM.
 
Old 01-16-2012, 10:24 AM   #13
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
I have :GNU sed version 4.2.1

So if doesnt work
Quote:
sed -e "1 i \ $VAR" file
try escaping the space dinakumar12 so use \\

Last edited by cbtshare; 01-16-2012 at 10:26 AM.
 
Old 01-16-2012, 10:34 AM   #14
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@cbtshare: Tried it with these versions:
Code:
$ sed --version
GNU sed version 4.1.5

$ sed --version
GNU sed version 4.2.1
And both fail with the same error (from a script and from the command line).

Did you try with a dummy variable or one filled with the above content?
 
Old 01-16-2012, 10:41 AM   #15
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
tried it at the command line, by entering random text to fill VAR:

Code:
[root@server1 Desktop]# sed -e "1 i \ $VAR" hello.txt
 SFSDSDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDSDFGDGHGFHFGHFFFFFFFFFFFFFFH45555555555555555555555555555555555555555555555555555555555555555555555555555555
two

Last edited by cbtshare; 01-16-2012 at 04:01 PM.
 
  


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
Pass Variable to smbclient artvarck Linux - Newbie 5 02-23-2010 01:49 PM
pass the variable contain lrios Programming 7 04-18-2008 05:56 PM
How to pass mysql query to a variable? chynna_v Programming 4 09-03-2004 06:09 AM
How do I pass a C variable to a Bash command ? Linh Programming 6 07-07-2003 04:12 PM
Pass text to variable Zed Linux - Software 6 05-12-2003 04:02 PM


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