LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-10-2007, 02:20 PM   #1
jimmy512
Member
 
Registered: Oct 2006
Location: London
Distribution: Arch
Posts: 183

Rep: Reputation: 31
Bash Script Variable Problem


Hi. I have got a bit of a problem that I need some help with, and it has been bugging me all afternoon. I need to add the contents of a variable to every line in a text file, using a bash script. For example, if the original file contained:

Code:
World
World
and the variable contained something like "Hello", how would I go about producing something like this:

Code:
Hello World
Hello World
(I am not really concerned about the space in the middle.)

It has to use a variable, because the variable is taken from what the user types in to the computer.

I have so far tried using:

Code:
cat $VAR file1 | cat > file2

Returning:

Hello World
World
But I need the Hello on every line.

I have also tried:

Code:
sed 's/^/$VAR/g'

Returning:

$VAR World
$VAR World
I'm sorry if my question is a bit vague, but I really need help with this, and have tried everything I can think of. Thanks, in advance, for any advice or help given. It is greatly appreciated.
 
Old 05-10-2007, 02:31 PM   #2
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Code:
$ cat file
World
World
$ var="Hello"
$ sed "s/^/$var /" file
Hello World
Hello World
$ awk '$1=v$1' v="$var " file
Hello World
Hello World
 
Old 05-10-2007, 02:35 PM   #3
drawde83
Member
 
Registered: May 2006
Location: New Zealand
Distribution: ubuntu, gentoo at uni
Posts: 31

Rep: Reputation: 15
seems like a pretty trivial problem for something like perl
I'd write a solution but my morning coffee hasn't kicked in yet :-)
if no one else replies I'll whip something up.

lol someone else did :-) looks much simpler than what I was suggesting ;-)

Last edited by drawde83; 05-10-2007 at 02:37 PM.
 
Old 05-11-2007, 02:52 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
billym.primadtpdev>ls -1 | perl -pe 'print "hello "'
hello access.conf
hello httpd.conf
hello httpd.conf-example
hello httpd.conf.bak
hello jserv.conf
hello jserv.properties
hello magic
hello mime.types
hello srm.conf
hello zone.properties
to replace in the file and backup

perl -pi.bak -e 'print "hello "' file

Last edited by bigearsbilly; 05-11-2007 at 02:53 AM.
 
Old 05-11-2007, 07:32 AM   #5
jimmy512
Member
 
Registered: Oct 2006
Location: London
Distribution: Arch
Posts: 183

Original Poster
Rep: Reputation: 31
Thanks for the quick replies everyone. Sorry, I'm not very experienced at writing scripts and the like. This is the first script I have written which actually does something useful. I will try all of the suggested solutions and see if they work. Thanks everyone.
 
Old 05-11-2007, 07:38 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
or even...

xargs -n1 echo hello < file


I'll shut up now
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Reloading a variable in bash script GSMD Programming 4 04-16-2007 01:42 AM
[SOLVED] issue with variable in bash script angel115 Programming 4 08-21-2006 01:42 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM
Bash script renaming a variable zael Programming 3 09-30-2003 04:37 AM

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

All times are GMT -5. The time now is 03:32 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
Open Source Consulting | Domain Registration