Operating System - HP-UX
1834276 Members
2527 Online
110066 Solutions
New Discussion

Re: simple scripting help

 
SOLVED
Go to solution
Doug_3
Frequent Advisor

simple scripting help

Hi all, I need a quick tip on scripting this:
I want to add one line of text to a flat file as a new first row and save the resulting file with the same name. My meager attempts at cat result in the text at the end of the file. I've tried various incarnations of cat and redirection with no success. I don't want to use cp or mv.

Text to add is 6-8 char long as a variable.

New Data:
$mydata = "mytext"

Original File:
$filename

Contents of $filename:
line1 of orig file
line2 of orig file
line3 etc etc etc

Desired contents resulting file to be saved as $filename:
mytext
line1 of orig file
line2 of orig file
line3 etc etc etc

Thanks in advance,
Doug
5 REPLIES 5
Pete Randall
Outstanding Contributor

Re: simple scripting help

Doug,

There's certainly more elegant ways to do this, but a simple approach like this should work:

echo $mydate > somefile
cat $filename >> somefile
mv somefile $filename


Pete


Pete
Patrick Wallek
Honored Contributor

Re: simple scripting help

Well, this is fairly dirty, but it would work:

#!/usr/bin/sh

mydata="mytext"

echo $mydata > /tmp/file

cat /tmp/file $filename > /tmp/file1

mv /tmp/file1 $filename
rm /tmp/file

Sundar_7
Honored Contributor

Re: simple scripting help

Hi Doug,

# echo $mydata > /tmp/newfile
# cat $filename >> /tmp/newfile
# mv /tmp/newfile $filename

--Sundar
Learn What to do ,How to do and more importantly When to do ?
john korterman
Honored Contributor

Re: simple scripting help

Hi,

maybe you can use this:

# TEXT="Hej Doug"
# echo "$TEXT" |cat - $filename

regards,
John K.
it would be nice if you always got a second chance
Dave La Mar
Honored Contributor
Solution

Re: simple scripting help

Doug-
Just answered similar yesterday i.e.
#!/usr/bin/ksh
# Test a prepend to file
# 1i means insert at line one.

echo "Keep on keeping on." | read STRING_TO_INSERT
string="$STRING_TO_INSERT"

ed << EOF >/dev/null
e /home/dlamar/work/test1.fil
1i
$string
.
w /home/dlamar/work/test1.fil
q
EOF

Best regards,
dl
"I'm not dumb. I just have a command of thoroughly useless information."