Reputation: 11
I have filenames as below(omitted the actual names) I need to extract the filename between the filename_prefix and runid Problem here being the filename changes and is 2-5 fields long. I have been using this
echo "filename_prefix_filename_filename_filename_runid_date_part-r-00020-c68fdc43-53bc-4aa9-a96b-2692ae2aa508.orc " | awk 'NR > 1 {print $1}' RS='filename_prefix' FS='runid'
This works fine on the command line but I need to pass RS and FS as variables which I am not able to do so because
echo of file name |awk -v file_p=$file_prefix -v r_id=_$RUN_ID 'NF > 1 {print $1}' RS=file_p FS=r_id
the above command doesn't work.
Filenames:
filename_prefix_filename_filename_filename_runid_date_part-r-00020-c68fdc43-53bc-4aa9-a96b-2692ae2aa508.orc
filename_prefix_filename_filename_runid_date_part-r-00020-c68fdc43-53bc-4aa9-a96b-2692ae2aa508.orc
Upvotes: 0
Views: 253
Reputation: 67567
you can do the same in bash
, although a two step process
$ f="filename_prefix_filename_filename_filename_runid_date_part-r-00020-c68fdc43-53bc-4aa9-a96b-2692ae2aa508.orc";
pre="filename_prefix_";
run="_runid*";
$ ff=${f%$run}; echo ${ff#$pre}
filename_filename_filename
Upvotes: 1
Reputation: 189948
The assignments to RS
and FS
are wrong. You cannot use Awk variables here; they are interpreted simply as static strings (which is the only arrangement which makes sense anyway).
awk 'NF > 1 {print $1}' RS="$file_prefix" FS="_$RUN_ID"
Upvotes: 1
Reputation: 532538
Because you are still setting RS
and FS
outside the awk
script itself, you use the shell variable, not awk
variables.
... | awk 'NF > 1 {print $1}' RS="$file_prefix" FS="$RUN_ID"
Alternately, you can use
... | awk -v RS="$file_prefix" -v FS="$RUN_ID" 'NF > 1 {print $1}'
or
... | awk -v file_p="$file_prefix" -v runid="$RUN_ID" 'BEGIN{RS=file_p; FS=runid}; NF > 1 {print $1}'
The last one, though, is unnecessarily indirect.
Upvotes: 1