merging files and add missing rows

hello all,
I have files that have a specific way for naming the first column
they are make of five names in Pattern of 3
Y = [james mark john peter sam] (no case sensitive)
so the files are names $Y-$Y-$Y or $X-$Y-$Z depending how we look
they only exist of the pattern exist
now I want to create a file from them that if a specific patter for that column didnt exist it will write the pattern and write "NA" for the rest of the columns

so basically I should have 125 lines and if the match exist it will write itself
else print NA

for X in james mark john peter sam
do
	name=$(basename $X) 
	if [ -s $X ]
  	then
grep  count/$X-$X-$X file01  | awk �{print $0}� >> file02.txt
else
echo  "count/$X-$X-$X NA NA NA NA" >> file02.txt
fi
done

I am doing this but its not working not sure what I am doing wrong :wall::wall::wall:
can you please help

Show your input files and desired output. Your above description looks lit bit :confused:...

I have this now which print the 125 right but all with NA :frowning: and dont get the pattern right

for X in james mark john peter sam 
do
for Y in james mark john peter sam
do
for Z in james mark john peter sam 
do
name=$(basename $X) 
	if [ -s $X ]
  	then
grep  -o count/$X-$Y-$Z file01  | awk �{print $0}� >> file02.txt
else
echo  "count/$X-$Y-$Z NA NA NA NA" >> file02.txt
fi
done 
done 
done

Please check below..

Assuming you are searching for pattern "$X-$Y-$Z"

for X in james mark john peter sam 
do
for Y in james mark john peter sam
do
for Z in james mark john peter sam 
do
if [[ `grep "$X-$Y-$Z" -c file01` -gt 0 ]]
then
grep  -o "$X-$Y-$Z" file01  >> file02.txt  
else
echo  "$X-$Y-$Z NA NA NA NA" >> file02.txt
fi
done 
done 
done

yap, if the line exist it only prints
count/$X-$Y-$Z and not the rest of the row
I can see now that GREP -O is not correct for start
solved I guess ... it should be -w instead of -o to get the whole line :slight_smile:

but dont know y I dont get 125 for all the files in one directory

remove -o

grep  "count/$X-$Y-$Z" file01  >> file02.txt  

have you checked for any entries with NA.. in output file.

1 Like
FILES01="F1/*"
for T in $FILES01 
do
name=$(basename $T) 
for X in james mark john peter sam 
do
for Y in james mark john peter sam
do
for Z in james mark john peter sam 
do

if [[ `grep "$X-$Y-$Z" -c $T` -gt 0 ]]
then
grep  -w "count/$X-$Y-$Z" $T >>  normal/$name
else
echo  "count/$X-$Y-$Z" >> normal/$name
fi
done 
done 
done
done 

there is no NA in any of the files
even without -w or -o i still dont get 125 ... i get different rows for different files :frowning:
I think i found the problem

1 Like

oops ... just missed the file name :smiley:
good catch :slight_smile:

1 Like