Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I need to split a file and rename it accordingly using awk command. I'm trying to use the substring command and pass a parameter to awk command but it fails.

for file in *.csv
  split -n 3 -d -a 2 "$file" "$file--"
  for each in $(seq -w 2 10)
        echo $file--$each

  awk -v filestem= ${$file--$each:2:5} '{filename = filestem; print >> filename}' $file--$each
share|improve this question

1 Answer 1

You have two options

  1. Apply the substring inside awk

    $ awk -v filestem="$file--$each" \
        '{filename = substr(filestem, 3, 6);print >> filename}' 
  2. Set $file--$each to a variable, then substring that variable

    $ filestem="$file--$each"
    $ awk -v filestem="${filestem:2:5}" \
        '{filename = filestem; print >> filename}'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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