perl/korn scirpt for appending first variable to end of line.

Discussion in 'Perl Misc' started by NNTP, Feb 11, 2004.

  1. NNTP

    NNTP Guest

    I am writting a script that does some analysis of a log file and here
    is what I need.

    I need to take the first variable of the line, cut it and paste it to
    the end of the line.

    example:
    input : accept blah blah blahc blah data data data
    output : blah blah blahc blah data data data accept

    no. of variables very and thats why I can't use a simple script like
    the following.

    cat file | awk '{print $2,$3,$4,$5,$6,$7,$8,$1}'

    any ideas?
     
    NNTP, Feb 11, 2004
    #1
    1. Advertising

  2. On Wed, 11 Feb 2004 06:32:41 -0800, NNTP wrote:

    > I am writting a script that does some analysis of a log file and here
    > is what I need.
    >
    > I need to take the first variable of the line, cut it and paste it to
    > the end of the line.
    >
    > example:
    > input : accept blah blah blahc blah data data data
    > output : blah blah blahc blah data data data accept
    >
    > no. of variables very and thats why I can't use a simple script like
    > the following.
    >
    > cat file | awk '{print $2,$3,$4,$5,$6,$7,$8,$1}'
    >
    > any ideas?


    Many :)

    You could a few functions to do this. Have a look at:
    perldoc -f split
    perldoc -f unpack
    perldoc perlre

    It depends on what you know about the data being parsed, how consistent
    the data is, and how much work you really want to put into it.

    HTH

    --
    Jim

    Copyright notice: all code written by the author in this post is
    released under the GPL. http://www.gnu.org/licenses/gpl.txt
    for more information.

    a fortune quote ...
    "I went to the museum where they had all the heads and arms from
    the statues that are in all the other museums." -- Steven
    <Wright
     
    James Willmore, Feb 11, 2004
    #2
    1. Advertising

  3. NNTP

    Tony Walton Guest

    NNTP wrote:
    > I am writting a script that does some analysis of a log file and here
    > is what I need.
    >
    > I need to take the first variable of the line, cut it and paste it to
    > the end of the line.



    The clue's in the question. cut and paste.

    #!/bin/ksh
    paste -d' ' <(cut -d' ' -f2- infile) <(cut -d' ' -f1 infile) > outfile

    Quick and dirty.

    --
    Tony
     
    Tony Walton, Feb 11, 2004
    #3
  4. NNTP wrote:
    > I need to take the first variable of the line, cut it and paste it
    > to the end of the line.
    >
    > example:
    > input : accept blah blah blahc blah data data data
    > output : blah blah blahc blah data data data accept


    If the "variables" are space separated, in Perl you can do:

    $_ = 'accept blah blah blahc blah data data data';
    my @vars = split;
    push @vars, shift @vars;
    my $newline = join ' ', @vars;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 11, 2004
    #4
  5. NNTP

    Beardy Guest

    Tony Walton wrote:
    > NNTP wrote:
    >
    >> I am writting a script that does some analysis of a log file and here
    >> is what I need.
    >>
    >> I need to take the first variable of the line, cut it and paste it to
    >> the end of the line.

    >
    >
    >
    > The clue's in the question. cut and paste.
    >
    > #!/bin/ksh
    > paste -d' ' <(cut -d' ' -f2- infile) <(cut -d' ' -f1 infile) > outfile
    >
    > Quick and dirty.
    >


    Tony, you certainly seem to have acquired a penchant for the "... <(...)
    ...." construct :)

    I prefer:

    cat infile | sed -e 's/^\([^ ]*\).\(.*\)$/\2 \1/' > outfile

    Far more legible ;-)
     
    Beardy, Feb 11, 2004
    #5
  6. In article <>,
    (NNTP) wrote:

    > I am writting a script that does some analysis of a log file and here
    > is what I need.
    >
    > I need to take the first variable of the line, cut it and paste it to
    > the end of the line.
    >
    > example:
    > input : accept blah blah blahc blah data data data
    > output : blah blah blahc blah data data data accept
    >
    > no. of variables very and thats why I can't use a simple script like
    > the following.
    >
    > cat file | awk '{print $2,$3,$4,$5,$6,$7,$8,$1}'
    >
    > any ideas?


    awk '{first=$1; $1=""; print $0, first}' file

    sed 's/^\([^ ]*\) \(.*\)$/\2 \1/' file

    --
    Barry Margolin,
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
     
    Barry Margolin, Feb 11, 2004
    #6
  7. NNTP

    Tony Walton Guest

    Beardy wrote:
    > Tony Walton wrote:
    >
    >> NNTP wrote:
    >>
    >>> I am writting a script that does some analysis of a log file and here
    >>> is what I need.
    >>>
    >>> I need to take the first variable of the line, cut it and paste it to
    >>> the end of the line.

    >>
    >>
    >>
    >>
    >> The clue's in the question. cut and paste.
    >>
    >> #!/bin/ksh
    >> paste -d' ' <(cut -d' ' -f2- infile) <(cut -d' ' -f1 infile) > outfile
    >>
    >> Quick and dirty.
    >>

    >
    > Tony, you certainly seem to have acquired a penchant for the "... <(...)
    > ..." construct :)


    I'm on commission from the manufacturers of "<", "(" and ")".


    >
    > I prefer:
    >
    > cat infile | sed -e 's/^\([^ ]*\).\(.*\)$/\2 \1/' > outfile
    >
    > Far more legible ;-)


    But using up most of the world's meagre supply of backslashes.

    --
    Tony
     
    Tony Walton, Feb 11, 2004
    #7
  8. In article <c0dfv5$15gdkq$-berlin.de>,
    Gunnar Hjalmarsson <> wrote:

    > NNTP wrote:
    > > I need to take the first variable of the line, cut it and paste it
    > > to the end of the line.
    > >
    > > example:
    > > input : accept blah blah blahc blah data data data
    > > output : blah blah blahc blah data data data accept

    >
    > If the "variables" are space separated, in Perl you can do:
    >
    > $_ = 'accept blah blah blahc blah data data data';
    > my @vars = split;
    > push @vars, shift @vars;
    > my $newline = join ' ', @vars;


    You don't even need the join; just do:

    print "@vars\n";

    --
    Barry Margolin,
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
     
    Barry Margolin, Feb 11, 2004
    #8
  9. NNTP

    Beardy Guest

    Tony Walton wrote:
    >
    > I'm on commission from the manufacturers of "<", "(" and ")".


    No doubt that in Rich Teer's "Next Keyboard and Mouse" survey, you
    requested an extra line of three keys with *just* these symbols, to
    avoid the use of nasty things like shift keys ;-) Maybe even three extra
    buttons on the mouse :)

    >>
    >> I prefer:
    >>
    >> cat infile | sed -e 's/^\([^ ]*\).\(.*\)$/\2 \1/' > outfile
    >>
    >> Far more legible ;-)

    >
    >
    > But using up most of the world's meagre supply of backslashes.
    >


    No, OJ Simpson did that :-D
     
    Beardy, Feb 11, 2004
    #9
  10. In article <pwsWb.2647$Y%9.net>,
    Beardy <> wrote:

    > Tony Walton wrote:
    > >
    > > I'm on commission from the manufacturers of "<", "(" and ")".

    >
    > No doubt that in Rich Teer's "Next Keyboard and Mouse" survey, you
    > requested an extra line of three keys with *just* these symbols, to
    > avoid the use of nasty things like shift keys ;-) Maybe even three extra
    > buttons on the mouse :)


    Lisp Machine keyboards had additional, unshifted parenthesis keys for
    this reason. I think they were where to the right of P, where standard
    keyboards have [ and ].

    --
    Barry Margolin,
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
     
    Barry Margolin, Feb 11, 2004
    #10
  11. NNTP

    Tintin Guest

    "NNTP" <> wrote in message
    news:...
    > I am writting a script that does some analysis of a log file and here
    > is what I need.
    >
    > I need to take the first variable of the line, cut it and paste it to
    > the end of the line.
    >
    > example:
    > input : accept blah blah blahc blah data data data
    > output : blah blah blahc blah data data data accept
    >
    > no. of variables very and thats why I can't use a simple script like
    > the following.
    >
    > cat file | awk '{print $2,$3,$4,$5,$6,$7,$8,$1}'
    >


    #!/bin/sh
    a=$1
    shift
    echo "$* $1"
     
    Tintin, Feb 11, 2004
    #11
  12. NNTP

    Beardy Guest

    Barry Margolin wrote:
    > In article <pwsWb.2647$Y%9.net>,
    > Beardy <> wrote:
    >
    >
    >>Tony Walton wrote:
    >>
    >>>I'm on commission from the manufacturers of "<", "(" and ")".

    >>
    >>No doubt that in Rich Teer's "Next Keyboard and Mouse" survey, you
    >>requested an extra line of three keys with *just* these symbols, to
    >>avoid the use of nasty things like shift keys ;-) Maybe even three extra
    >>buttons on the mouse :)

    >
    >
    > Lisp Machine keyboards had additional, unshifted parenthesis keys for
    > this reason. I think they were where to the right of P, where standard
    > keyboards have [ and ].
    >


    LISP - Lots of Irritating Single Parentheses

    COBOL - Completely Over and Beyond Obvious Logic

    BASIC - Beginners are Sadistically Inclined towards C*********
    (Netiqutte precludes the final component) - and I know that BASITC would
    look crap, but it was about 1983 when I came up with the above.
     
    Beardy, Feb 11, 2004
    #12
  13. NNTP

    Beardy Guest

    Tintin wrote:
    > "NNTP" <> wrote in message
    > news:...
    >
    >>I am writting a script that does some analysis of a log file and here
    >>is what I need.
    >>
    >>I need to take the first variable of the line, cut it and paste it to
    >>the end of the line.
    >>
    >>example:
    >>input : accept blah blah blahc blah data data data
    >>output : blah blah blahc blah data data data accept
    >>
    >>no. of variables very and thats why I can't use a simple script like
    >>the following.
    >>
    >>cat file | awk '{print $2,$3,$4,$5,$6,$7,$8,$1}'
    >>

    >
    >
    > #!/bin/sh
    > a=$1
    > shift
    > echo "$* $1"
    >
    >


    That is sooooo far away from what the OP needs: it simply tries to
    rearrange the command line arguments passed to the script.

    Check the other replies for more accurate solutions ;-)
     
    Beardy, Feb 11, 2004
    #13
  14. NNTP

    Ben Morrow Guest

    Beardy <> wrote:
    > LISP - Lots of Irritating Single Parentheses


    Lots of Irrelevant Silly Parentheses

    Ben

    --
    I've seen things you people wouldn't believe: attack ships on fire off the
    shoulder of Orion; I've watched C-beams glitter in the darkness near the
    Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
    Time to die. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-|
     
    Ben Morrow, Feb 11, 2004
    #14
  15. NNTP

    Beardy Guest

    Ben Morrow wrote:
    > Beardy <> wrote:
    >
    >>LISP - Lots of Irritating Single Parentheses

    >
    >
    > Lots of Irrelevant Silly Parentheses
    >
    > Ben
    >


    Tread carefully on the contents of your .signature! Copyright rules
    could prevail.

    Check out the .signature of Barbie LeVile for ideas ;-)

    BTW: You can't have mine:

    "I'd rather have a full bottle in front of me, than a full frontal lobotomy"

    Apparently it is also a plaigiarism, but I'm *sure* I was the original
    author ;-)
     
    Beardy, Feb 11, 2004
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Schneeman
    Replies:
    16
    Views:
    541
    Daniel Bush
    Aug 27, 2008
  2. A. Fuentes

    Korn Shell vs. Perl

    A. Fuentes, Jun 30, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    113
    Tassilo v. Parseval
    Jun 30, 2003
  3. TP
    Replies:
    1
    Views:
    375
    Tad McClellan
    Oct 23, 2003
  4. John
    Replies:
    1
    Views:
    110
  5. sherifffruitfly
    Replies:
    0
    Views:
    145
    sherifffruitfly
    Apr 25, 2007
Loading...

Share This Page