Link to home
Start Free TrialLog in
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!
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ttist25
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!
sed or perl is what I would use to do this.  Glad you solved it on your own.
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 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.