Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: problem with splitting file on whitespace: how to circumvent inconsistent formatting through file

by shadowsong (Monk)
on Jul 05, 2016 at 09:54 UTC ( #1167220=note: print w/replies, xml ) Need Help??


in reply to problem with splitting file on whitespace: how to circumvent inconsistent formatting through file

Hi angela2

I can see that you've already had some great responses - some more easily understood than others; so for posterity here are a couple of one-liners similar to what BrowserUk suggested here Re: problem with splitting file on whitespace: how to circumvent inconsistent formatting through file that also take into account tab delimited columns and numbers with decimal points.

one-liner to print all columns

perl -F"\s*(?=[^\d,\.])" -wanle "print qq[@F]" badfile.txt

one-liner to print column 1

perl -F"\s*(?=[^\d,\.])" -wanle "print $F[0]" badfile.txt

...use $F[n-1] to access column n, e.g. print $F[1] will print the value for column 2.

quick explanation of command-line flags

  • -a and -F splits line text into the @F array; -a tells it to do the splitting and -F is used to specify the delimiter to split on - the default delimiter is space
  • -w sets the warning flag similar to the use warnings pragma
  • -n along with -p are the more commonly used command line switches both concerned with reading <ARGV>. -n provides us with the implicit construct
    while (<>) { "...your code here ..." }
  • -l sets the output record separator $\ to the input record separator $/ - which is \n by default. I often use it to preclude the need to add \n in my print statements
  • -e the most commonly used switch - it tells perl not to load and run a program file but to run the text following the -e as a program


See perlrun for a more info as I have only scratched the surface...

Best Wishes,
shadowsong

  • Comment on Re: problem with splitting file on whitespace: how to circumvent inconsistent formatting through file
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: problem with splitting file on whitespace: how to circumvent inconsistent formatting through file
by angela2 (Sexton) on Jul 05, 2016 at 14:22 UTC
    This is good stuff, thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1167220]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2019-06-17 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (80 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!