Putting two perl scripts together... triming whitespace off of recently parsed file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Putting two perl scripts together... triming whitespace off of recently parsed file
# 1  
Old 10-20-2011
Putting two perl scripts together... triming whitespace off of recently parsed file

Thanks to people's help, I have composed a single line within a .sh script that Ports a file into a csv:

perl -p -i -e 's/^(.{11})(.{6})(.{1})(.{2})(.{2})(.{2})(.{16})(.{5})(.{9})(.{8})(.{16})(.{23})(.{2})(.{2})(.{2})/"$1","$2","$3","$4","$5","$6","$7","$8""$9","$10","$11","$12","$13","$14","$15"/g' < /home/myfile.txt >/home/myfile.csv

From the web I found this perl script that uses regular expressions to pull the white space (extra spaces) from a list.

Perl - Trim whitespaces > beginning and end

#!/usr/bin/perl -w
use strict;

my @strings = (" foo ", " bar ", " foo bar ");

for (1..100000) {
foreach my $string (@strings) {
$string =~ s/^\s+//;
$string =~ s/\s+$//;
}
}

Now, I believe I could kludge this together by reading in the newly created file as a string... however :would there be a better way of doing it?

In a language like Tcl I could just use the trim command on each element... but in Perl how would I do it?

More appropriately what is the "right way" of doing this?
# 2  
Old 10-20-2011
What does your "/home/myfile.txt" look like?

tyler_durden
# 3  
Old 10-20-2011
Instead of one huge hardcoded regex, make a list of positions and loop through it. Then you get to use join() instead of listing each individual thing, too.

Code:
#!/usr/bin/perl

my @arr=(11,6,1,2,2,2,16,5,9,8,16,23,2,2,2);
my @out;

while($str=<STDIN>)
{
        for($X=0, $N=0; $arr[$N]; $N++)
        {
                $out[$N]=substr($str, $X, $arr[$N]);
                $out[$N] =~ s/^\s+//; # Leading whitespace
                chomp($out[$N]); # Trailing whitespace
                $out[$N] = '"' . $out[$N] . '"';
                $X += $arr[$N];
        }

        print join(",", @out), "\n";
}

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 10-20-2011
Ok, having gone through your previous post, I think maybe you want something like the following?

Code:
$
$
$ cat f37
1234567890123456 123456BIGAUDIODYNAMITE33      123.12  123456  12345678901234MARK E WILLIAMS           123456
$
$
$
$ perl -plne '$_="\"".join("\",\"", map {s/^\s*//; $_} unpack("A10 A6 A1 A2 A2 A2 A16 A5 A9 A8 A16 A26 A2 A2 A2"))."\""' f37
"1234567890","123456","","12","34","56","BIGAUDIODYNAMITE","33","123.12","123456","12345678901234","MARK E WILLIAMS","12","34","56"
$
$
$

Those numbers after "A" are the lengths of the "chunks" you tokenize your input string into.
They may also be perceived as "running differences" of your comma positions, as seen below -

Code:
    11    17    18    20    22    24    40    45    54    62    78...
----  ----  ----  ----  ----  ----   ---  ----  ----  ----  ----
  |     |     |    |     |     |      |     |     |     |     |
  V     V     V    V     V     V      V     V     V     V     V
 10     6     1    2     2     2     16     5     9     8    16   ...

=> "A10 A6 A1 A2 A2 A2 A16 A5 A9 A8 A16 ..."

tyler_durden

Last edited by durden_tyler; 10-20-2011 at 05:36 PM..
This User Gave Thanks to durden_tyler For This Post:
# 5  
Old 10-21-2011
Sorry, I've been away... (work full time + full time student + some "email" emergency + caffeine withdrawal etc)

Thank you everyone for your help. I'm trying to learn Perl and finding it syntactically different than other C-like languages. (I've most recently been using VBA for data analysis)


But basically even though this code is going to be used... I'm also trying to learn what is going on.

Thanks Everyone.
# 6  
Old 10-21-2011
Perl is "C-like" the same way elephants are like deep-sea fishing. Smilie

Except for { } code blocks I really don't see any resemblance.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Triming the data in a file.

Hi, I have a big csv file with below data. file: La Cage Aux Folles (Widescreen) Famous Mystics and Psychics A Passion for Planning Financials Operations Marketing Management and Ethics Precious Moments Holy Bible New King James Version Precious Angels Edition Blue Practical Recording... (9 Replies)
Discussion started by: Raghuram717
9 Replies

2. Shell Programming and Scripting

Putting s Perl module into your own script

Hi We have to create a perl script..for part of a project I ceated a script that called another perl module use AppUtil::VMUtil; But have been told that we need to incorpurate that module into my script...(with comments) Would you know how to copy or place a module from one script... (2 Replies)
Discussion started by: olearydc
2 Replies

3. Shell Programming and Scripting

Find and move files parsed from cvs file

I need help with a bash script. We have a directory of files which need to be renamed and moved to another directory based on filename information in a cvs file. The contents of the cvs file are as follows: A102345,abc123 A102347,dfg475 Where dfg475 is the basename without extension Our... (8 Replies)
Discussion started by: Lloyd Boyette
8 Replies

4. Shell Programming and Scripting

Triming a string

Hi i have an input " load /appss/asdfas/... I want to take the string present between first / / i.e appss Input is "load /appss/asdfas/..." Expected output is appss Thanks in advance Ananth (9 Replies)
Discussion started by: Ananthdoss
9 Replies

5. Shell Programming and Scripting

Perl program to simulate Least Recently Loaded paging

Hi, If I want to write perl program that simulated Least Recently Loaded(LRL) to calculate page fault: The user must input page access and number of page fram that need to be greater than 3 Example Page Access: 5, 3, 4, 1, 7, 5, 3, 2, 3, 6, 7, 1, 4, 6 Frames: 3 PA: 5 3 4 1 7 5 3 2 3 6... (6 Replies)
Discussion started by: guidely
6 Replies

6. Shell Programming and Scripting

Search for a recently updated file

Hi, I am looking for a command to search for a specific file which was recently modified in the current directory leaving some unwanted files to be listed. For example, when I try ls - lrt it shows the following output. I want to ommit the files with the name 'resend' and... (3 Replies)
Discussion started by: svajhala
3 Replies

7. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

8. Shell Programming and Scripting

Updating a CSV file by multiple PERL scripts

Hi Friends, I'm writing code to update a CSV file by multiple PERL scripts. I've around 2000 PERL scripts which need to register their entries into a CSV file. Hence, I'm inserting following line code in all the 2000 PERL scripts. open(FILE,... (5 Replies)
Discussion started by: Lokesha
5 Replies

9. Shell Programming and Scripting

Running many PERL scripts in one file

Hi, I have many PERL scripts in my system(Solaris 10 UNIX OS).I want to define all scripts in one file and run.Please suggest how to define. (3 Replies)
Discussion started by: sudhakaryadav
3 Replies

10. Shell Programming and Scripting

ftp most recently modified file

Hi what is the most optimum way to ftp the most recently modified file starting with a particular string. i tried this ftp -n 2>logfile 1>&2 <<EOF open xxxxxx user xxxx xxxx prompt ls -ltr f* res !var=`tail -1 |awk { print $9 }'` bye EOF that gives... (6 Replies)
Discussion started by: ahmedwaseem2000
6 Replies
Login or Register to Ask a Question