Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by shadowsong (Pilgrim)
on Jul 05, 2016 at 09:54 UTC ( [id://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

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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1167220]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-03-28 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found