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.

4
  • $FN is wrong there, try $file. And don't use cat, just do strings $file | sort .... Commented Apr 21, 2016 at 13:24
  • What is your expected output? Commented Apr 21, 2016 at 13:38
  • My expected output would be: Commented Apr 21, 2016 at 13:48
  • My expected output would be:14204|Bob|Smith|5|20160420 Commented Apr 21, 2016 at 13:49

1 Answer 1

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. ;-)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.