Advertisement

04.17.2005 at 08:33PM PDT, ID: 21392233
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Remove leading whitespaces

Tags: perl, leading, remove, space
I have a text file in the format:

(Future) Ch. Giscours Margaux 750ml 12 per case
WA 90-92 points, This saturated ruby/purple-colored effort offers notes of liquid minerals interspersed with subtle toasty oak, black currant, graphite and a hint of flowers.
$28 per 750ml Bottle      
$317 per Case of 12      
       2002 Future
       Red Blend
       Bordeaux
       France
       Red
       Margaux
(Future) Ch. Giscours Margaux 750ml 12 per case
WS 92-94 points, Intense aromas of cooked fruit and prunes. Full-bodied, with rich and ripe fruit character. Soft, round tannins and a long finish. Heady and rich.  
$38 per 750ml Bottle
$448 per Case of 12
       2003 Future
       Red Blend
       Bordeaux
       France
       Red
        Margaux       
      
The white spaces at the beginning of each line need to be eliminated so they start at the beginning. Need to adapt this script to read an outside text file and properly reformat it:

#!/usr/bin/perl
use strict;

my $a="   parse text    ";

$a =~ s/(^ *)||( *$)//g;
print "$a.\n";
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: dprasad
Solution Provided By: teraplane
Participating Experts: 4
Solution Grade: A
Views: 23
Translate:
Loading Advertisement...
04.17.2005 at 08:59PM PDT, ID: 13803603

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:19PM PDT, ID: 13803650

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:20PM PDT, ID: 13803652

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:22PM PDT, ID: 13803657

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:24PM PDT, ID: 13803659

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:26PM PDT, ID: 13803667

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:27PM PDT, ID: 13803668

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:32PM PDT, ID: 13803676

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:34PM PDT, ID: 13803679

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:34PM PDT, ID: 13803680

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:35PM PDT, ID: 13803683

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:36PM PDT, ID: 13803688

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:37PM PDT, ID: 13803689

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:38PM PDT, ID: 13803695

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:42PM PDT, ID: 13803704

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:46PM PDT, ID: 13803717

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:50PM PDT, ID: 13803729

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:55PM PDT, ID: 13803745

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 09:57PM PDT, ID: 13803758

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 10:10PM PDT, ID: 13803793

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 10:19PM PDT, ID: 13803808

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 10:23PM PDT, ID: 13803820

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 10:24PM PDT, ID: 13803823

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.17.2005 at 10:28PM PDT, ID: 13803833

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.18.2005 at 04:02AM PDT, ID: 13804930

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.18.2005 at 04:39AM PDT, ID: 13805115

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.18.2005 at 04:47AM PDT, ID: 13805168

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.18.2005 at 04:48AM PDT, ID: 13805175

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.18.2005 at 11:28AM PDT, ID: 13808962

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
04.17.2005 at 08:59PM PDT, ID: 13803603
I tried this:

#!/usr/bin/perl
use strict;



$outfile = 'out.txt';
 
open (FILE, 'mydata.txt') or die "cannot open $file: $!"; # opens the file
open (outfile, 'out.txt') or die "cannot open $file: $!"; # opens the file

@my_file = <FILE>;





@my_file =~ s/(^ *)||( *$)//g;
print outfile "@my_file.\n";

gives error:

Global symbol "$outfile" requires explicit package name at p1.pl line 6.
Global symbol "$file" requires explicit package name at p1.pl line 8.
Global symbol "$file" requires explicit package name at p1.pl line 9.
Global symbol "@my_file" requires explicit package name at p1.pl line 11.
Global symbol "@my_file" requires explicit package name at p1.pl line 17.
Global symbol "@my_file" requires explicit package name at p1.pl line 18.
Execution of p1.pl aborted due to compilation errors.
 
04.17.2005 at 09:19PM PDT, ID: 13803650

Rank: Sage

No need for a script, just do:

