0

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)

Francesco
  • 51
  • 4
  • 1
    Do not write csh scripts or you will frequently find yourself stating `I'm having a problem with a csh script that i'm writing.`. Google "csh why not" for the excellent reasons. – Ed Morton Sep 11 '13 at 15:31
  • Thanks for your "2cents" ...appreciate also hints about what could i do to process huge text files without compiling. The problem is that often in some branches of physics you are somehow forced to work on old codes...what would you say if i'd tell u that i have to code fortran77 ??? Btw....still need to solve that. :-) – Francesco Sep 12 '13 at 06:56
  • Just write shell scrips in any bourne-derived shell, e.g. ksh, bash, or zsh instead of [t]csh. For text processing, write your scripts in awk. – Ed Morton Sep 12 '13 at 11:47
  • I moved the script to bash...same problem remains in a similar form: `cat file1 | awk '{n='${array['NR']}';print n,$0}' > file2` when i print n it's only using the 1st element of the array... – Francesco Sep 12 '13 at 11:57
  • Not a huge surprise since the shell part will be expanded before the awk script executes so you'll get the same value as if you did `echo "${array[any_unset_symbol]}"` in the shell, i.e. the same as `echo "${array[0]}"`. The answer is to pass the array contents to awk in a variable or a here document or a file. Show us some sample values of the array and sample input file and expected output and we can help you. – Ed Morton Sep 12 '13 at 12:08
  • Ok this is part of a 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 Sep 12 '13 at 12:14
  • Please update your question instead of trying to put code or any formatted text in a comment as it's very hard to read there. – Ed Morton Sep 12 '13 at 12:16
  • sorry, i'm new here. edit done – Francesco Sep 12 '13 at 12:21
  • No problem. I updated it to format your script to be readable. When posting questions, readability is important if you want people to take the time to try to understand your question and help you. I'll post the start of an answer now so we can continue this discussion there and I can update the answer as you answer questions about what you're trying to do. – Ed Morton Sep 12 '13 at 12:27

1 Answers1

1

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?

Ed Morton
  • 188,023
  • 17
  • 78
  • 185