LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-09-2021, 11:00 AM   #1
TempleStone4510!
LQ Newbie
 
Registered: Mar 2019
Posts: 6

Rep: Reputation: Disabled
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
 
Old 03-09-2021, 12:09 PM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by TempleStone4510! View Post
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.

Last edited by shruggy; 03-09-2021 at 02:00 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Spreadsheet question find value for column b based on max value in column k davholla General 2 02-28-2014 03:50 AM
How to display 2 different column field values as one column value in mysql VijayaRaghavanLakshman Linux - General 2 04-16-2012 09:56 AM
how to sort the 2nd column on the basis of first column without repeating the value ? zediok Linux - Newbie 15 12-20-2011 11:48 AM
difference between value *value and value * value PoleStar Linux - Newbie 1 11-26-2010 03:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration