LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script to edit a file (https://www.linuxquestions.org/questions/programming-9/shell-script-to-edit-a-file-872321/)

jacky29 04-01-2011 04:17 AM

shell script to edit a file
 
i have a file called number which contains data as



1 2 3 4 5
6 7 8 9 0
9 8 7 6 5
4 3 2 1 0

0 1 2 3 4

needed a shell script to print the output as

1
7
7
1
4
and

1 2 3 4 5
6 7 8 9
9 8 7
4 3
0

Nominal Animal 04-01-2011 08:08 AM

Please tell me this is not homework.
Awk solutions are quite simple:
Code:

awk '(NF>i) { i++ ; print $i }' number
Code:

awk '(NF>0) { if (NF>m && n<1) { n=NF; m=NF } ; printf("%s", $1); for (i=2; i<=n; i++) printf(" %s", $i); printf("\n"); n-- }' number

kurumi 04-01-2011 08:21 AM

Quote:

Originally Posted by Nominal Animal (Post 4310671)
Please tell me this is not homework.

yes it is (flagged out in another forum), and you have just done him/her a favor

colucix 04-01-2011 08:28 AM

Quote:

Originally Posted by kurumi (Post 4310692)
and you have just done him/her a favor

Quite the opposite! ;)

@jacky29: a good attitude here would be to show what have you tried so far and ask for some specific questions if stuck at some point. Not simply ask "Do a script for me!". If this is really homework, you have to show your effort in the learning process and the willingness to solve the problem by yourself, not only to copy homework from your friends. Nuff' said.

Nominal Animal 04-01-2011 08:44 AM

Quote:

Originally Posted by kurumi (Post 4310692)
yes it is (flagged out in another forum), and you have just done him/her a favor

Thanks, Kurumi, for the heads up. I seriously hate cheaters. I will be ignoring jacky29 from now on.


All times are GMT -5. The time now is 11:19 PM.