LinuxQuestions.org
Have you listened to LQ Radio?
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

Tags used in this thread
Popular LQ Tags , , ,

Reply
 
Thread Tools
Old 04-23-2009, 12:51 PM   #1
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 4.6 x86_64 GNU/Linux
Posts: 34
Thanked: 0
how to make several columns in a text file of different lengths the same length


[Log in to get rid of this advertisement]
Hi there,
I have a matrix of numbers separated by a space in a text file that looks like this:

Code:
2 3 9 8 9 0 
6 9 0 8 5 
0 9 8 7 
6 5 4
1 3
5
(FYI: I don't have a 6x6 matrix, as the example above, but 400x400)

Now I want to fill the total matrix with zero's in the lower triangle:

Code:
2 3 9 8 9 0 
6 9 0 8 5 0
0 9 8 7 0 0
6 5 4 0 0 0
1 3 0 0 0 0
5 0 0 0 0 0
One more piece of info: I also have each column in it's own text file, for instance the second column:

Code:
3
9
9
3
3
If I could get a script to go into the file of that second column and change the 5 rows into 6 rows and placing a "0" in the empty row, this second file could change into this:

Code:
3
9
9
3
3
0
that would also be of great help.


Thanks for any help/tips.

Mike
(I'm comfortable with simple grep, sed, awk, and tr scripts but not a hard line programmer)

Last edited by Mike_V; 04-23-2009 at 10:33 PM..
Mike_V is offline  
Tag This Post , , ,
Reply With Quote
Old 04-23-2009, 01:53 PM   #2
druuna
Senior Member
 
Registered: Sep 2003
Location: netherlands
Distribution: lfs
Posts: 3,269
Thanked: 78
Hi,

This should take care of the first problem (filling out matrix with zero's):

awk '{ for (x = 1; x <= 25; x++) printf("%d ", $x)}{ printf("\n")}' infile

With the counter set to 25 (crank it up to 400 if all is well), here's a testrun:
Code:
$ cat infile 
2 3 9 8 9 0 
6 9 0 8 5 
0 9 8 7 
6 5 4
1 3
5

$ awk '{ for (x = 1; x <= 25; x++) printf("%d ", $x)}{ printf("\n")}' infile 
2 3 9 8 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
6 9 0 8 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 9 8 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
6 5 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Hope this helps.

PS: What are the names of the files that you want changed?
druuna is offline     Reply With Quote
Thanked by:
Old 04-23-2009, 03:23 PM   #3
druuna
Senior Member
 
Registered: Sep 2003
Location: netherlands
Distribution: lfs
Posts: 3,269
Thanked: 78
Hi again,

This could be the solution for your second problem:

awk '{print $1 }END {for (y = NR+1; y<= 6; y++) print "0" }' column.file

Test run:
Code:
$ cat column.file
1
2
3

$ awk '{print $1 }END {for (y = NR+1; y<= 6; y++) print "0" }' column.file
1
2
3
0
0
0
Adjust the counter to your liking.
druuna is offline     Reply With Quote
Thanked by:
Old 04-23-2009, 10:17 PM   #4
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 4.6 x86_64 GNU/Linux
Posts: 34
Thanked: 0

Original Poster
Hi Druuna,
those scripts worked like a charm!
Thanks!
Groeten uit Boston
Mike
Mike_V is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
bash/sed/awk fill each line in text file with space to fixed length khairil Programming 11 01-09-2008 06:28 AM
All the text is two columns off after first boot of LFS 6.2 creativename Linux From Scratch 3 09-09-2007 11:56 PM
sed: delete text till <pattern2> depending on length of text oyarsamoh Programming 2 05-05-2007 02:40 AM
delete columns 15 to 27 of a text file powah Programming 1 01-31-2007 03:03 AM
storing text in a text file for a specified length of time. mrobertson Programming 7 08-02-2005 11:27 AM


All times are GMT -5. The time now is 02:07 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration