Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^4: selecting columns from a tab-separated-values file

by Lotus1 (Curate)
on Jan 22, 2013 at 04:35 UTC ( #1014565=note: print w/replies, xml ) Need Help??


in reply to Re^3: selecting columns from a tab-separated-values file
in thread selecting columns from a tab-separated-values file

You can't reorder the output fields with 'cut'

Isn't that what I just said? Maybe you intended to reply to mildside.

...if you have 'sed' you can do this...

I have sed but I prefer Perl.

update: So I couldn't resist trying this sed command and found that it works for the input provided but as soon as you add more fields at the end it breaks.

Given this input file:

FIRST MIDDLE LAST STRNO CITY STATE ZIP 1 2 + 3 4 5 FIRST MIDDLE LAST STRNO CITY STATE ZIP 1 2 + 3 4 FIRST MIDDLE LAST STRNO CITY STATE ZIP

You get this output:

ZIP FIRST MIDDLE LAST STRNO CITY 3 STATE FIRST MIDDLE LAST STRNO 2 LAST FIRST CITY

The greedy '.*' regex expressions cause the regex engine to match from the right and work back. '\1' ends up holding everything on the left that remains unmatched. For the first line \1 holds FIRST    MIDDLE    LAST    STRNO        CITY    .

Here is a version that works.

C:\b\perlmonks\commands>sed -n "s/^\([^\t]*\t\)[^\t]*\t\([^\t]*\t\)[^\ +t]*\t[^\t]*\t\([^\t]*\).*/\2\1\3/p" sedtest.csv LAST FIRST CITY LAST FIRST CITY LAST FIRST CITY

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1014565]
help
Chatterbox?
Discipulus Syracusis anicula deos cotidie obsecrabat ut Dionysius..

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2016-12-05 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (75 votes). Check out past polls.