ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/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!
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.