LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-01-2011, 04:17 AM   #1
jacky29
LQ Newbie
 
Registered: Mar 2011
Posts: 14

Rep: Reputation: 0
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
 
Old 04-01-2011, 08:08 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
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

Last edited by Nominal Animal; 04-01-2011 at 08:17 AM. Reason: Second one no longer needs the n parameter.
 
Old 04-01-2011, 08:21 AM   #3
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Quote:
Originally Posted by Nominal Animal View Post
Please tell me this is not homework.
yes it is (flagged out in another forum), and you have just done him/her a favor
 
Old 04-01-2011, 08:28 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by kurumi View Post
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.
 
Old 04-01-2011, 08:44 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by kurumi View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
edit help with .bat shell script visle Programming 1 03-28-2010 11:10 PM
How can I use Shell script to edit a data at a particular location in a txt file? leena_d Programming 30 02-08-2010 12:43 AM
How can I use Shell script to edit row 23 column 5-8 in a txt file? leena_d Linux - Newbie 4 12-14-2009 03:43 AM
bash shell script find and edit fields in a file hchoonbeng Programming 9 10-29-2008 02:13 AM
Shell script to edit a file kaash_m31 Linux - Newbie 2 06-02-2008 05:43 AM


All times are GMT -5. The time now is 09:49 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration