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-23-2009, 11:51 AM   #1
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Rep: Reputation: 19
how to make several columns in a text file of different lengths the same length


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 09:33 PM.
 
Old 04-23-2009, 12:53 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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?
 
Old 04-23-2009, 02:23 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 04-23-2009, 09:17 PM   #4
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
Hi Druuna,
those scripts worked like a charm!
Thanks!
Groeten uit Boston
Mike
 
  


Reply

Tags
awk, grep, paste, print


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


All times are GMT -5. The time now is 08:07 PM.

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