Reputation: 1602
Good morning
I've been developing a script to make certain search/compare/retrieve data on Linux systems. In particular what I'm trying to cross is data from 2 files previously created by the script with the following example text:
file_1:
/dev/abcde_vg/abcde_lv APP /bbdd_abcde
/dev/abcde_vg/system_lv APP /bbdd_abcde/system
/dev/abcde_vg/data_lv DAT /bbdd_abcde/data
file_2:
/bbdd_abcde APP
/bbdd_abcde/system APP
/bbdd_abcde/data DAT
The goal here is to getting a list of the first column of the file 2 with awk:
for i in `cat file_2|awk '{print $1}'
do ..........;done
and to obtain something similar to the following statement:
awk '$3 ~ "^/bbdd_abcde$"{print $0}' file_1
/dev/abcde_vg/abcde_lv APP /bbdd_abcde
I've tried the following with no success:
for i in `cat file_2|awk '{print $2}'
do
awk -v VAR1="$i" '$3 ~ VAR1{print $0}' (or awk -v VAR1="$i" '$3 ~ "^VAR1$"{print $0}' or similars)
done
With no success at all.
Thanks for the answers, finally I applied a search for an exact match using AWK with this:
for i in $(cat file_2|awk '{print $2}')
do
awk -v VAR1="$i" '$3 == VAR1{print $0}'
done
The idea behind this is more complex, the problem itself is part of a 200+ script for crossing data using bash only
The script generates a file on each server with a format like explained before
/dev/abcde_vg/abcde_lv APP /bbdd_abcde
/dev/abcde_vg/system_lv APP /bbdd_abcde/system
/dev/abcde_vg/data_lv DAT /bbdd_abcde/data
And those files are check against a "how it should be" file on a NAS with the format:
/bbdd_ APP
/bbdd_/system APP
/bbdd_/data DAT
To verify if the "type" of disk assigned to an lv is correct. (disk types are figured out in another part of the script)
So the goal was to compare each mount point disk type "APP/DAT..." to the same in "how it should be"
the main problem for me was that searching for a specific mount point like "/bbdd_abcde" on one file with awk and "~" I got a lot of results instead of only the exact one. I needed to use "==" in my awk statement.
Thanks for the help
Upvotes: 1
Views: 306
Reputation: 17288
This solution reads the cross-reference keys (APP, DATA) from file1 into an array then prints the matches with file2.
awk 'FNR==NR{a[$2]=$1;next} $2 in a' file2 file1
Upvotes: 1
Reputation: 195289
I hope I understood your question right, try this:
awk 'NR==FNR{a[$1]=7;next}a[$3]' file2 file1
note that, there is difference between this line and your codes. in my line, I didn't do regex match. because if you read /bbdd_abcde
, with regex match checking all lines in your file1 would match. I don't know if it is expected.
If matching was desired, you can change the 2nd part of the codes.
Upvotes: 1