LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2012, 06:20 AM   #1
jkobori
LQ Newbie
 
Registered: Sep 2012
Location: Budapest, Hungary
Distribution: Ubuntu Hardy Heron 8.04.3, Ubuntu x64 12.04
Posts: 23

Rep: Reputation: Disabled
awk variable


Hi everybody,

I have the following code:

#!/bin/bash
.
.
.
for ((i=0;i<$detval-1;i++))
do
t="${array[i+1]}"
cat "${array[k]}"n"${array[i+1]}".d64 | awk '{print $t}' > fg.dat
done

The script supposed to cd to a particular directory, cat a file, then choose one column (t), and print it to fg.dat.
The problem is, that if I use "$t" in the awk, it does not do what I want it to do,
I guess beacuse I actually should use "$($t)". Of course, I've tried it, also cut -f $t,
but none of them has worked.
Can anyone help me out?
Thank you very much!

Regards,
Joe
 
Old 10-21-2012, 07:35 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

awk has a separate variable system from the shell, and shell values need to be passed into awk variables before they can be used internally. This is usually done with the "-v" option.

Also, you have a Useless Use Of Cat, and you don't need the extra quotes around the constructed filename.

Code:
#!/bin/bash
.

for ((i=0; i<$detval-1 ; i++)); do

	t="${array[i+1]}"
	awk -v var="$t" '{print $t}' "${array[k]}n${array[i+1]}".d64 > fg.dat 

done

In the future, if you want more detailed help, you really need to supply some sample input data as well for us to work with. It's hard to figure out exactly what's happening from just the code alone.


Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/
 
Old 10-21-2012, 09:20 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
One might even hope that you explain what your doing and doing what you explain.

You mention that the code is to cd into a directory, yet I see no evidence in the code presented (explained but not doing)

You also have some mysterious variables 'detval' & 'k' which are never defined or changed so seem pointless (doing but not explained)

And as David aptly pointed out, without any example data it is difficult to understand what you require as you have only shown what does not work.
 
Old 10-21-2012, 10:11 AM   #4
jkobori
LQ Newbie
 
Registered: Sep 2012
Location: Budapest, Hungary
Distribution: Ubuntu Hardy Heron 8.04.3, Ubuntu x64 12.04
Posts: 23

Original Poster
Rep: Reputation: Disabled
Here you go:

Code:
#!/bin/bash

grbno=`cat ftest.cat | wc -l`   

read -a y <<< `cat ftest.cat | awk '{print $1}'`
read -a m <<< `cat ftest.cat | awk '{print $2}'`
read -a d <<< `cat ftest.cat | awk '{print $3}'`
read -a s <<< `cat ftest.cat | awk '{print $4}'`
read -a pt <<< `cat ftest.cat | awk '{print $5}'`
read -a at <<< `cat ftest.cat | awk '{print $6}'`



        for ((i=0;i<$grbno;i++))
                do


                                cd /data/fermi/programs/supplementary_programs
                                read -a tomb <<< $(head -1 "${y[$i]}${m[$i]}${d[$i]}${s[$i]}".dat)
                                let detval="${#tomb[@]}"

                                cd /data/fermi/"${y[$i]}${m[$i]}${d[$i]}${s[$i]}"/d64
k=0

                                for ((i=0;i<$detval-1;i++))
                                        do

read -a y <<< `cat /data/fermi/programs/ftest.cat | awk '{print $1}'`
read -a m <<< `cat /data/fermi/programs/ftest.cat | awk '{print $2}'`
read -a d <<< `cat /data/fermi/programs/ftest.cat | awk '{print $3}'`
read -a s <<< `cat /data/fermi/programs/ftest.cat | awk '{print $4}'`
read -a pt <<< `cat /data/fermi/programsftest.cat | awk '{print $5}'`
read -a at <<< `cat /data/fermi/programs/ftest.cat | awk '{print $6}'`

                                                t="${tomb[i+1]}"
                                                cat "${tomb[k]}"n"${tomb[i+1]}".d64 | gawk -v r=$t '{print $(r+1)}' > fg.dat

                                        done

                done
Thank you David the H., I have to use the -v option, now it works!
 
Old 10-21-2012, 12:09 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
So I am curious ... have you just found here strings and want to try them out? Because if not, that looks like a whole lot of hard work.

I won't go through all of it, but just looking at the outer loop and the over use of arrays, you could simply do:
Code:
while read -r y m d s pt at
do
    <your stuff here>
done<ftest.cat
The above replaces:
Code:
grbno=`cat ftest.cat | wc -l`   

read -a y <<< `cat ftest.cat | awk '{print $1}'`
read -a m <<< `cat ftest.cat | awk '{print $2}'`
read -a d <<< `cat ftest.cat | awk '{print $3}'`
read -a s <<< `cat ftest.cat | awk '{print $4}'`
read -a pt <<< `cat ftest.cat | awk '{print $5}'`
read -a at <<< `cat ftest.cat | awk '{print $6}'`



        for ((i=0;i<$grbno;i++)) # this is the outer loop
                do
done
I maybe could have provided more, however, your use of head and multiple changes of directory are a bit confusing. So I leave it with you ... of course if your current method
is working and you are happy then all good

Last edited by grail; 10-21-2012 at 12:10 PM.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
get variable out from awk sharathkv25 Programming 1 05-08-2007 06:33 PM
awk on a variable onradius Programming 8 02-22-2006 08:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:23 AM.

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