perl -pe 's/^\s+//' mydata.txt >out.txt

 
04.17.2005 at 09:20PM PDT, ID: 13803652

Rank: Wizard

Try this

#!/usr/bin/perl
use strict;

my $outfile = 'out.txt';
open (FILE, "<mydata.txt") or die "cannot open mydata.txt: $!"; # opens the file
open (outfile, ">out.txt") or die "cannot open $outfile: $!"; # opens the file

while <FILE>
{
   $_ =~ s/(^ *)||( *$)//g;
   print(outfile "$_\n";
}
close(outfile);
 
04.17.2005 at 09:22PM PDT, ID: 13803657
hmm ok. When I do that, the output file is in the same format as the input, i.e. no changes are made
 
04.17.2005 at 09:24PM PDT, ID: 13803659

Rank: Sage

If you were to write it as a script, I'd do it like:

#!/usr/bin/perl
use strict;

my $data = 'mydata.txt';
my $outfile = 'out.txt';

open FILE, $data or die "Can not open $data $!\n";
open OUTPUT, ">$outfile" or die "Can not open $outfile $!\n";

while (<FILE>) {
  s/^\s+/;
  print OUTPUT;
}

Now, your script sample also tries to delete trailing whitespace, if you need that, add

s/\s+$//;

to the while loop.
 
04.17.2005 at 09:26PM PDT, ID: 13803667
ok, yeah I just need the leading spaces. I get the error:

Substitution replacement not terminated at p1.pl line 11.

which is: s/^\s+/;
 
04.17.2005 at 09:27PM PDT, ID: 13803668

Rank: Wizard

i made a few errors, this one works for me

my $outfile = 'out.txt';
open (FILE, "<mydata.txt") or die "cannot open mydata.txt: $!"; # opens the file
open (outfile, ">out.txt") or die "cannot open $outfile: $!"; # opens the file

while (<FILE>)
{
      chomp;
   $_ =~ s/(^ *)||( *$)//g;
   print(outfile "$_\n");
}
close(outfile);
 
04.17.2005 at 09:32PM PDT, ID: 13803676
hmmm... ok Im copying and pasting. Only the very last line got its whitespace cut out. All of the rest are still the same.
 
04.17.2005 at 09:34PM PDT, ID: 13803679

Rank: Wizard

here is the outfile produced by my code, all white space stripped. what version of perl are u using?
---
Future) Ch. Giscours Margaux 750ml 12 per case
WA 90-92 points, This saturated ruby/purple-colored effort offers notes of liquid minerals interspersed with subtle toasty oak, black currant, graphite and a hint of flowers.
$28 per 750ml Bottle
$317 per Case of 12
2002 Future
Red Blend
Bordeaux
France
Red
Margaux
(Future) Ch. Giscours Margaux 750ml 12 per case
WS 92-94 points, Intense aromas of cooked fruit and prunes. Full-bodied, with rich and ripe fruit character. Soft, round tannins and a long finish. Heady and rich.
$38 per 750ml Bottle
$448 per Case of 12
2003 Future
Red Blend
Bordeaux
France
Red
Margaux
 
04.17.2005 at 09:34PM PDT, ID: 13803680
(Future) Ch. Giscours Margaux 750ml 12 per case
WS 92-94 points, Intense aromas of cooked fruit and prunes. Full-bodied, with rich and ripe fruit character. Soft, round tannins and a long finish. Heady and rich.
$38 per 750ml Bottle
$448 per Case of 12
      2003 Future
      Red Blend
      Bordeaux
      France
      Red
Margaux       
 
04.17.2005 at 09:35PM PDT, ID: 13803683
not sure, how do i find out?
 
04.17.2005 at 09:36PM PDT, ID: 13803688

Rank: Wizard

on command line type
perl -v
 
04.17.2005 at 09:37PM PDT, ID: 13803689
This is perl, v5.8.4 built for MSWin32-x86-multi-thread
(with 3 registered patches, see perl -V for more detail)
 
