Delete all files if another files in the same directory has a matching occurence of a specific word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete all files if another files in the same directory has a matching occurence of a specific word
# 1  
Old 11-19-2009
Delete all files if another files in the same directory has a matching occurence of a specific word

Hello,
I have several files in a specific directory.
A specific string in one file can occur in another files.
If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string.

Example.
file1
ShortName "Blue Jeans"
price 89.47
cur EURO
file2
ShortName "Blue Jeans"
Price 59.47
CUR USD
file3
ShortName "Blue Jeans"
Price 99.47
CUR GBP
Since the value of ShortName "Blue Jeans" is occuring in file2 & file3. Both this file should be deleted. Similarly files with other ShortName
Could any one please help me how can it done via script (ksh, SED, AWK). I am on solaris.

Thank you
# 2  
Old 11-19-2009
How about:
Code:
awk '/ShortName / && A[$0] { print FILENAME } {A[$0]=1}' infile* | xargs rm

you can use
Code:
| xargs echo rm

to see if the awk script has the desired result first.
# 3  
Old 11-20-2009
Thank you.
I was trying but some how the script does not work.

awk '/ShortName/ && A[$0] {print FILENAME} {A[$0]=1}' infile* | xargs echo rm

Could you please tell me what this subscript A[$0] and A[$0]=1 means
$0 represent the complete line.
A[$0]=1 assignment

this statement work
awk '/ShortName/ {print FILENAME, $2}' file1
where $2 the value of ShortName
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete all the files and folders inside all the directories except some specific directory?

hi, i have a requirement to delete all the files from all the directories except some specific directories like archive and log. for example: there are following directories such as A B C D Archive E Log F which contains some sub directories and files. The requirement is to delete all the... (7 Replies)
Discussion started by: Little
7 Replies

2. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

3. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory RSK_123_20141113_031500.txt RSK_123_20141113_081500.txt RSK_126_20141113_041500.txt RSK_126_20141113_081800.txt RSK_128_20141113_091600.txt Here, "RSK" is file prefix and 123 is a code name and rest is just timestamp of the file when its... (7 Replies)
Discussion started by: kridhick
7 Replies

4. Shell Programming and Scripting

Delete all files with specific extension in directory tree

I'm sure this has been asked many times, but a search didn't turn up a definitive best method for this (if there ever is such a thing). I have been using rsync to back up my main data directory, but I have accumulated a large number of older backups that I don't need. All of the files I don't... (14 Replies)
Discussion started by: LMHmedchem
14 Replies

5. Shell Programming and Scripting

How to delete last occurence of word using Linux command?

Hi all, I am trying to delete last occurrence of word using sed command. for example. I have input like this on a.id1 = b.id1 and on a.id2 = b.id2 and on a.id3 = b.id3 and and I am expecting output like this on a.id1 = b.id1 and on a.id2 = b.id2 and on a.id3 = b.id3 I just need to... (11 Replies)
Discussion started by: nsk
11 Replies

6. UNIX for Dummies Questions & Answers

List all files with sum of matching word

grep -c 'avihai' 1.log will give me count of 'avihai' in log I want to have a list of files in the folder that show file name with count side by side. Please advice (2 Replies)
Discussion started by: avihaimar
2 Replies

7. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

8. Shell Programming and Scripting

Delete all occurence of a word in one shot

i have a file called file1 cat file1 i am namish namish lives in India India and namish both are good. I want to delete all the occurences of namish in one shot,if i do it with sed i guess all the lines will be deleted containing the pattern.Suggest me any idea without AWK. Thanks... (6 Replies)
Discussion started by: namishtiwari
6 Replies

9. Shell Programming and Scripting

delete files in specific directory

i have a directory "ABC" with lots of old files and sub directories in it. the issue now is i want to delete away files which are older than 15 days in "ABC" without deleting the files in the sub directories and without deleting the sub directory. i tried using find command but it will drill down... (2 Replies)
Discussion started by: legato
2 Replies
Login or Register to Ask a Question