LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   trying to read external file & extract a column value to use in function within a do done loop (https://www.linuxquestions.org/questions/linux-general-1/trying-to-read-external-file-and-extract-a-column-value-to-use-in-function-within-a-do-done-loop-4175691802/)

TempleStone4510! 03-09-2021 11:00 AM

trying to read external file & extract a column value to use in function within a do done loop
 
Hi,

Have an issue with some scripting:
*******************************************************
#!/bin/bash
proj=w
cd /mnt/$proj/_04Images/
cat tmp00.txt
while read line
do
grep -H aaa ..etc etc.. >> tmp01.txt
grep -H bbb ..etc etc.. >> tmp01.txt
cat tmp01.txt | tr -d '\n' | awk '{printf ("%s", $0)}'>> tmp02.txt
cat tmp02.txt | awk 'BEGIN {i=(((18000*cos(-1*(atan2(8196908-8148966,585323.3-576355.2)))+576355.2))); printf (" %s\n", i)}'>> tmp02.txt

rm -f tmp01.txt
done <tmp00.txt
*******************************************************

Script runs fine & generates a multi-column file tmp02.txt:
FileName09.tif.ccc -1 -511 48799 261 0 576355 8148966 48795 585323 8196908 579665
FileName33.tif.ccc -1 -511 48799 261 0 576355 8148966 48795 585323 8196908 579665

*******************************************************
From above, I am manually generating the last column (with the atan2 function) 579665 & adding in newline (for next do-done loop).
The 579665 value is made (above cos/atan2 function) with columns of tmp02.txt (after line: cat tmp01.txt | tr -d '\n' | awk '{printf ("%s", $0)}'>> tmp02.txt)

What I'm trying to do is replace the manual numbers with that of the file to make the 579665 value. E.G. wanting to change the script line from:

cat tmp02.txt | awk 'BEGIN {i=(((18000*cos(-1*(atan2(8196908-8148966,585323.3-576355.2)))+576355.2))); printf (" %s\n", i)}'>> tmp02.txt

to

cat tmp02.txt | awk 'BEGIN {i=(((18000*cos(-1*(atan2($11-$8,$10-$7)))+$7))); printf (" %s\n", i)}'>> tmp02.txt

But the above does not input that column (e.g. $11 should be 8196908 from file tmp02.txt), but I think is either thought of as number 11 (or 0). Have tried to do other things (e.g.
using the "cut" to extract the 8196908
cat tmp02.txt | awk 'BEGIN {i=(((18000*cos(-1*(atan2($(cut tmp02.txt -f 11)-8148966,585323.3-576355.2)))+576355.2))); printf (" %s\n", i)}'>> tmp02.txt


But it keep failing.

Any straight-forward fix?

I know my script is probably not efficient etc. But am wanting to just try and get it to work

shruggy 03-09-2021 12:09 PM

Quote:

Originally Posted by TempleStone4510! (Post 6228837)
cat tmp02.txt | awk 'BEGIN {i=(((18000*cos(-1*(atan2($11-$8,$10-$7)))+$7))); printf (" %s\n", i)}'>> tmp02.txt

The BEGIN block in awk gets executed before the first line of the file is read. $0 and the field variables ($1, $2, etc.) simply don't exist at this point. Get rid of BEGIN
Code:

awk '{print $0,18000*cos(-atan2($11-$8,$10-$7))+$7}' tmp01.txt >>tmp02.txt
Quote:

I know my script is probably not efficient
The script definitely looks over-complicated. It would be easier, if you provided some context, a sample of input data, and explained what you're trying to achieve. Then somebody here could suggest how to improve it.

Particularly,
Code:

| tr -d '\n' | awk '{printf ("%s", $0)}'
looks redundant as tr and awk seem to do the same—remove newline characters.

I guess these two lines
Code:

cat tmp01.txt | tr -d '\n' | awk '{printf ("%s", $0)}'>> tmp02.txt
cat tmp02.txt | awk 'BEGIN {i=(((18000*cos(-1*(atan2(8196908-8148966,585323.3-576355.2)))+576355.2))); printf (" %s\n", i)}'>> tmp02.txt

could be replaced with one awk expression. And using the FILENAME variable in awk could probably eliminate the two grep lines before that as well.


All times are GMT -5. The time now is 04:09 PM.