CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Replacing first row first coulmn value from comma delimiter .csv files from a directory

 



afzal
New User

May 6, 2011, 2:11 AM

Post #1 of 6 (1642 views)
Replacing first row first coulmn value from comma delimiter .csv files from a directory Can't Post

Hi Gurus,

I am New to perl,

I am genrating number of comma delimited .csv files in particular directory with header through Informatica ETL .

But informatica is genrating each .csv file's header with #

like

#col1,col2,col3,col4

1,a,b,c,d

2,e,f,g,h

3,i,j,k,l

But I want to remove # from first column and change it to col1

Required all .csv files in directory should be

col1,col2,col3,col4

1,a,b,c,d

2,e,f,g,h

3,i,j,k,l



Thanks and Regards,

Afzal

ETL Architect


mishin
Novice

May 6, 2011, 6:53 AM

Post #2 of 6 (1637 views)
Re: [afzal] Replacing first row first coulmn value from comma delimiter .csv files from a directory [In reply to] Can't Post

remove # from beginning of string:
perl -p -i.bak -e 's/^#//g' test.txt


(This post was edited by mishin on May 6, 2011, 6:58 AM)


miller
User

May 6, 2011, 10:10 AM

Post #3 of 6 (1632 views)
Re: [mishin] Replacing first row first coulmn value from comma delimiter .csv files from a directory [In reply to] Can't Post

And if you want to isolate that replacement to just the header row only.


Code
perl -p -i.bak -e '$. == 1 && s/^#//' test.csv



afzal
New User

May 8, 2011, 11:00 PM

Post #4 of 6 (1625 views)
Re: [miller] Replacing first row first coulmn value from comma delimiter .csv files from a directory [In reply to] Can't Post

Hi Miller,

Thanks for your response,

When I execute the perl its show below error

C:\Documents and Settings\Informatica>perl -p -i.bak -e '$. == 1 && s/#//' D:\Informatica\PowerCen
ter8.1.1\server\infa_shared\TgtFiles\B2CC_DATALOAD_POSTVAL_CA.csv
Can't find string terminator "'" anywhere before EOF at -e line 1.

Also please let me know how to use it for more than one .csv files in a folder at time.

Thanks and Regards,

Afzal


miller
User

May 8, 2011, 11:03 PM

Post #5 of 6 (1623 views)
Re: [afzal] Replacing first row first coulmn value from comma delimiter .csv files from a directory [In reply to] Can't Post

If you're on windows, use double quotes for the strings, instead of single quotes.


Code
perl -p -i.bak -e "$. == 1 && s/^#//" test.csv



afzal
New User

May 9, 2011, 10:42 PM

Post #6 of 6 (1616 views)
Re: [miller] Replacing first row first coulmn value from comma delimiter .csv files from a directory [In reply to] Can't Post

Thanks miller ,It's working.

But issue is, it's creating two files with same name like

B2CC_DATALOAD_POSTVAL_CA.csv (header with #)

#col1,col2,col3,col4

1,a,b,c,d

2,e,f,g,h

3,i,j,k,l


and B2CC_DATALOAD_POSTVAL_CA.csv ((header without #)

col1,col2,col3,col4

1,a,b,c,d

2,e,f,g,h

3,i,j,k,l


Please suggest on it as well as please suggest on how to implement it for all .csv files in directory from script.

Thanks and Regards,

Afzal

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives