0
|
1 #!/usr/bin/perl -w
|
|
2 #
|
|
3 # $RCSfile: ModifyTextFilesFormat.pl,v $
|
|
4 # $Date: 2015/02/28 20:46:20 $
|
|
5 # $Revision: 1.34 $
|
|
6 #
|
|
7 # Author: Manish Sud <msud@san.rr.com>
|
|
8 #
|
|
9 # Copyright (C) 2015 Manish Sud. All rights reserved.
|
|
10 #
|
|
11 # This file is part of MayaChemTools.
|
|
12 #
|
|
13 # MayaChemTools is free software; you can redistribute it and/or modify it under
|
|
14 # the terms of the GNU Lesser General Public License as published by the Free
|
|
15 # Software Foundation; either version 3 of the License, or (at your option) any
|
|
16 # later version.
|
|
17 #
|
|
18 # MayaChemTools is distributed in the hope that it will be useful, but without
|
|
19 # any warranty; without even the implied warranty of merchantability of fitness
|
|
20 # for a particular purpose. See the GNU Lesser General Public License for more
|
|
21 # details.
|
|
22 #
|
|
23 # You should have received a copy of the GNU Lesser General Public License
|
|
24 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or
|
|
25 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330,
|
|
26 # Boston, MA, 02111-1307, USA.
|
|
27 #
|
|
28
|
|
29 use strict;
|
|
30 use FindBin; use lib "$FindBin::Bin/../lib";
|
|
31 use Getopt::Long;
|
|
32 use File::Basename;
|
|
33 use Text::ParseWords;
|
|
34 use Benchmark;
|
|
35 use FileUtil;
|
|
36 use TextUtil;
|
|
37
|
|
38 my($ScriptName, %Options, $StartTime, $EndTime, $TotalTime);
|
|
39
|
|
40 # Autoflush STDOUT
|
|
41 $| = 1;
|
|
42
|
|
43 # Starting message...
|
|
44 $ScriptName = basename $0;
|
|
45 print "\n$ScriptName:Starting...\n\n";
|
|
46 $StartTime = new Benchmark;
|
|
47
|
|
48 # Get the options and setup script...
|
|
49 SetupScriptUsage();
|
|
50 if ($Options{help} || @ARGV < 1) {
|
|
51 die GetUsageFromPod("$FindBin::Bin/$ScriptName");
|
|
52 }
|
|
53
|
|
54 my(@TextFilesList);
|
|
55 @TextFilesList = ExpandFileNames(\@ARGV, "csv tsv");
|
|
56
|
|
57 # Process options...
|
|
58 print "Processing options...\n";
|
|
59 my(%OptionsInfo);
|
|
60 ProcessOptions();
|
|
61
|
|
62 print "Checking input text file(s)...\n";
|
|
63 my(%TextFilesInfo);
|
|
64 RetrieveTextFilesInfo();
|
|
65
|
|
66 # Generate output files...
|
|
67 my($FileIndex);
|
|
68 if (@TextFilesList > 1) {
|
|
69 print "\nProcessing text files...\n";
|
|
70 }
|
|
71 for $FileIndex (0 .. $#TextFilesList) {
|
|
72 if ($TextFilesInfo{FileOkay}[$FileIndex]) {
|
|
73 print "\nProcessing file $TextFilesList[$FileIndex]...\n";
|
|
74 ModifyTextFileFormat($FileIndex);
|
|
75 }
|
|
76 }
|
|
77 print "\n$ScriptName:Done...\n\n";
|
|
78
|
|
79 $EndTime = new Benchmark;
|
|
80 $TotalTime = timediff ($EndTime, $StartTime);
|
|
81 print "Total time: ", timestr($TotalTime), "\n";
|
|
82
|
|
83 ###############################################################################
|
|
84
|
|
85 # Modify text file format...
|
|
86 sub ModifyTextFileFormat {
|
|
87 my($Index) = @_;
|
|
88 my($TextFile, $NewTextFile, $InDelim, $Line, @Words);
|
|
89
|
|
90 $TextFile = $TextFilesList[$Index];
|
|
91
|
|
92 $NewTextFile = $TextFilesInfo{OutFile}[$Index];
|
|
93 $InDelim = $TextFilesInfo{InDelim}[$Index];
|
|
94
|
|
95 print "Generating new $NewTextFile file...\n";
|
|
96
|
|
97 open NEWTEXTFILE, ">$NewTextFile" or die "Error: Can't open $NewTextFile !$ \n";
|
|
98 open TEXTFILE, "$TextFile" or die "Error: Can't open $TextFile: $! \n";
|
|
99
|
|
100 while ($Line = GetTextLine(\*TEXTFILE)) {
|
|
101 @Words = quotewords($InDelim, 0, $Line);
|
|
102 $Line = JoinWords(\@Words, $OptionsInfo{OutDelim}, $OptionsInfo{OutQuote});
|
|
103 print NEWTEXTFILE "$Line\n";
|
|
104 }
|
|
105
|
|
106 close NEWTEXTFILE;
|
|
107 close TEXTFILE;
|
|
108 }
|
|
109
|
|
110 # Retrieve information about input text files...
|
|
111 sub RetrieveTextFilesInfo {
|
|
112 my($Index, $TextFile, $NewTextFile, $FileDir, $FileName, $FileExt, $InDelim, $OutFileExt);
|
|
113
|
|
114 %TextFilesInfo = ();
|
|
115 @{$TextFilesInfo{FileOkay}} = ();
|
|
116 @{$TextFilesInfo{InDelim}} = ();
|
|
117 @{$TextFilesInfo{OutFile}} = ();
|
|
118
|
|
119 FILELIST: for $Index (0 .. $#TextFilesList) {
|
|
120 $TextFile = $TextFilesList[$Index];
|
|
121
|
|
122 $TextFilesInfo{FileOkay}[$Index] = 0;
|
|
123 $TextFilesInfo{InDelim}[$Index] = "";
|
|
124 $TextFilesInfo{OutFile}[$Index] = "";
|
|
125
|
|
126 if (!(-e $TextFile)) {
|
|
127 warn "Warning: Ignoring file $TextFile: It doesn't exist\n";
|
|
128 next FILELIST;
|
|
129 }
|
|
130 if (!CheckFileType($TextFile, "csv tsv")) {
|
|
131 warn "Warning: Ignoring file $TextFile: It's not a csv or tsv file\n";
|
|
132 next FILELIST;
|
|
133 }
|
|
134 if (!open TEXTFILE, "$TextFile") {
|
|
135 warn "Warning: Ignoring file $TextFile: Couldn't open it: $! \n";
|
|
136 next FILELIST;
|
|
137 }
|
|
138 close TEXTFILE;
|
|
139
|
|
140 ($FileDir, $FileName, $FileExt) = ParseFileName($TextFile);
|
|
141
|
|
142 if ($FileExt =~ /^tsv$/i) {
|
|
143 $InDelim = "\t";
|
|
144 }
|
|
145 else {
|
|
146 $InDelim = "\,";
|
|
147 if ($OptionsInfo{InDelim} !~ /^(comma|semicolon)$/i) {
|
|
148 warn "Warning: Ignoring file $TextFile: The value specified, $OptionsInfo{InDelim}, for option \"--indelim\" is not valid for csv files\n";
|
|
149 next FILELIST;
|
|
150 }
|
|
151 if ($OptionsInfo{InDelim} =~ /^semicolon$/i) {
|
|
152 $InDelim = "\;";
|
|
153 }
|
|
154 }
|
|
155
|
|
156 if (lc($InDelim) eq lc($OptionsInfo{OutDelim})) {
|
|
157 warn "Warning: Ignoring file $TextFile: The value specified, $Options{outdelim}, for option \"--outdelim\" is same as input delimiter\n";
|
|
158 next FILELIST;
|
|
159 }
|
|
160
|
|
161 $OutFileExt = ($Options{outdelim} =~ /^tab$/i ) ? "tsv" : "csv";
|
|
162
|
|
163 $NewTextFile = $FileName;
|
|
164 if ($OptionsInfo{OutFileRoot} && (@TextFilesList == 1)) {
|
|
165 my ($RootFileDir, $RootFileName, $RootFileExt) = ParseFileName($OptionsInfo{OutFileRoot});
|
|
166 if ($RootFileName && $RootFileExt) {
|
|
167 $NewTextFile = $RootFileName;
|
|
168 }
|
|
169 else {
|
|
170 $NewTextFile = $OptionsInfo{OutFileRoot};
|
|
171 }
|
|
172 $NewTextFile .= ".$OutFileExt";
|
|
173 }
|
|
174 else {
|
|
175 $NewTextFile .= "FormatModified" . ".$OutFileExt";
|
|
176 }
|
|
177
|
|
178 if (!$OptionsInfo{Overwrite}) {
|
|
179 if (-e $NewTextFile) {
|
|
180 warn "Warning: Ignoring file $TextFile: New Text file, $NewTextFile, already exists\n";
|
|
181 next FILELIST;
|
|
182 }
|
|
183 }
|
|
184
|
|
185 $TextFilesInfo{FileOkay}[$Index] = 1;
|
|
186 $TextFilesInfo{InDelim}[$Index] = $InDelim;
|
|
187 $TextFilesInfo{OutFile}[$Index] = $NewTextFile;
|
|
188 }
|
|
189
|
|
190 }
|
|
191
|
|
192 # Process option values...
|
|
193 sub ProcessOptions {
|
|
194 %OptionsInfo = ();
|
|
195
|
|
196 $OptionsInfo{InDelim} = $Options{indelim};
|
|
197
|
|
198 $OptionsInfo{OutFileRoot} = $Options{root} ? $Options{root} : undef;
|
|
199 $OptionsInfo{Overwrite} = $Options{overwrite} ? $Options{overwrite} : undef;
|
|
200
|
|
201 $OptionsInfo{OutDelim} = ($Options{outdelim} =~ /^tab$/i ) ? "\t" : (($Options{outdelim} =~ /^semicolon$/i) ? "\;" : "\,");
|
|
202 $OptionsInfo{OutQuote} = ($Options{quote} =~ /^yes$/i) ? 1 : 0;
|
|
203
|
|
204 }
|
|
205
|
|
206 # Setup script usage and retrieve command line arguments specified using various options...
|
|
207 sub SetupScriptUsage {
|
|
208
|
|
209 # Retrieve all the options...
|
|
210 %Options = ();
|
|
211
|
|
212 $Options{indelim} = "comma";
|
|
213 $Options{outdelim} = "tab";
|
|
214 $Options{quote} = "yes";
|
|
215
|
|
216 if (!GetOptions(\%Options, "help|h", "indelim=s", "outdelim=s", "overwrite|o", "quote|q=s", "root|r=s", "workingdir|w=s")) {
|
|
217 die "\nTo get a list of valid options and their values, use \"$ScriptName -h\" or\n\"perl -S $ScriptName -h\" command and try again...\n";
|
|
218 }
|
|
219 if ($Options{workingdir}) {
|
|
220 if (! -d $Options{workingdir}) {
|
|
221 die "Error: The value specified, $Options{workingdir}, for option \"-w --workingdir\" is not a directory name.\n";
|
|
222 }
|
|
223 chdir $Options{workingdir} or die "Error: Couldn't chdir $Options{workingdir}: $! \n";
|
|
224 }
|
|
225 if ($Options{indelim} !~ /^(comma|semicolon)$/i) {
|
|
226 die "Error: The value specified, $Options{indelim}, for option \"--indelim\" is not valid. Allowed values: comma or semicolon\n";
|
|
227 }
|
|
228 if ($Options{outdelim} !~ /^(comma|semicolon|tab)$/i) {
|
|
229 die "Error: The value specified, $Options{outdelim}, for option \"--outdelim\" is not valid. Allowed values: comma, tab, or semicolon\n";
|
|
230 }
|
|
231 if ($Options{quote} !~ /^(yes|no)$/i) {
|
|
232 die "Error: The value specified, $Options{quote}, for option \"-q --quote\" is not valid. Allowed values: yes or no\n";
|
|
233 }
|
|
234 }
|
|
235
|
|
236 __END__
|
|
237
|
|
238 =head1 NAME
|
|
239
|
|
240 ModifyTextFilesFormat.pl - Change CSV Textfile(s) into TSV Textfile(s) and vice versa
|
|
241
|
|
242 =head1 SYNOPSIS
|
|
243
|
|
244 ModifyTextFilesFormat.pl TextFile(s)...
|
|
245
|
|
246 ModifyTextFilesFormat.pl [B<-h, --help>] [B<--indelim> comma | semicolon]
|
|
247 [B<--outdelim> comma | tab | semicolon] [B<-q, --quote> yes | no] [B<-r, --root> rootname]
|
|
248 [B<-w, --workingdir> dirname] TextFile(s)...
|
|
249
|
|
250 =head1 DESCRIPTION
|
|
251
|
|
252 Interchange CSV and TSV I<TextFile(s)> format. Mutiple file names are separated by spaces.
|
|
253 The valid file extensions are I<.csv> and I<.tsv> for comma/semicolon and tab delimited
|
|
254 text files respectively. All other file names are ignored. All the text files in a current
|
|
255 directory can be specified by I<*.csv>, I<*.tsv>, or the current directory name. The
|
|
256 B<--indelim> option determines the format of I<TextFile(s)>. Any file which doesn't
|
|
257 correspond to the format indicated by B<--indelim> option is ignored.
|
|
258
|
|
259 =head1 OPTIONS
|
|
260
|
|
261 =over 4
|
|
262
|
|
263 =item B<-h, --help>
|
|
264
|
|
265 Print this help message.
|
|
266
|
|
267 =item B<--indelim> I<comma | semicolon>
|
|
268
|
|
269 Input delimiter for CSV I<TextFile(s)>. Possible values: I<comma or semicolon>.
|
|
270 Default value: I<comma>. For TSV files, this option is ignored and I<tab> is used as a
|
|
271 delimiter.
|
|
272
|
|
273 =item B<-o, --overwrite>
|
|
274
|
|
275 Overwrite existing files.
|
|
276
|
|
277 =item B<--outdelim> I<comma | tab | semicolon>
|
|
278
|
|
279 Output text file delimiter. Possible values: I<comma, tab, or semicolon>
|
|
280 Default value: I<comma>.
|
|
281
|
|
282 =item B<-q, --quote> I<yes | no>
|
|
283
|
|
284 Put quotes around column values in output text file. Possible values: I<yes or
|
|
285 no>. Default value: I<yes>.
|
|
286
|
|
287 =item B<-r, --root> I<rootname>
|
|
288
|
|
289 New text file name is generated using the root: <Root>.<Ext>. Default new file
|
|
290 name: <InitialTextFileName>FormatModified.<Ext>. The csv, and tsv
|
|
291 <Ext> values are used for comma/semicolon, and tab delimited text files
|
|
292 respectively. This option is ignored for multiple input files.
|
|
293
|
|
294 =item B<-w, --workingdir> I<dirname>
|
|
295
|
|
296 Location of working directory. Default: current directory.
|
|
297
|
|
298 =back
|
|
299
|
|
300 =head1 EXAMPLES
|
|
301
|
|
302 To convert Sample*.csv into TSV files, type:
|
|
303
|
|
304 % ModifyTextFilesFormat.pl --outdelim tab -q no -o Sample*.csv
|
|
305
|
|
306 To convert Sample1.tsv into NewSample1.csv without any quotes around column
|
|
307 data values, type:
|
|
308
|
|
309 % ModifyTextFilesFormat.pl --outdelim comma - q no
|
|
310 -r NewSample1 -o Sample1.tsv
|
|
311
|
|
312 =head1 AUTHOR
|
|
313
|
|
314 Manish Sud <msud@san.rr.com>
|
|
315
|
|
316 =head1 SEE ALSO
|
|
317
|
|
318 ModifyNewLineChar.pl
|
|
319
|
|
320 =head1 COPYRIGHT
|
|
321
|
|
322 Copyright (C) 2015 Manish Sud. All rights reserved.
|
|
323
|
|
324 This file is part of MayaChemTools.
|
|
325
|
|
326 MayaChemTools is free software; you can redistribute it and/or modify it under
|
|
327 the terms of the GNU Lesser General Public License as published by the Free
|
|
328 Software Foundation; either version 3 of the License, or (at your option)
|
|
329 any later version.
|
|
330
|
|
331 =cut
|