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

N

NNTP

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?
 
J

James Willmore

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
 
T

Tony Walton

NNTP said:
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.
 
G

Gunnar Hjalmarsson

NNTP said:
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;
 
B

Beardy

Tony said:
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 ;-)
 
B

Barry Margolin

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
 
T

Tony Walton

Beardy said:
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.
 
B

Barry Margolin

Gunnar Hjalmarsson said:
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";
 
B

Beardy

Tony said:
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
 
B

Barry Margolin

Beardy said:
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 ].
 
T

Tintin

NNTP said:
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"
 
B

Beardy

Barry said:
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.
 
B

Beardy

Tintin said:
#!/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 ;-)
 
B

Beardy

Ben said:
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 ;-)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top