Bash of records into file

Hi All,

Seeking for your assistance to check if there a file PROS containing on the file1.txt and display the missing PROS file.

cat file1.txt
049|0276
230|0457
094|0134
094|1268
080|0445
049|1896
cd home/sysad
PROS0490276042017.TXT
PROS2300457042017.TXT
expected output:
missing PROS File:
PROS0940134
PROS0941268
PROS0800445
PROS0491896
so far what i did was awk the file1.txt
list=`awk -F "|" '{print $1$2}' file1.txt`

i don't know if read line is the correct code
while read line
do
 ls PROS$list*
 <i don't know next if there's a non matching file how do i print it.>

Please advise,

Thanks,

Try

ls home/sysad/PROS* | awk 'NR == FNR {T[substr($1, 5, 7)] = $1; next} !($1$2 in T) {print "PROS" $1 $2}' FS="|" - file1
PROS0940134
PROS0941268
PROS0800445
PROS0491896
1 Like

Hi RudiC,
I think solution need 6 character to extract instead 7

substr($1, 5, 7)

Thanks
Pravin

1 Like

Hi Sir RudiC,

Would you mind explaining it to me?

Thanks,

Hello znesotomayor,

Could you please try following and let me know if this helps.

awk 'BEGIN{print "missing PROS File:"} {gsub(/\|/,X,$0);system("find . -type f -iname *" $0 "* > temp_file");file="temp_file";Q=getline<file;if(!Q){print "PROS" $0};print Q;if(Q){system("rm temp_file")}}'  Input_file

EDIT:Also in above command you could change command to as follows too where you could mention the path from where you want to look for the files.

awk 'BEGIN{print "missing PROS File:"} {gsub(/\|/,X,$0);system("find /singh/is/king/path -type f -iname *" $0 "* > temp_file");file="temp_file";Q=getline<file;if(!Q){print "PROS" $0};print Q;if(Q){system("rm temp_file")}}'  Input_file

Thanks,
R. Singh

by the way i'm using shell. my output is different.

---------- Post updated at 07:22 PM ---------- Previous update was at 07:12 PM ----------

Hi Sir R. Singh,

Please see output below:

[dbadmin@BIDBPD1 test]$ awk 'BEGIN{print "missing PROS File:"} {gsub(/\|/,X,$0);system("find . -type f -iname *" $0 "* > temp_file");file="temp_file";Q=getline<file;if(!Q){print "PROS" $0}} END{system("rm temp_file")}'  file1.txt
missing PROS File:
PROS2300457
PROS0940134
PROS0941268
PROS0800445
PROS0491896
[dbadmin@BIDBPD1 test]$ cat file1.txt
049|0276
230|0457
094|0134
094|1268
080|0445
049|1896

PROS2300457 is included although there's already a file on it.

Thanks,

---------- Post updated at 07:39 PM ---------- Previous update was at 07:22 PM ----------

Hi Sir Pravin,

I don't see an output on my side. is there a difference if my file directory is different?

Please advise,

Thanks,

Those "PROS" files are listed and piped into awk , which collects them into the T array. Then file1 is read and each line's values compared against the indices of the T array. If not found, "PROS" plus the values are printed.

thanks Sir RudiC.

what's the meaning of this? specially on the substring part?

awk 'NR == FNR {T[substr($1, 5, 7)] = $1; next} !($1$2 in T)

substr() is a built-in function in AWK, to extract a consecutive sequence of characters from inside a string.

substr($1, 5, 7) extracts from field 1, 7 characters, starting at character 5.

NR == FNR   ## Apply the action to only the lines from first file
{T[substr($1, 5, 7)] = $1;...}  ## Create an associative array which index is the return from substr(), and the value is the first field.
{...; next}  ## Skip to next line
!($1$2 in T)  ## execute {...} for any line from second file if the concatenation of field one and two is not an index found in the associative array T.
1 Like

Thanks Ms. Aia. i now get it.

BR,
-nik