Operating System - HP-UX
1833464 Members
2684 Online
110052 Solutions
New Discussion

Re: need help with shell scripting 101 class

 
SOLVED
Go to solution
Randy Hagedorn
Regular Advisor

need help with shell scripting 101 class

Hi,

I am a novice at HP-UX shell scripting. I need help with the syntax of the following.

File A, contains "Buggers".

I want file B with the results of "Huggers Buggers". How would I write a script to concatenate "Huggers " with "Buggers"?

Thanks,
Randy
9 REPLIES 9
Hein van den Heuvel
Honored Contributor

Re: need help with shell scripting 101 class


First lesson:

review your questions from the audience perspective. In doing so, you may actually solve the problem.

In this case... where is 'Huggers' coming from? File C? Every other lines in file A?


You might not needs a scripts... the 'paste' command may well do just what you need.

Cheers,
Hein.
Pete Randall
Outstanding Contributor

Re: need help with shell scripting 101 class

Try:

cat Huggers Buggers > HuggersBuggers


Pete

Pete
Randy Hagedorn
Regular Advisor

Re: need help with shell scripting 101 class

To claify my question.

I want to 'hardcode' "Huggers " before whatever value is in file A, so that "Huggers " always preceeds the contents of File A and is written to File B.

Thanks,
Randy
Bob Smith_23
Advisor
Solution

Re: need help with shell scripting 101 class

sed 's/^/Huggers /' FileA > FileB

sed 's/Buggers/Huggers Buggers/' A > B
A. Clay Stephenson
Acclaimed Contributor

Re: need help with shell scripting 101 class

# Pure Shell solution

OFILE="FileB"
echo "Huggers \c" > ${OFILE}
cat FileA >> ${OFILE}
If it ain't broke, I can fix that.
Biswajit Tripathy
Honored Contributor

Re: need help with shell scripting 101 class

Solution 1)

while read -r line
echo "Huggers $line"
done < FileA > FileB

Solution 2)

cat FileA | sed 's/^/Huggers //' > FileB

- Biswajit
:-)
Jim Butler
Valued Contributor

Re: need help with shell scripting 101 class

If buggers is the only thing in file a,

cat filea | while read line
do
var1=$line
echo "Huggers $line"
done

If buggers is field 3 in file a,
lets say filea has the following text

bob has buggers the size of idaho
sally has BugGers that are smaller

Then you want to pick the third word out of file a

cat filea | while read line
do
word=`awk '{print $3}' $line`
echo "Huggers $line"
done

would output
Huggers buggers
Huggers BugGers

Don't forget the shebang line is the first line of your script, and specifies the shell


she - bang #! - get it?
(The exclamation point is the BANG, the pound sign is the comment shhhh)

good luck
Man The Bilge Pumps!
Hein van den Heuvel
Honored Contributor

Re: need help with shell scripting 101 class

>> I want to 'hardcode' "Huggers " before whatever value is in file A, so that "Huggers " always preceeds the contents of File A and is written to File B.

Ah! Clear:

# cat > a
aap
noot
mies
# perl -ne 'print "Huggers $_"' a > b
# cat b
Huggers aap
Huggers noot
Huggers mies

or

# awk '{print "Huggers " $0}' a
Huggers aap
Huggers noot
Huggers mies


Cheers,
Hien.


Randy Hagedorn
Regular Advisor

Re: need help with shell scripting 101 class

Thanks all for you participation. It's obvious that there is more than one way to do things in Unix.

It seems that I need to take a class in scripting and / or the command language.

Thanks,
Huggers Buggers