문제

I have two files: file1 and file2 as follows:

file1                    file2
orangejuice              orangejuice_9.88_9.88
pineapplejuice           appleslices_6.3_2.2
appleslices              pineapplejuice_1.2_3.9
Mangojuice               Mangojuice_5.55_5.55

The output should be:

orangejuice_988
pineapplejuice_120_390
appleslices_630_220
Mangojuice_555

While reading line by line from file1, search for the pattern found in the line of file1 in file2, once found compare the 2nd and 3rd fields of file2 , if they are the same print it once, if not print the two numbers.(The number should be multiplied by 100 anyways)

I thought of this:

while read -r -u 3 line1
do
nawk ' "$line1" print $0}' file2.txt
if "$2" == "$3"
then echo "scale=2;$2*100" |bc
else echo "$2_$3"
fi
done 3<file1.txt

So, I want to know if the logic is correct and fix the multiplication that would give me 988 instead of 988.0.

도움이 되었습니까?

해결책

One way with GNU awk:

$ awk 'NR==FNR{a[$0];next}($1 in a){print $1,$2==$3?$2*100:$2*100OFS$3*100}' FS=_ OFS=_ file1 file2
orangejuice_988
appleslices_630_220
pineapplejuice_120_390
Mangojuice_555

다른 팁

Your logic is much easier to express purely in Awk. Assuming the role of file1 is to limit the data from file2 to only some records (your example does not seem to demonstrate this), try something like this.

awk -F _ '# Make OFS equal to FS
    BEGIN { OFS=FS }
    # Read file1 into a[]
    NR==FNR { a[$0]++; next }
    # If we fall through to here, we are in file2
    # On any lines where $1 is in a[]
    a[$1] {
        if ($2==$3) print $1, $2*100;
        else print $1, $2*100, $3*100;
    }' file1 file2
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top