Visit Our UNIX and Linux User Community

match string in a file to file in a directory

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting match string in a file to file in a directory
# 1  
Old 01-12-2008
match string in a file to file in a directory

Hello awk gurus,

I have a text file (st15.txt) containing many station ids.
And, I want to match each station number to individual files in a directory, and then
I would like to copy that file into a different folder if any station id matches to a file.
For example,
"ls *.DAT" shows all files in the current directory.

If you look at "st15.txt", ONLY 4 stations are matching the files in the current directory. Thus,
033445 matches Q033445.DAT
234567 matches X234567.DAT
012345 matches Q012345.DAT
222345 matches Q222345.DAT

So, all these 4 *.DAT files should be copied into a new directory.
I think that awk would be the solution, but I don't know how to attack this problem.

# 2  
Old 01-12-2008
this is without awk, will not perform well if the st15.txt is having a huge list of Ids. anyway

$ cat st15.txt

$ ls thedir/
Q012345.DAT Q033445.DAT Q123456.DAT Q222345.DAT X234567.DAT X345678.DAT

$ cat st15.txt | while read line; do cp thedir/*$line.DAT newdir/. ; done

$ ls newdir/
Q012345.DAT Q033445.DAT Q222345.DAT X234567.DAT
# 3  
Old 01-12-2008

while read id
  if [ -f ./thedir/Q$id.DAT ]; then
    cp ./thedir/Q$id.DAT /to/new/directory
done < st15.txt

# 4  
Old 01-12-2008
ls *.DAT | awk 'BEGIN{
 while( ( getline line < "file" ) > 0  ) {
  if ( substr($0,2,6) in keys ) {
    cmd = "cp \047" $0 "\047 \047" newdir "\047"
    print cmd
    #system(cmd) #uncomment to copy

# 5  
Old 01-12-2008
Another one:

(IFS=$'\n';cp $(printf "[QX]%s.DAT\n" $(<st15.txt)) dest)

If your shell doesn't expand $'\n' to a newline, use:


Last edited by radoulov; 01-12-2008 at 06:44 AM..
# 6  
Old 01-19-2008
Problem was solved.

cat st15.txt|while read line; do cp $(printf "[QX]%s.DAT" $line) ./dest;done

Thanks all!!

Previous Thread | Next Thread
Test Your Knowledge in Computers #181
Difficulty: Medium
The Macintosh project began in 1979 when Apple employee Jef Raskin envisioned an easy-to-use, low-cost computer for the average consumer.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to move specific files to directory based on match to file

I am trying to mv each of the .vcf files in the variants folder to the folder in /home/cmccabe/f2 that the .vcf id is found in file. $2 in file will always have the id of a .vcf in the variants folder. The line in blue staring with R_2019 in file up to the -v5.6 will always be an exact match to a... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Display match or no match and write a text file to a directory

The below bash connects to a site, downloads a file, searches that file based of user input - could be multiple (all that seems to work). What I am not able to figure out is how to display on the screen match found or no match found" and write a file to a directory (C:\Users\cmccabe\Desktop\wget)... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

Match string in two files and add data to one file

Gents, file1 S 65733.00 19793.00 1 0 318592.8 2792489.5 29.1063000008 S 65733.00 19801.00 1 0 323120.8 2789153.6 13.3063000044 S 66009.00 19713.00 1 0 318672.7 2792538.2 30.6063000120 S 65801.00 19799.00 1 ... (2 Replies)
Discussion started by: jiam912
2 Replies

5. Shell Programming and Scripting

Match string from two file input

Hello all, I have file like this: file 1: aa bb cc dd ee file2: 111 111 111 111 111 111 (2 Replies)
Discussion started by: attila
2 Replies

6. Shell Programming and Scripting

check if a given string is a file or directory

hi i want to know how to do this if the given is /tmp/ and it is a valid directory then it will echo directory if the given is /tmp/file.txt and is a valid file then it will echo file.. thanks! (5 Replies)
Discussion started by: h0ujun
5 Replies

7. Shell Programming and Scripting

Need Help - match file name and copy to Directory

I am trying to sort the following files from folder Bag to Apple, Cat Food, Dog Food. I can get all of the files I want into a new folder, but not sure of the best approch to get them to their final directory My Files ========== apple.1234.ext apple.1235.ext cat food 101.ext Cat Food... (2 Replies)
Discussion started by: mtschroeder
2 Replies

8. UNIX for Dummies Questions & Answers

How to Find a String in Each File in a Directory

Hi, I have the ff. list of files in the dir named /home/joule/unix/archive: $ /home/joule/unix/archive joule1.gz joule2.gz joule3.gz joule4.gz joule5.gz joule6.gz joule7.gz joule8.gz What I would like to do is to find this string in a group of files in the directory above: String to... (1 Reply)
Discussion started by: Joule
1 Replies

9. Shell Programming and Scripting

Fetch the rows with match string on a fixed lenth text file - NO delimiters

Hi I am trying to fetch the rows with match string "0000001234" Input file looks like below: 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1... (6 Replies)
Discussion started by: nareshk
6 Replies

10. Shell Programming and Scripting

How do I search a File for a string exact match

Hi, Can you help please. I have the following comand: if ]; then l_valid_string="Y" fi The problem I am trying to solve is that my l_string = ABC and my file contains ABC ABC_EFG I only want back the value ABC exact match. (3 Replies)
Discussion started by: CAGIRL
3 Replies

Featured Tech Videos