Converting awk script from bash to csh

I have the following script set up and working properly in bash. It basically copies a set of lines which match "AS1100002" from one file and replaces the same lines in another file.

awk -vN=AS1100002*  'NR==FNR { if($1 ~ N)K[$1]=$0; next }
{ if($1 in K) $0=K[$1]; print }' $datadir/file1 $datadir/file2  > $datadir/file2.new

But in csh I get the following error when running:

Unmatched '.

Any ideas on how to fix this issue?

Thanks!

That should not be the case nor should it happen, unless you copied/paste the code you submitted: When using GUI the selection of 2 lines is understood "AS" 2 lines and will put a en of line and the end of first line... You remove it and Im sure it works... e.g. in vi you use J to join both lines...

awk -vN=AS1100002*  'NR==FNR { if($1 ~ N)K[$1]=$0; next }\
 { if($1 in K) $0=K[$1]; print }'\
 $datadir/file1 $datadir/file2  > datadir/file2.new

It works fine on the command line. I get the error when running the script.

In csh the backslash should be outside the 'string'

echo \
'part 1'\
'part 2'

--
You certainly want -vN=AS1100002 i.e. without trying to match files in the current directory or allowing several 2 at the end.

1 Like

When adding in a backslash

awk -vN=AS1100002* 'NR==FNR { if($1 ~ N)K[$1]=$0; next }'\
'{ if($1 in K) $0=K[$1]; print }' $datadir/file1 $datadir/file2 > $datadir/file2.new

I get

awk: No match.

Just tested on AIX:

awk -vN=AS1100002*  'NR==FNR { if($1 ~ N)K[$1]=$0; next } { if($1 in K) $0=K[$1]; print }'\
 $datadir/file1 $datadir/file2  > $datadir/file2.new

works...

So what is in your csh script then? If you doubt about the awk line, just put that in a file and execute it with csh:

 csh -x file 

Just realised:

awk: No match.

Means thats its working... Only awk found no match...

1 Like

'No match' is because of the * too many.
I added that in my last post.
BTW

set nonomatch

makes csh behave more like sh/bash.

1 Like