Avatar of ttist25
ttist25
 asked on

Find and Replace Only 1st line in many csv files

Hello,

I have over 100 CSV files that I need to pre-process before importing to SQL.  The files are too large to open in Excel.

What I'm trying to do is modify the first line (the header row) of each file to add double-quotes to the field terminators.

For example, given this header row:
field1|field2|field3|field4

replace | with "|" so the first row would be:
field1"|"field2"|"field3"|"field4

Notepad++ has a "find in files" feature that can use regex but I'm not sure how to linit it to the first line.

Any ideas would be greatly appreciated.  

Thanks!
Regular ExpressionsSQL

Avatar of undefined
Last Comment
wilcoxon

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
slightwv (䄆 Netminder)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ttist25

ASKER
Thanks slightwv -

I ended up installing sed from mingw and ran the following command to get the desired output:

for i in `find . -iname *.csv` ; do  sed -i '1s/|/\"|\"/g' $i ; done

Thanks again for your help!
wilcoxon

sed or perl is what I would use to do this.  Glad you solved it on your own.
slightwv (䄆 Netminder)

You should use the utilities the OS gives you, especially when it is Powershell.  It is incredibly powerful/

If I were already in a Linux environment, sure use seq.  Since you are on Windows, why install Linux tools?

Note:  You can use Wildcards in Powershell go get all the files as well.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
wilcoxon

I completely disagree with slightwv.  You should use whatever tools work best which are usually unix/linux tools.  I always have Cygwin 64-bit (though mingw is a better option as long as it has the tools you want) and Strawberry Perl installed on Windows machines because they simply work better than what is included with Windows.