cancel
Showing results for 
Search instead for 
Did you mean: 

About the script writing

eric_204
Frequent Advisor

About the script writing

I have the following report on the shell , how can I cut the field of last two columns and rows ( 49 , 50 , 59 , 60 ) and paste it to other file ? thx

#vi system1.txt
01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
12 REPLIES
Stuart Browne
Honored Contributor

Re: About the script writing

awk is your friend.

awk 'END {
printf( "%s, %s, %s, %s\n", PrevOne, PrevTwo, LastOne, LastTwo );
}
{
PrevOne = LastOne;
PrevTwo = LastTwo;
LastOne = $(NF - 1);
LastTwo = $(NF);
}' < system1.txt

Not the most neat/elegant, but it works.
One long-haired git at your service...
eric_204
Frequent Advisor

Re: About the script writing

can I simply use the "cut" comamand ? thx
Stuart Browne
Honored Contributor

Re: About the script writing

You possibly could, if it was always a fixed number of fields, and a fixed number of lines, in combination with 'tail'.

tail --lines=2 system1.txt | cut -f9-10

would be close, but not in the format listed above.
One long-haired git at your service...
Huc_1
Honored Contributor

Re: About the script writing

Very neat..!
had been trying my hand at it when you can out with it...

J-P (0 point for this just a comment, thanks.)
Smile I will feel the difference
Mark Grant
Honored Contributor

Re: About the script writing

Actually, that last cut thing doesn't work on Linux. At least not in my version of cut because the defaukt delimiter is a tab.

tail -2 system1.txt | cut -d " " -f 9-10

works for me though.
Never preceed any demonstration with anything more predictive than "watch this"
eric_204
Frequent Advisor

Re: About the script writing

thx all reply , another question ,
how can i cut this characters "49,50,59,60" from the file "a.txt" and then paste it to another file "b.txt" in a specific position ? thx

$vi b.txt
<----------blank---------->
<--blank->49 50<--blank--->
<--blank->59 60<--blank--->
Mark Grant
Honored Contributor

Re: About the script writing

eric,

I think we are now moving away from the realm of one liners :)

Could you be a bit more specific about where in the second file you want the number to appear.
Never preceed any demonstration with anything more predictive than "watch this"
eric_204
Frequent Advisor

Re: About the script writing

hi Mark ,

I just want the characters paste to the third column ( leave the first two column blank ) , how to do it ? thx.
Mark Grant
Honored Contributor

Re: About the script writing

This works, though it might be better to put it in a script of it's own rather than just running it from the command line

tail -2 a.txt | cut -d " " -f 9-10 | {
echo
while read a
do
echo " $a"
done
echo
} > b.txt
Never preceed any demonstration with anything more predictive than "watch this"
Mark Grant
Honored Contributor

Re: About the script writing

Actually, the thing I just posted above assumes you are starting with your original system1.txt
file, not just those four numbers.
Never preceed any demonstration with anything more predictive than "watch this"
eric_204
Frequent Advisor

Re: About the script writing

thx Mark, my second requirement is paste the numbers to the third column , could suggest how to do it ? thx.
Mark Grant
Honored Contributor

Re: About the script writing

Hi eric,

I think you just need to have an appropriate number of spaces before the $a in this bit

echo "<-spaces-> $a"

Unless I am misunderstanding you.
Never preceed any demonstration with anything more predictive than "watch this"