0

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!

1 Answer 1

0

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

#!/bin/sh
set -e

oldname=$1

# create file
printf '%s\n%s\n' \
       '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 {} +
3
  • Thanks! Looks like, no matter what file is specified as input, it outputs a file with just the data from the "create file" block. I assume that was there for testing. How to specify the input from the command as the the file to search? Also trying to get looping working with find. Commented Nov 23, 2017 at 3:39
  • @ChrisDC, yes, you should remove the part that creates the file. find doesn't loop, per se; but the -exec option executes a command on each found file. It's a weird utility, but very powerful, and worth struggling with. Commented Nov 23, 2017 at 15:33
  • Thank you so much! I was able to get through all the files very quickly and efficiently! Commented Nov 28, 2017 at 19:55

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.