0

I have a directory full of files with the date they were created in the filename (ie "mvr-20160420.csv"). The files contain comma delimited data that I'm combining into a single text file. I'm trying to figure out how to add the date portion of each file to the data. I'm able to read the files, but not the filenames.

Sample file for mvr-20160420.csv:

client employee id,first name,last name,points
14204,Bob,Smith,5

I have a Korn script and just trying to add the filename to the data:

for file in $Folder/path/burnsmcd-mvr*.csv
do 
FN=file

if [ $FIRSTTIME = $STATUS_OK ]
then 
  FIRSTTIME=$STATUS_NOT_OK
  cat $file | strings | sort -n | awk 'BEGIN{FS=","}{OFS="|"}{if ( $1 ~ /^[0-9]+$/) print "ADMVR", $1, $2, $3, $4, $FN}' > $Folder/path/ext_test.txt
else      
  cat $file | strings | sort -n | awk 'BEGIN{FS=","}{OFS="|"}{if ( $1 ~ /^[0-9]+$/) print "ADMVR", $1, $2, $3, $4, $FN}' >> $Folder/path/ext_test.txt
fi
done

So my output ext_test.txt looks like this:

14204|Bob|Smith|5|14204,Bob,Smith,5

I can't seem to add the filename. I also need to extract the date portion.

  • $FN is wrong there, try $file. And don't use cat, just do strings $file | sort .... – ott-- Apr 21 '16 at 13:24
  • What is your expected output? – 123 Apr 21 '16 at 13:38
  • My expected output would be: – elzar Apr 21 '16 at 13:48
  • My expected output would be:14204|Bob|Smith|5|20160420 – elzar Apr 21 '16 at 13:49
0
$ awk 'BEGIN {OFS=","} 
      { split (FILENAME, parts, "[-.]"); print FILENAME, parts[2], $0 }' \
      mrv-20161225.csv 
mrv-20161225.csv,20161225,client employee id,first name,last name,points
mrv-20161225.csv,20161225,14204,Bob,Smith,5

A morning without awk is like a long day at work. ;-)

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.