0

I have a shell script which is trying to trim a file from end of the line but I always get some error.

Shell Script:

AWK_EXPRESSION='{if(length>'"$RANGE1"'){ print substr('"$0 "',0, length-'"$RANGE2"'}) } else { print '"$0 "'} }'

for report in ${ACTUAL_TARGET_FOLDER}/* ; do
  awk $AWK_EXPRESSION $report > $target_file
done

If I trigger the AWK command, I get unexpected newline or end of string near print.

What am I missing?

halfer
  • 19,824
  • 17
  • 99
  • 186
Rasmi
  • 501
  • 1
  • 6
  • 26

2 Answers2

2

Why are you trying to store the awk body in a shell variable? Just use awk and the -v option to pass a shell value into an awk variable:

awk -v range1="$RANGE1" -v range2="$RANGE2" '{
    if (length > range1) {
        print substr($0,0, length-range2)
    } else { 
        print 
    }
}' "$ACTUAL_TARGET_FOLDER"/* > "$target_file"

Add a few newlines to help readability.

Get out of the habit of using ALLCAPS variable names, leave those as reserved by the shell. One day you'll write PATH=something and then wonder why your script is broken.

Unquoted variables are subject to word splitting and glob expansion. Use double quotes for all your variables unless you know what specific side-effect you want to use.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
1

I would recommend writing the AWK program using AWK variables instead of interpolating variables into it from the shell. You can pass variable into awk on the command line using the -v command line option.

Also, awk permits using white space to make the program readable, just like other programming languages. Like this:

AWK_EXPRESSION='{
  if (length > RANGE1) {
    print substr($0, 1, length-RANGE2)
  } else {
    print
  }
}'

for report in "${ACTUAL_TARGET_FOLDER}"/* ; do
  awk -v RANGE1="$RANGE1" -v RANGE2="$RANGE2" "$AWK_EXPRESSION" "$report" > "$target_file"
done
Joni
  • 108,737
  • 14
  • 143
  • 193
  • 1
    Thanks @joni this helped and worked for me... was breaking my head whole day today on this. – Rasmi Jul 25 '20 at 22:11