Replacing text in a .csv file using Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing text in a .csv file using Perl
# 1  
Old 02-13-2009
Data Replacing text in a .csv file using Perl

I have played with this for some time but i dont seem like i am getting it right. I am trying to change the delimiters on a file so i can import it into a database. this file has rows of data separated by enter

Right now the delimiters are represented by tabs and " ".

e.g.
"dlfkldfs flsdflk" [tab] "dkfjdlfj" [tab] "Jan 27, 2009 10:23 AM" [tab] ....

i want to change it from that to ---->
kjfjgfd gkjfkgg, kdjfljff, Jan 27, 2009 10:23 AM, .....
kjfjgfd gkjfkgg, kdjfljff, Jan 27, 2009 10:23 AM, .....

basically i want to get rid of the double qoutation and the tabs and just have commas as the delimitters.

Help is greatly appreciated
# 2  
Old 02-13-2009
Hammer & Screwdriver Is this what you are trying?

Code:
> cat file170
"dlfkldfs flsdflk"      "dkfjdlfj"      "Jan 27, 2009 10:23 AM" ....
"dlfkldfs flsdflk"      "abcdefgh"      "Jan 28, 2009 10:23 AM" ....

> cat file170 | tr "\t" "," | tr -d '"'
dlfkldfs flsdflk,dkfjdlfj,Jan 27, 2009 10:23 AM,....
dlfkldfs flsdflk,abcdefgh,Jan 28, 2009 10:23 AM,....

# 3  
Old 02-13-2009
Yes exactly.

I am running on Windows OS if that matters...
# 4  
Old 02-13-2009
First let me say that i am no programmer and have little knowledge. And that i am running the Perl Script on Windows OS, which i forgot to mention that

having that said i am trying to do this on a larger data set and automate the process. This is what i have so far (plz keep in mind i just learnt Perl)

#! usr/bin/perl -w
# print qq(Insert the file path of the file to convert);
# $input = <STDIN>;
# chomp $input;

# print qq(Insert the file path of the file to write to);
# $output = <STDIN>;
# chomp $output;

