0

I have a fixwidth file. Below is the sample:

0196969696645001200    1100450000000000TEST1 / TECHNOLOTEST               97827Y
0196969696645001500    1100450000000000TEST222 EXPRESS - CMTEST           97827Y
0196969696600701325    7003150000000000TEST33333                          70215U
0196969696621055400    1476210000000000TEST44444SE - CMC AUERSTEST        97803Y

Sample metadata file:

COL1,1,2
COL2,3,19
COL3,22,27
COL4,28,33
COL5,34,37
COL6,38,40
COL7,78,81

Problem statement: Read meta data file(start and end position)for each column and create one file for each column taking reference of fix width file.

My code:

feed_file=$1
metadata_file=$2

 if [ $# -lt 2 ]
  then
   echo "Insufficient Number of Parameters passed"
   echo "Usage <FEED FILE NAME> <META DATA FILE NAME>"
   exit 1
 fi

#Read metadata file and create feed file for each attribute 
 while read line
  do
      attrib_name=`echo $line | cut -d "," -f1`
      beg_point=`echo $line | cut -d "," -f2`
      end_point=`echo $line | cut -d "," -f3`
      echo "$attrib_name || $beg_point || $end_point"

      range=$beg_point-$end_point

      cat $feed_file | cut -c $range > $attrib_name
 done < $metadata_file


Error: cut: bad list for c option
  • that actually works for me, I can't replicate the error from cut. An empty line would give an error on the redirection. But, you could use while IFS=, read -r name start end ; do... to split the metadata fields directly, without using cut for that – ilkkachu Dec 8 '17 at 9:57
1

Don't use shell loops to process text, use text processing tools instead like awk:

awk -F, '
  !meta_done{start[$1] = $2; len[$1] = $3 - $2 + 1; next}
  {for (i in start) print substr($0, start[i], len[i]) > i}
  ' "$meta_file" meta_done=1 "$feed_file"

Note that there's a limitation in that $meta_file and $feed_file must not be shaped like awk variable assignments. If you have files called like meta=data.txt, you'll need to pass ./meta=data.txt to awk instead of meta=data.txt.

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.