Merging folder of files according to name and contents.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging folder of files according to name and contents.
# 1  
Old 02-02-2011
Merging folder of files according to name and contents.

I have absolutely no idea how to do this and everything I have done doesn't even come close.

Here's the scenario:

There are a number of files in a folder named in this manner:

agd.txt
stv.txt
frk.txt
dqp.txt

There is also a series of other files in the same folder with like file names but with a .doc extension. The catch here is that most but not all .txt files have a corresponding .doc file.

agd.doc
stv.doc
dqp.doc

What has to happen here is that each .doc file has to be scanned for specific lines that contain a word. Let's use "rabbit" as an example. It may be in the form of rabbit, rabbits, _rabbit, xxxrabittxyz, RABBIT, Rabbit, etc.

If any lines in agd.doc contain rabbit, in any form, those specific lines need to be appended to the bottom of the agd.txt.

This has to happen for each matching .doc and .txt file.

If there is a frk.txt but no frk.doc or a frk.doc but no frk.txt then nothing is to be processed for that particular file name.

Once a .doc file has been tested and the corresponding .txt file has been appended the .doc file needs to be deleted, even if rabbit was not found in the .doc. At the end of the routine the only .doc files that should be left are those that did not have a matching .txt file to append to.
# 2  
Old 02-02-2011
please try and see if it's ok since no test on you data:
Code:
for i in `ls *.doc| sed 's/.doc//'`
do
  if [ -f $i.txt ]
  then
   awk 'NR==FNR && /rabbit/ {a=a"\n"$0} NR!=FNR {print FNR==1?a"\n"$0:$0}' $i.doc $i.txt >tmp
   cat tmp >$i.txt
   rm $i.doc tmp
  fi
done

note: before you run it, please have you data get a copy

Last edited by yinyuemi; 02-02-2011 at 09:36 PM..
# 3  
Old 02-02-2011
lets see if this helps.
first how do I know the "word like rabbit" ??
I will assume that we have a file with the key word in it 1 per line.
Code:
for y in `ls -1 *.doc`
 do
        b_name=`basename $y .doc`
        if [ -f $b_name.txt ]; then
            egrep -f <file of key words> -i $y >> $b_name.txt
        fi
done


Last edited by aix-guy; 02-02-2011 at 10:27 PM.. Reason: forgot the tags
# 4  
Old 02-02-2011
yinyuemi ,

I have tested your script and it works as desired except that it places the lines before the first line of the target file. I need the lines inserted after the last line of the file.
# 5  
Old 02-03-2011
try:

Code:
for i in `ls *.doc| sed 's/.doc//'`
do
  if [ -f $i.txt ]
  then
   awk 'NR==FNR && /rabbit/ {a=a"\n"$0} NR!=FNR {print $0}END{print a}' $i.doc $i.txt >tmp
   cat tmp >$i.txt
   rm $i.doc tmp
  fi
done

# 6  
Old 02-03-2011
yinyuemi,

Okay, your second script performs correctly and places the lines below the current last line in the target file. Now I will research your script and figure out what everything does so I can learn from it. Thank you very much for your help.




aix-guy,

Concerning your script .... am I to assume <file of key words> is a file I create with a list of words and if any of those words are found in a line that line gets appended to the .txt file? I ask because I am not getting your script to do anything and I do have a file that lists rabbit as a word to be found.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Best way to move the contents of a folder to another one

what is the best way to move the contents of a folder to another one without deleting the structure of the first one. the contents could include subfolder too. both folder, the source-folder and the target-folder are on the same host. any idea is appreciated . (7 Replies)
Discussion started by: andy2000
7 Replies

2. Shell Programming and Scripting

Remove folder contents

for dir in BKP/*/ do echo You are in :$dir done O/P -- BKP/201448/ BKP/201449/ BKP/201450/ BKP/201451/ BKP/201452/ BKP/201501/ BKP/201502/ BKP/201503/ BKP/201504/ BKP/201505/ BKP/201506/ BKP/201507/ (3 Replies)
Discussion started by: rocking77
3 Replies

3. UNIX for Dummies Questions & Answers

Loop for file merging in a folder

Dear all, I have a few files in a folder (lets say 5) and each have a few lines in them. I want to create merges of each of them with the other ones e.g. I need the following merges I tried to write a loop and started with combinations of 2, I get the write file names but for each... (17 Replies)
Discussion started by: A-V
17 Replies

4. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

How to copy all the contents of a list of files present in a folder to a particular file?

Hi All, I want to copy all the contents of a list of files in a folder to a particular file. i am using following command: cat dir/* >> newFile.txtIt's not working. Could you please help? Thanks, Pranav (3 Replies)
Discussion started by: Pranav Bhasker
3 Replies

6. Shell Programming and Scripting

copy folder and its contents to another folder

Hi experts, I am coming to you with this basic question on copying a folder and its content from one location to another folder using PERL script. This is my requirement. I have a folder AB under /users/myhome I want to copy AB and its contents to /user/workspace. Finally it should... (1 Reply)
Discussion started by: amvarma77
1 Replies

7. UNIX for Dummies Questions & Answers

Replacing a particular string in all files in folder and file contents

I need to replace all filesnames in a folder as well as its content from AK6 to AK11. Eg Folder has files AK6-Create.xml, AK6-system.py etc.. the files names as well as contents should be changes to AK9-Create.xml, AK9-system.py etc All files are xml and python scripts. ---------- Post... (0 Replies)
Discussion started by: Candid247
0 Replies

8. Shell Programming and Scripting

Merging files based on the contents

Hi, I have a file f1 having the contents as below select (<condn>) from dual I have another file f2 having the contents as below 1, 2, 3 I want to replace <condn> in f1 with the contents of f2 I tried using sed like this sed "s:<condn>:`cat f2`:g" f1 The above command resulted in sed:... (3 Replies)
Discussion started by: mr_manii
3 Replies

9. UNIX for Dummies Questions & Answers

copy folder contents

I need to make a new dir in side the dir lab5 the new dir is called testLab5 without changing directories copy all files from your lab5 directory into your testLab5 directory then i have to without chaning directories and using exactly one command remove all files that start with the... (1 Reply)
Discussion started by: robsk8_99
1 Replies

10. UNIX for Dummies Questions & Answers

Folder Contents

Hi, I'm trying to allow people to access the contents of a folder on a web site, I am automatically placing files in this folder for people to download. I'm using Apache on Mac OS X, if that makes a difference. Can anyone help with this? I've found no documentation on this so far... ... (6 Replies)
Discussion started by: spencer
6 Replies
Login or Register to Ask a Question