Being cryptic about what you're really trying to do makes it harder to give you any real help. What Ravinder or drl suggested might do exactly what you want.
You could also try this alternative:
#!/bin/bash
awk '
FNR == NR {
A[$1, $2]
next
}
($2, $1) in A {
printf("echo \"string %s found with numbers %s\"\n", $2, $1)
printf("echo mv \"somefile1\" \"somefile2\" \"somefile3\" \"/some/other/directory\"\n")
delete A[$2, $1]
}' file1 file2 | bash
which produces the output:
string CC found with numbers 3
mv somefile1 somefile2 somefile3 /some/other/directory
where the echo
shown in red prints the rm
command instead of executing it. (But, of course, you have to actually have awk
create the list of files to be moved and create an mv
command for bash
to execute.
If the names of the files to be moved depend on the values from the matched lines, something more like:
#!/bin/bash
awk '
FNR == NR {
A[$1, $2]
next
}
($2, $1) in A {
print $2, $1
delete A[$2, $1]
}' file1 file2 | while IFS= read -r string number
do printf 'string %s found with numbers %d\n' "$string" "$number"
echo mv "FileContaining${string}and$number".* "/some/other/directory"
done
might be more appropriate. In a directory that contains the files:
-rw-r--r-- 1 dwc staff 0 Aug 17 09:28 FileContainingCCand3.1.txt
-rw-r--r-- 1 dwc staff 0 Aug 17 09:28 FileContainingCCand3.4.txt
-rw-r--r-- 1 dwc staff 0 Aug 17 09:28 FileContainingCCand3.7.txt
-rw-r--r-- 1 dwc staff 30 Aug 17 09:04 file1
-rw-r--r-- 1 dwc staff 15 Aug 17 09:04 file2
-rw-r--r-- 1 dwc staff 682 Aug 17 09:05 problem
-rwxr-xr-x 1 doc staff 532 Aug 17 09:28 tester
it produces the output:
string CC found with numbers 3
mv FileContainingCCand3.1.txt FileContainingCCand3.4.txt FileContainingCCand3.7.txt /some/other/directory
as long as you keep the echo
shown in red. If you remove the echo
, it would actually attempt to move those files.