How to take values from a file.?

Hi All,

As of now I am checking for a sring using hard coded values in all_data.txt.
Instead of that I have to check using from the file.

awk '$1 ~ /COMS|SMAS|MARC/ {print $1, $2, $3 > "file_" $1}' all_data.txt

The below file is having the list.

client_list
COMS
SMAS
MARC

Please help me.

Thanks.

Try

awk 'NR==FNR {a[$0]=$0;next} ($0 in a) {print $0 > "file_" $0}' pattern_file all_data.txt

Take a look at the below link for at least one approach you can take:

clx and mjf,
Note that $1 in a and the discussion in the referenced thread provide a test for exact matches, but $1 ~ /COMS|SMAS|MARC/ is testing for substring matches. In other words, if the first field in the input contains the substring COMS or the substring SMAS or the substring MARC , it will match even if it is not one of those three exact strings.

To get what was requested here, we would need something more like:

awk '
FNR == NR {
	e = (FNR == 1) ? $1 : (e "|" $1)
	next
}
$1 ~ e {if($1 in f)
		print $1, $2, $3 >> f[$1]
	else {	f[$1] = "file_" $1
		print $1, $2, $3 > f[$1]
	}
	close(f[$1])
}' client_list all_data.txt

Note that the close() is needed because since it is performing a substring match, even if there are only three lines in client_list , there could be thousands of output files created.

Of course, it is entirely possible that ROCK_PLSQL wants exact matches, but that is not what the code supplied in post #1 in this thread does.

1 Like

Don,
I completely missed that subtlety on substring matches!

This solution appears to work as well:

$cat all_data.txt                                                                           
COMS 1111 AAAA
SMAS 2222 BBBB
MARC 3333 CCCC
MARCX 4444 DDDD
XCOMS 5555 EEEE
YYYY 6666 FFFF COMS
ZZZZ 7777 GGGG
$cat pattern_file                                                                           
COMS
SMAS
MARC
 
$nawk 'NR==FNR {a[$1];next} {for (i in a) if ($1 ~ i) {print $0}}' pattern_file all_data.txt
COMS 1111 AAAA
SMAS 2222 BBBB
MARC 3333 CCCC
MARCX 4444 DDDD
XCOMS 5555 EEEE