Hi,
here's my - not so easy to describe - problem: I want to compare the values of one file (FileA) with a cutoff-value and, if this comparison is true, substitute those values with those in the second file (FileB). However, there are many FileA's (FileA[1->200]), whereas there is only one FileB. Every FileA has three lines, each containing one value.
The three values of FileA1 should now be compared line-by-line with the cutoff-value. If true, the corresponding value of FileB should be assigned. However, those corresponding values are contained within a line of FileB. So I now need some kind of script which substitutes line x of FileA[1] (if value > or < cutoff) with the field in line[1] and cloumn x.
My script so far:
# getting number of line of FileB in which values of FileA are contained
n=`echo "$1" |sed 's/.*\([0-9]\{1,3\}\).*/\1/'`
# comparison and substitution
awk -v val=$n '{
getline < "$1"
for(i=1; i<=NF; i++){
if($i >= 3.5){
print $i
}
else{
getline < "FileB.txt"
NR==n {print $i}
}
}
} ' $1 FileB.txt > $1_new.txt
since i'm a beginner in awk, it's very intuituve aaaand - of course - doesn't work.
The script should chek FileA[1] for a line containing a value greater or equal to a value, in this example 3.5. If this is true, the original value should be printed, if it's false, this specific value (in this example in line 1 of FileA[1]) should be replaced with the corresponding value in FileB, here line1 (since its FileA[1]), column 1 (first value in FileA). Again, notice that the corresponding values are listed in one column in FileA and one line in FileB.
the code works, though I now have some new problems
i don't really understand it (but i'll try to)
the code only works when i paste it into the shell, not when i try to run the script (but that's not the main point)
i have to type in all FileA's (>200) consecutively (s. 4.) (also not the main point)
i can't compare one FileA individually: if I take FileA[145], the values (if necessary) become substituted with those of line 1 from FileB, not with line 145
Hi Klashxx,
I took your code and modified it slightly for my purposes. The files indeed have a pattern in their naming: [0-9]{1,3}[A-Z]{3}. Your code gives me correct output solely for FileA's, in which the comparison is true.
For FileA no. 2 however, I always get the same output: in case of comparison is true, i get the three values contained in this file plus as much "2"s as FileB has lines. In case that the comparison is not true, i get as many empty lines as FileB has lines.
Do you have any explanation for this finding?
cheers,
waddle
Hi Klashxx,
I applied exactly your code, it just didn't work out for me. I still got some incorrect output with a lot of empty lines. It's ok though, I solved it with another (more artless) bash-script.
Thanks anyway!