Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
do
  split -n 3 -d -a 2 "$file" "$file--"
  for each in $(seq -w 2 10)
  do
        echo $file--$each

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

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

 
discard

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.