Reputation: 51
I'm having a problem with a csh script that i'm writing.
What i wanna do is to read a file, and for every new line of this file that i'm reading, assign a new value to a certain variable that i'll use later.
To make it simple I have a string, array called "zn" that contains 6 values. I can print every value using smth like:
echo ${zn[$i]}
Then i try to use the values of this array with something like (easy example just to explain):
cat file1 | awk '{i=NR;n='${zn[i]}';print n,$0}' >! file2
or other attempt:
cat file1 | awk '{n='${zn[NR]}';print n,$0}' >! file2
Well, I tried almost every possible combination of brackets, apostrophes, quotes...and I always get some errors like:
missing -.
Any help would be really appreciated, the solution it's probably smth pretty easy and obvious. (i'm sorry if my syntax is not the best but i'm kinda new to this)
EDIT: I ported the script in bash
...this is part of the script I use to prepare some text files to prepare a graphic in GMT:
cat crosspdf.dat |
awk '
BEGIN { n = int(('$dz')/('$dz_new')) }
{
z=$1
for (i=6;i<=NF;i++) {
if ($i!=0) {
for(j=1;j<=n;j++)
print (i-4)*'$dv', z+(j-n/2)*'$dz_new', $i
}
}
}
' >! temp
This works: the only thing you need to know is that $dz was a constant value, and now i wanna change it in order to have a different value for each line of the file i'm scanning. I can easily prepare the array with the values but i'm not able to pass include it somehow in the previous line. PS: thanks for the support – Francesco 2 mins ago edit
EDIT 2
1) dv, and dz_new are just parameters 2) dz would be an array with variable lenght containing just numbers (depth intervals: smth like -6.0 1.0 5.0 10.0 ... 36.0) 3) crosspdf.dat contains some histogram-like data: Each line corresponds to a different depth (depths were equally spaced, now not anymore, so that's why i need to use the dz array)
Upvotes: 0
Views: 1902
Reputation: 204310
Let's start by re-writing your script:
cat crosspdf.dat |
awk '
BEGIN { n = int(('$dz')/('$dz_new')) }
{
z=$1
for (i=6;i<=NF;i++) {
if ($i!=0) {
for(j=1;j<=n;j++)
print (i-4)*'$dv', z+(j-n/2)*'$dz_new', $i
}
}
}
'
to pass shell variable values to awk the right way and cleanup the UUOC. The above should be written as:
awk -v dv="$dv" -v dz="$dz" -v dz_new="$dz_new" '
BEGIN { n = int(dz/dz_new) }
{
z=$1
for (i=6;i<=NF;i++) {
if ($i!=0) {
for(j=1;j<=n;j++)
print (i-4)*dv, z+(j-n/2)*dz_new, $i
}
}
}
' crosspdf.dat
Now some questions you need to answer are: which of your shell variables (dv
, dz
, and/or dz_new
) is it you want to have different values for each line of the input file? What are some representative values of those shell variables? What values could crosspdf.dat
contain? What would your expected output look like?
Update your question to show some small sample of crosspdf.dat
, some settings of your array variable(s), and the expected output given all of that.
Actually - maybe this is all the hint you need:
$ cat file
abc
def
ghi
$ cat tst.sh
dz="12 23 17"
awk -v dz="$dz" '
BEGIN{ split(dz,dzA) }
{ print dzA[NR], $0 }
' file
$ ./tst.sh
12 abc
23 def
17 ghi
Questions?
Upvotes: 1