Need to replace one string, in a file, with another string from a different file ...

For the following code:

awk -F":" -v OFS=":" 'NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1]}1'  shadow2 shadow1 > shadow3 

I get syntex error (random one ) as below:

awk: NR==FNR{a[]=;next}a[]{=a[]}1
awk:           ^ syntax error
awk: fatal: invalid subscript expression

What might be the potential cause?

---------- Post updated 09-15-09 at 12:28 AM ---------- Previous update was 09-14-09 at 11:40 PM ----------

I just noticed that when I create a bash script using the HERE-TAG(from within another bash script) ; in the following manner:

#!/usr/bin/env bash
# Bash_script001.sh

   echo " Inside Bash_script001.sh";
   ...
   ...
   ...

cat>>"Bash_script002.sh"<<EOF
#!/usr/bin/env bash
# Bash_script002.sh

   echo " Inside Bash_script002.sh";
   ...

   awk -F":" -v OFS=":" 'NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1]}1' shadow2 shadow1 > shadow3;
   ...

   ...

EOF

   ....
   echo " Again inside Bash_script001.sh processing rest of Bash_script001.sh ";

Now when the 'Bash_script002.sh' gets created and as it contains the awk script which inturns contains variables $1, $2 as array a[] indexes; the resultant 'Bash_script002.sh' contains only the following:

awk -F":" -v OFS=":" 'NR==FNR{a[]=;next}a[]{=a[]}1' shadow2 shadow1 > shadow3;

Thats why I'm receiving the syntex error.

Will you please suggest, what can be done to avoid getting the above erronious 'awk' statement in the resultant 'Bash_script002.sh' ?

Thanks in advance and my sincere appreciation for the support so far I received in this forum from all.

hi Praveen for the time being declare a variable
#!/usr/bin/env bash
# Bash_script001.sh

echo " Inside Bash_script001.sh";
s="$"
...
...
...

cat>>"Bash_script002.sh"<<EOF
#!/usr/bin/env bash
# Bash_script002.sh

echo " Inside Bash_script002.sh";
...

awk -F":" -v OFS=":" 'NR==FNR{a[${s}1]=${s}2;next}a[${s}1]{${s}2=a[${s}1]}1' shadow2 shadow1 > shadow3;
...

...

EOF

....
echo " Again inside Bash_script001.sh processing rest of Bash_script001.sh ";

for the time being u can use this...this would work fine hopefully....

To keep the forums high quality for all users, please take the time to format your posts correctly.

  1. Use Code Tags when you post any code or data samples so others can easily read your code.
    You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags and by hand.)
  2. Avoid adding color or different fonts and font size to your posts.
    Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
  3. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply With Quote

I thought "we" had already fixed it here?

that was good reply vgersh...i never knew this...thanks...

Hey Fellas.

I am new to scripting. After searching the forums, this is about the closest to what i'm looking for. I am trying to find a particular sting in a file and replace the next line, roughly about 1500 times in the file. I have been trying to do this with sed, but i haven't had the best of luck. The string that i need to replace with is in another file in csv format. example:
first found instance in file1 is replaced with line 1 in csv file
second found instance in file1 is replaced with line 2 in csv file and so on.
When i say i'm brand new to scripting i mean BRAND new. I'm taking some classes, but i need to get this done as soon as possible and what i have learned so far hasn't gotten me much closer. any help ya'll can give would be greatly appreciated.
Thanks!