Operating System - HP-UX
1820254 Members
2566 Online
109622 Solutions
New Discussion юеВ

Re: Appending Text to End of File

 
nagendra_3
Advisor

Appending Text to End of File

Hi,
I have 10 text files in a directory. I need to apped some text( in file x) to all the files. COuld you please help me achieve this,

I am doing it this way

cat file >> * . This is creating a file *.

Your help is greatly appreciated.

Thanks,
Nag
Knowledge is Power
10 REPLIES 10
Alexander M. Ermes
Honored Contributor

Re: Appending Text to End of File

Hi there.
Tried this before ?

cat filex >> file1
cat filex >> file2
.....


Rgds
Alexander M. Ermes
.. and all these memories are going to vanish like tears in the rain! final words from Rutger Hauer in "Blade Runner"
TommyT
Valued Contributor

Re: Appending Text to End of File

Hi Nag!
Do a script that you can call appendfile.sh

then:
cat filex >> file1
cat filex >> file2
cat filex >> file3

and so on....

chmod 755 appendfile .sh
then execute the file.
./appendfile.sh


//Tommy
tompa
Graham Cameron_1
Honored Contributor

Re: Appending Text to End of File

You can avoid repitition by using a loop, eg

for f in f1 f2 f3 f4 f5 f6 f7 f8 f9 f10
do
cat x >> $f
done

Where f1-f10 are your file names.
Or, if your 10 files patch a pattern, (eg *.txt), use

for f in *.txt
do
cat x >> $f
done

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Michael Schulte zur Sur
Honored Contributor

Re: Appending Text to End of File

Hi,

the shell does no expansion of things like * after a redirection >. Graham has the simplest solution.

greetings,

Michael
John Meissner
Esteemed Contributor

Re: Appending Text to End of File

Give this a try

#!/usr/bin/ksh
filedir=/dir_where_files_are

files=$(ls $filedir | grep -v fileX)

cat $filedir/fileX >> $files
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: Appending Text to End of File

ignore the post above... this one is better

#!/usr/bin/ksh
filedir=/dir_where_files_are

files=$(ls $filedir | grep -v fileX)
for i in $files
do
cat $filedir/fileX >> $i
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: Appending Text to End of File

Ok... it's either early or I'm retarded :) I forgot to put a "done" as the last line of the script above. It should be

ignore the post above... this one is better

#!/usr/bin/ksh
filedir=/dir_where_files_are

files=$(ls $filedir | grep -v fileX)
for i in $files
do
cat $filedir/fileX >> $i
done
All paths lead to destiny
James Lynch
Valued Contributor

Re: Appending Text to End of File

Nag,

The simplest way to append a string of test to the end of an existing file is to use the echo command.

echo "text to append" >> file

Using the cat command requires that you create a new file with your text to append.

If the directory contains just the 10 files that you want to append to, then you can do the following:

for i in *
do
echo "text to append" >> $i
done


JL
Wild turkey surprise? I love wild turkey surprise!
Michael Schulte zur Sur
Honored Contributor

Re: Appending Text to End of File

Hi,

if the problem is solved, could you spare some points from the endless supply of points, HP has, for those, who could help you? ;-)

greetings,

Michael
Bruno Ganino
Honored Contributor

Re: Appending Text to End of File

Negendra, i hope that i have understood....
Considering that the files are only 10, i think to commands manually.
With the command "vi" (or "ed" or "ex") you extract the part of text from the fileX and saves it in fileY.
# vi fileX

Then with the command "cat" you append fileY to all the files.
# cat filey > > file1
# cat filey > > file2
# cat filey > > file3
(...repeat for all files)

You can avoid repetition by using a script with cicle "for".
Like explanation by others answers previous (of others user).

HTH
Bruno
Torino (Turin) +2H