Register
It is currently Sat Sep 20, 2014 10:04 pm

Appending text to a series of txt files


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu Dec 04, 2008 10:23 am   

Joined: Thu Dec 04, 2008 9:54 am
Posts: 3
Location: UK
Hi, I'm trying to append a block of text to the end of a series of text files arranged within a number of folders. The appended text also has to have some generic characters e.g., nnnnn that need to be substituted by the folder name.

I'm sure this is relatively simple to achieve in a script perhaps involving sed, but my experience is a bit limited, and I'm taking way too long at the moment!

W


Top
 Profile WWW  
 PostPosted: Fri Dec 05, 2008 8:11 am   

Joined: Tue Dec 02, 2008 8:30 am
Posts: 6
Location: Bonny Scotland
this should work
Code:
for i in `find $DIRROOT|grep ".txt"`
do
  cat $TEXTBLOCKFILE >> $i
done

where DIRROOT is the root directory containing the text files and TEXTBLOCKFILE is the file name of the
file you want to add to the end of each .txt file
you can pretty it up if you want to run the thing regularly.
as you can see, this just appends the contents of the TEXTBLOCKFILE to each file ending in .txt under the DIRROOT path

you would have to be more specific about the "generic characters" part of your request. but the following line would extract the directory in the above for loop
Code:
DIRPART=`echo $i|awk -F"/" '{ OFS="/"; $NF=""; print $0 }' `


Top
 Profile  
 PostPosted: Fri Dec 05, 2008 3:10 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
You could use sed to replace your generic characters:

[code]
sed 's/nnnnnnn/your text here/'
[code]

But like Timsoft said, we'll prolly need more information to really help :) If you've already started on something, perhaps you could post what you have so far?

-J


Top
 Profile YIM  
 PostPosted: Mon Dec 08, 2008 8:44 am   

Joined: Thu Dec 04, 2008 9:54 am
Posts: 3
Location: UK
Ok, this is what I've tried so far, and it seems to work

Code:
if [ -z $1 ]; then
  echo "Needs directory as argument."
  exit 1
elif [ -d "$1" ]; then
  dirname="$1"
fi
cd $1
list="$(ls ARCH*.test)"
echo $list
for i in $list
do
  sed -i '$a\
QUAL 6 STATUS TEST\
ARCHIVE 12\
partial k-effective\
retrieve from ./REF_INTS/nnn_testy.REFINT\
begin\
INTER x 12\
compare k-effective\
begin\
\
QUAL 3 STATUS REFERENCE\
ARCHIVE x\
partial k-effective\
store as ./REF_INTS/nnn_testy.REFINT\
begin' $i
done

for i in $list
do
  sed -i s/nnn/"$1"/ $i
done



2 more things..........
(a) I now need to strip out part of the filename,a numeric character after the word "test" in the filename, and sub that for y in text to append
(b) I need to spool through the input file, before appending to it, and find the last occurrence of a word from a look up list. When that occurs, I need to strip out the last numerical character from that line, and again sub for x in text to append. Can I use awk for that?

I guess I need to do these three amendments to the text to append before doing the appending..............


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP