Artificial intelligent assistant

Bulk rename (append) CSV files based on a value within Is there a way to rename a batch of CSV files in a folder based on a value within each? I want to take the first value on the second row and append it to the end of the left-hand side. For example: Filename: 20160101-2015-12-01-20-45-1034581.csv Contents: HDR,FEC,8.1,FEC Webforms,8.1.0.0, F1N,C00593228,,JOSH LAROSE SENATORIAL VICTORY SUPER PAC New filename would have a dash and "F1N" added to the end: New filename: 20160101-2015-12-01-20-45-1034581-F1N.csv I'm trying to sort through a ton of electronic reports (about 80k files of various sizes), but I need to grab that field to filter out the ones I don't need. Working on a Mac. Thanks!

Here's a script that does it for one file.


#!/bin/sh
set -e

oldname=$1

# create file
printf '%s\
%s\
' \
'HDR,FEC,8.1,FEC Webforms,8.1.0.0,' \
'F1N,C00593228,,JOSH LAROSE SENATORIAL VICTORY SUPER PAC' \
> "$oldname"

# get that "first" value:
value=$(awk -F, 'NR == 2 {print $1; exit 0}' $oldname)

# insert it into the old name
newname=$(echo $oldname | sed 's/\.csv$/'"-$value&/")

# rename the file
mv $oldname $newname

# show your work ;-)
ls -l $newname
head $newname


Testing 1 2 3:


$ ./rename foo.csv
-rw-r--r-- 1 jklowden staff 90 Nov 22 20:33 foo-F1N.csv
HDR,FEC,8.1,FEC Webforms,8.1.0.0,
F1N,C00593228,,JOSH LAROSE SENATORIAL VICTORY SUPER PAC


You can modify it to be a loop, or write a loop around it. Personally, I would execute it from **find** (1) as


$ find ~/my/csv/files/ -exec ./rename {} +

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy dc5748d2f762f6c6fc2140458fdd8b8c