Fill the values between -500 to 500 -awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fill the values between -500 to 500 -awk
# 1  
Old 10-06-2010
Fill the values between -500 to 500 -awk

input

Code:
-200	2.4
0	2.6
30	2.8

output
Code:
-500	0
-499	0
-488	0
..........
..........
....
-200	2.4
....
...
0	2.6
...
...
30	2.8
...
......
.......
499	0
500	0

# 2  
Old 10-06-2010
Code:
#!/usr/bin/env ruby -w
# 1.9.1
h={}
File.foreach("file"){|x|a,b=x.split;h[a.to_i]=b}
(-500..500).to_a.each{|i|print (h.key?(i))?"#{i} #{h[i]}\n":"#{i} 0\n"}

Code:
 $ ruby myscript.rb


Last edited by kurumi; 10-06-2010 at 10:30 AM..
# 3  
Old 10-06-2010
If you want to use Python:

Code:
#!/usr/bin/python

import sys

fname=sys.argv[1]

kd={}
for line in open(fname):
    k,v = line.split()
    kd[k] = v
 
for i in range(-500,501):
    if str(i) in kd:
       print '%s %s' % (i,kd[str(i)])
    else:
       print '%s 0' % i

Usage:
Code:
script.py input:file

# 4  
Old 10-06-2010
Quote:
Originally Posted by quincyjones
input

Code:
-200	2.4
0	2.6
30	2.8

output
Code:
-500	0
-499	0
-488	0
..........
..........
....
-200	2.4
....
...
0	2.6
...
...
30	2.8
...
......
.......
499	0
500	0

the output is not so clear, do you need fill the data as 0 or the value such as:

Code:
200	2.4
199   2.4
...
0	2.6
1      2.6

or

Code:
200	2.4
199   0
...
0	2.6
1      0

# 5  
Old 10-06-2010
OP asked for awk script

Code:
awk -v s=-500 -v e=500 '
{
   while ( $1 > s ) print s++"\t0";
   print $1"\t"$2;
   s=$1+1;
}
END {
   while (s<=e) print s++"\t0";
} ' input


Last edited by Chubler_XL; 10-06-2010 at 09:51 PM.. Reason: typo
# 6  
Old 10-07-2010
Another awk:

Code:
awk '{a[$1]=$2}END{for (i=-500;i<=500;i++){print i,"\t",a[i]==""?"0":a[i]}}'  input_file

This User Gave Thanks to Klashxx For This Post:
# 7  
Old 10-07-2010
Thanx alot guys!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. War Stories

The 500 Mile Email

Not my story, but interesting enough to be worth posting here IMHO. (Original is here) The following is the 500-mile email story in the form it originally appeared, in a post to sage-members on Sun, 24 Nov 2002.: From trey@sage.org Fri Nov 29 18:00:49 2002 Date: Sun, 24 Nov 2002 21:03:02... (3 Replies)
Discussion started by: Corona688
3 Replies

2. Shell Programming and Scripting

Deleting all lines except last 500

Hi All, I want to write a script which first check the line counts of a file if its more than 500 it deletes rest except the last 500.. I tried sed but it looks sed counts line numbers from the head & not from tail.. May be I need a wc -l frist then apply if statement & pass on the line count... (17 Replies)
Discussion started by: ailnilanjan
17 Replies

3. Shell Programming and Scripting

AWK splitting a string of equal parts of 500 chars

Hi , can someone help me how to make an AWK code for splitting a string of equal parts of 500 chars in while loop? Thank you! (4 Replies)
Discussion started by: sanantonio7777
4 Replies

4. UNIX Desktop Questions & Answers

for loop (001 to 500)

hey, how do i create a for loop that runs for i from 001 to 500 ? i need that the zero prefix will remain so when i print "i" it will look like so: 001 002 . . 008 009 . . 058 059 . . 500 please advise. (2 Replies)
Discussion started by: boaz733
2 Replies

5. IP Networking

To add more than 500 ips

To add more than 500 ips Hi, I have a server, which has more than of 1000 ip address, but only 500 ip address are responding in the Server. Here below the error message received while restarting the named Code: Sep 22 00:25:00 ns2 named: creating IPv4 interface... (2 Replies)
Discussion started by: gsiva
2 Replies

6. Shell Programming and Scripting

To add more than 500 ips

Hi, I have a server, which has more than of 1000 ip address, but only 500 ip address are responding in the Server. Here below the error message received while restarting the named Sep 22 00:25:00 ns2 named: creating IPv4 interface eth0:595 failed; interface ignored Sep 22 00:25:00 ns2... (5 Replies)
Discussion started by: gsiva
5 Replies

7. Windows & DOS: Issues & Discussions

HTTP Status 500

Hello all, sori my english very bad,but 1 month i search about this case and nothing can help,maybe I wrong place but please if anybody can help me about this error: java.lang.ArrayIndexOutOfBoundsException: 0 >= 0 at java.util.Vector.elementAt(Vector.java:432) at... (2 Replies)
Discussion started by: fredginting
2 Replies

8. News, Links, Events and Announcements

Linux Notebook for $500

Balance 14.1" Notebook Computer with CD-ROM Drive (2 Replies)
Discussion started by: Perderabo
2 Replies
Login or Register to Ask a Question