Search code examples
bashawksyntax-error

AWK syntax error when the variable output has a full stop in it


I am trying to add this variable (it takes the batch job number and extracts the ID from a text file, with one ID being a new row) so that it can be inputted into the different locations as a variable.

I want this variable outcome (the file ID) to then be added to the start of every entry of the second column of a file.

This is part of the code on LSB (it is working slightly different on that supercomputer than slurm):

FILE_ID=$(sed -n "{$LSB_JOBINDEX}p $IDs_file)

awk -v OFS="\t" FILE_ADD=$FILE_ID '{$2="FILE_ADD"$2'}1 $INPUTFILE > $OUTPUTFILE

However, I got this error:

awk: cmd. line:1: FILE_ADD=ID123.
                                ^ syntax error

Thanks!! Amy


Solution

  • the $2=FILE_ADD $2 part concatenates the FILE_ADD variable with the second column ($2) in awk and the 1 at the end is a shorthand way to print all the modified rows

    check this out :

    FILE_ID=$(sed -n "{$LSB_JOBINDEX}p" "$IDs_file")
    
    awk -v OFS="\t" -v FILE_ADD="$FILE_ID" '{$2=FILE_ADD $2}1' "$INPUTFILE" > "$OUTPUTFILE"