I have a file that I am performing some replacement of certain columns in two new files. It works fine right now.

I wanted to put some of the variables like the "X" (the character I replace a column with) and "\036" separator, the columns selected like $13, $6,$10,$5, etc into a configuration file kind of like below. I am not sure how to accomplish this the best way with an awk statement.


export FIELD_DELIMITER="\036"
export MASK_COLUMN="$13"
export PRINT_COLUMN="$13, $6, $10, $5"


awk 'BEGIN{FS=OFS="\036"} {gsub(/./, "X", $13)} 1' $1 > $file_directory'/'$mask_filename$seperation$temp$DATA_SUFFIX
awk 'BEGIN{FS=OFS="\036"} {print$6,$10,$5,$13}' $1 > $file_directory'/'$mask_filename$seperation$temp1$DATA_SUFFIX

You can pass awk variables from outside by using the -v argument. You can use it repeatedly to pass multiple variables.

For example, for the first line of your script:

awk -v FIELD_DELIMITER='\036' -v WIPEOUT_CHARACTER='X' 'BEGIN{FS=OFS=FIELD_DELIMITER} {gsub(/./, WIPEOUT_CHARACTER, $13)} 1' $1 > $file_directory'/'$mask_filename$seperation$temp$DATA_SUFFIX

I checked GNU Awk 4.1.4 and it's happy to take the "\036" and interpret that as ASCII RS (record separator), so I'd expect that should work for you too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.