open(FILE, "WorkItems.csv" || die "Can't open file: $!";
@lines = <FILE>;
close (FILE);
foreach $line (@lines) {
$_ = $line;
s#\t#,;
}
open (NEWFILE, ">test1.txt") || die "Opening test1.txt: $!";
NEWFILE = @lines;
close (NEWFILE);
exit 0;
# 5  
Old 02-14-2009
sticking with your code and not making any suggestions or asking questions, this is how it maybe will work:

Code:
open(FILE, "WorkItems.csv" || die "Can't open file: $!";
@lines = <FILE>;
close (FILE);
foreach $line (@lines) {
  $line =~ tr/"//d;#remove all double-quotes
  $line =~ s/\t/,/g;#replace tabs with a comma
}

open (NEWFILE, ">test1.txt") || die "Opening test1.txt: $!";
print NEWFILE @lines;
close (NEWFILE);
exit 0;

See if that works. Make sure to check the newfile well.
# 6  
Old 02-15-2009
Computer

thanks for your help on the code. at least now im not getting any error messages Smilie

it did take out the double qoutes and put some commas here and there... but it didnt do it consistently and it also added lots of these 爀


Here is how one line of data looks like
"12722" "New" "Major" "Unassigned" "Asset Manager" "Unassigned" "7.1.1" "" "library" "Bruce Borth" "Jan 7, 2009 10:27 AM" "Jan 11, 2009 1:50 PM" "Unassigned" "Library function does not have the ability to create a library without enabling it - Liraries do not depend on other lib messge missing details" "" "" "Defect"爀

now this is what i got as an output for that same line:

12722Ⰰ NewⰀ MajorⰀ UnassignedⰀ Asset ManagerⰀ UnassignedⰀ 7.1.1Ⰰ Ⰰ libraryⰀ Bruce BorthⰀ Jan 7, 2009 10:27 AMⰀ Jan 11, 2009 1:50 PMⰀ UnassignedⰀ Library function does not have the ability to create a library without enabling it - Liraries do not depend on other lib messge missing detailsⰀ Ⰰ Ⰰ Defectഀ
㄀㌀㄀㔀㐀 ,一攀眀 ,一漀爀洀愀氀 ,䴀攀搀椀甀洀 ,刀愀琀椀漀渀愀氀 䄀猀猀攀琀 䴀愀渀愀最攀爀 ,匀攀爀瘀攀爀 ,㜀⸀㄀⸀㄀ ,㜀⸀㄀⸀㄀⸀㄀ 䐀攀瘀 , ,匀愀甀爀愀戀栀 䄀最愀爀眀愀氀 ,䨀愀渀 ㈀ Ⰰ ㈀  㤀 㐀㨀㐀㔀 倀䴀 ,䨀愀渀 ㈀ Ⰰ ㈀  㤀 㐀㨀㐀㘀 倀䴀 ,唀渀愀猀猀椀最渀攀搀 ,唀猀攀爀 椀猀 愀氀氀漀眀攀搀 琀漀 搀漀眀渀氀漀愀搀 琀栀攀 愀猀猀攀琀 攀瘀攀渀 琀栀漀甀最栀 椀昀 琀栀攀礀 搀漀渀琀 栀愀瘀攀 瀀攀爀洀椀猀猀椀漀渀 , , ,䐀攀昀攀挀琀 ਍

thank you
# 7  
Old 02-15-2009
post some real lines from your data file, put them betwen code tags. I suspect your data file might not be formatted how you said it is.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

TEXT to CSV using Perl

Hi Folks Need some help with this and my Perl isnt the hottest I also have text::csv installed on my perl install The large text with a few million entries is in a format below example text file Fig Leafs Cake No: 0000001 Author: King s. Record No: 995-34343-232-232... (5 Replies)
Discussion started by: messiah1
5 Replies

2. Shell Programming and Scripting

Replacing text in Perl given by command line

Hi I need to write a Perl script that the file given as first argument of the command line that will find all occurrences of the string given as the third argument of the command line and replace with the string given as the fourth argument. Name newfound file is specified as the second... (3 Replies)
Discussion started by: nekoj
3 Replies

3. Shell Programming and Scripting

Replacing comma with in double quotes in a csv file

Hello, I need to read a csv file and I am trying to replace a comma with a text DSEE?DSEE. Example Input "Chapter","NewTrains, "oldTrains","Delayed",10,"London" "Chapter","Newbuses,oldbuses","On Time",20,"London" Output "Chapter","NewTrainsDSEE?DSEE... (5 Replies)
Discussion started by: venkatvani
5 Replies

4. UNIX for Advanced & Expert Users

Replacing the comma in .csv file in unix

Hi All, Could some one help me on one of my requirement below: I have a sequential file with 4fields in it and it is a comma (,) seperated file. Delimeter is 'comma'. But in of the file column for ex: 3rd column it is 'Description' (column name) I am getting the values with commas.... (6 Replies)
Discussion started by: eskay_s
6 Replies

5. Shell Programming and Scripting

Format text to bold from perl script to csv

Hi everyone, is there any way in perl using which we can print the selective words in bold when we write the output to a csv file? Please find the example below 1. Filename: A 2. name age 12 3. city add 23 Line1 should only be bold. Outputs from other files being read in the... (2 Replies)
Discussion started by: ramakanth_burra
2 Replies

6. Shell Programming and Scripting

selective replacing text using sed/perl

Hi, I have the following text which I want to search and replace using perl and sed. I would appreciate any help. Please notice the file contains schema name with a single dot and a double dot . &&WEBDIR_SCHEMA. and &&WEBDIR_SCHEMA .. } I would like to change it to the acutal schema... (5 Replies)
Discussion started by: jville
5 Replies

7. Shell Programming and Scripting

Replacing strings in csv file.

Hi, I have a problem.. 1) I have a file that contains the lines as below : VRF-TM_DummyLab/mse02.lab,mse02.lab,ge-2/0/7.222 VRF-EMS_HUAWEI_MSAN_208/mse01.lab,mse01.lab,xe-1/0/0.208 2) I need a method to read this file, line by line from :... (5 Replies)
Discussion started by: msafwan82
5 Replies

8. Shell Programming and Scripting

Replacing strings in a log file and saves as a new csv

Hello Im new here.I need to replace strings and change it into csv format, or at least saves the file as csv if that would work :p. Heres an example of my scenario 1) I have a log file, named abc.log, and its like a txt based file anyway, and the content looks like this ... (2 Replies)
Discussion started by: tententen
2 Replies

9. UNIX for Dummies Questions & Answers

Replacing characters in csv file

Hello all, This is my first post here, so please excuse me if this question is too obvious or has been asked before. I am new to Unix and although I tried to search your forum for the answer to my question, I could not find an answer that would help me. I have a 500MB csv file with numeric values... (1 Reply)
Discussion started by: finwhiz
1 Replies

10. Shell Programming and Scripting

replacing commas with tilde in csv file.

hello all, i have a comma delimited file. i want to replace the commas in the file with the tilde symbol using sed. how can i do this? thanks. (4 Replies)
Discussion started by: femig
4 Replies
Login or Register to Ask a Question