04.17.2005 at 09:38PM PDT, ID: 13803695

Rank: Wizard

should be fine then, have you tried my last program exactly as entered above?
 
04.17.2005 at 09:42PM PDT, ID: 13803704
yes, I copied and pasted it into a file called p2.pl

run with


perl p2.pl

weird.
 
04.17.2005 at 09:46PM PDT, ID: 13803717

Rank: Wizard

I can simplify it a bit, can you check the creation date of out.txt to see it is the latest generated by the program?

my $outfile = 'out.txt';
open (FILE, "<mydata.txt") or die "cannot open mydata.txt: $!"; # opens the file
open (outfile, ">out.txt") or die "cannot open $outfile: $!"; # opens the file

while (<FILE>)
{
   chomp;
   $_ =~ s/^ *//g;
   print(outfile "$_\n");
   print ">>>$_<<<\n";  #this will echo output to the console for checking
}
close(outfile);
 
04.17.2005 at 09:50PM PDT, ID: 13803729
yes, the out.txt is up to date. I was deleting it after each execution just to be sure. On that last bit, I get this spit out to the screen:

>>>WS 92-94 points, Intense aromas of cooked fruit and prunes. Full-bodi
 rich and ripe fruit character. Soft, round tannins and a long finish. H
 rich.  <<<
>>>$38 per 750ml Bottle<<<
>>>$448 per Case of 12<<<
>>>     2003 Future <<<
>>>     Red Blend <<<
>>>     Bordeaux <<<
>>>     France <<<
>>>     Red <<<
>>>Margaux      <<<

but no changes appear in out.txt.

hate to ask this, but is there any way I could mail you the file? I'm a perl newbie, I wonder if theres something wrong with my installation.. The file is 181 kb. I woukld really appreciate it
 
04.17.2005 at 09:55PM PDT, ID: 13803745

Rank: Wizard

I think i can see the problem, there may be tabs in your data.
Could you  try this last change before sending the file?

while (<FILE>)
{
   $_ =~ s/^\s*//g;
   print(outfile "$_");
}
Accepted Solution
 
04.17.2005 at 09:57PM PDT, ID: 13803758
ahhhhh cool, yes that worked! thanks a lot for the help

Dinesh
 
04.17.2005 at 10:10PM PDT, ID: 13803793

Rank: Sage

Sorry.  Typo.  Should have been:

#!/usr/bin/perl
use strict;

my $data = 'mydata.txt';
my $outfile = 'out.txt';

open FILE, $data or die "Can not open $data $!\n";
open OUTPUT, ">$outfile" or die "Can not open $outfile $!\n";

while (<FILE>) {
  s/^\s+//;
  print OUTPUT;
}
Assisted Solution
 
04.17.2005 at 10:19PM PDT, ID: 13803808
It's all good, I was starting to feel my carpel tunnel tire out from manually taking those damn tabs out, phew
 
04.17.2005 at 10:23PM PDT, ID: 13803820

Rank: Wizard

glad it's working. As I helped solve the question by identifying the tabs (white space) problem it would have been nice to  also have received some points :)
 
04.17.2005 at 10:24PM PDT, ID: 13803823
woops, sorry about that I wasnt paying attention!! I will re assign points, my bad
 
04.18.2005 at 04:02AM PDT, ID: 13804930

Rank: Sage

I've reopened the question so you can close it the way you want.

jmcg
EE Page Editor for Perl
 
04.18.2005 at 04:39AM PDT, ID: 13805115

Rank: Wizard

If you are on windows, an earlier suggestion by Tintin might be modified to suit your requirements

perl -pe "s/^\s+//" mydata.txt >out.txt

or if you want to change te original file itself,

perl -i.bak -pe "s/^\s+//" mydata.txt

this will change mydata.txt and save the original file as mydata.txt.bak
 
04.18.2005 at 04:47AM PDT, ID: 13805168

Rank: Wizard