Have a list of numbers, want to write code to manipulate them


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Have a list of numbers, want to write code to manipulate them
# 1  
Old 01-05-2012
Have a list of numbers, want to write code to manipulate them

I have a list of numbers in the format:

5
4
15
65
32

I want to write code to manipulate the list and give me 100 - x such that the list looks like:

95
96
85
35
68

How do I go about doing that? Thanks!
# 2  
Old 01-05-2012
Lots of ways. This seems to easy almost. Is this homework perchance?

Anyway, how is the input? Keyed line by line until quit; In a file; on the command line; something else?



Robin
Liverpool/Blackburn
UK

Last edited by rbatte1; 01-05-2012 at 01:44 PM..
# 3  
Old 01-05-2012
No it is not. I am doing this for my phd in genetics and I do not have a computer science background, hence the stupid question.
# 4  
Old 01-05-2012
Apologies if I have been offensive, but can you answer the second question?

Also is there a preference to what language you would like any offers of code in?





Robin
# 5  
Old 01-05-2012
The input is in a text file where the numbers are listed in a single column.

I would prefer awk or sed, but other languages would also work. Thanks a lot!

Quote:
Originally Posted by rbatte1
Apologies if I have been offensive, but can you answer the second question?

Also is there a preference to what language you would like any offers of code in?





Robin
# 6  
Old 01-05-2012
Well, I'm not great on awk, but I could give you a simple shell program to do this:-

Code:
#!/bin/ksh
cat $1 | while read val
do
   ((newval=100-$val))
   echo $newval
done

You would call this from the command line giving it your input file, e.g.
Code:
myscript  input-filename

The output would be written to the screen, so you can capture it in a file if you like as below:-
Code:
myscript  input-filename  >  output-filename

Be aware not to use the same file name from input & output or you will just get an empty file.


Does that suffice?


If the input file is very large, then awk would probably process much faster, but I will leave another to suggest code for that.



Robin
Liverpool/Blackburn
UK
# 7  
Old 01-05-2012
Thanks. I copied and pasted the script into a text file called myscript.

However, when I do myscript inputfile.txt > outputfile.txt, I get the following error message:

-bash: myscript: command not found

How do I go about fixing that? Thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filter and delete numbers in a list

Hello, I've got a list of a single column numbers like 3000.66 3002.55 3062.23 3242.12 etc... I would like to delete all numbers higher than for example 3060.00 and lower than 2990.00 How can I do that? Thanks in advance (2 Replies)
Discussion started by: Board27
2 Replies

2. Shell Programming and Scripting

Comparing a list of numbers is less than a variable

Hello everyone, I want to compare a list of numbers in the file TEST01 to the variable $Post. Also remove any duplicate numbers. Create an if then statement indicating if the numbers listed in TEST01 is less than the number value of $Post then print an error message. Here is the contents of... (5 Replies)
Discussion started by: seekryts15
5 Replies

3. Shell Programming and Scripting

How search a list of numbers from a file

Hi Guys! I have two input files. I want to search each of the numbers (studentid) on inputfile1 from inputfile2 and print the studentid and section that are listed from inputfile1. See the desired output below. inputfile1.txt: studentid 261054689 266605695 269826642 264966513... (2 Replies)
Discussion started by: pinpe
2 Replies

4. UNIX for Dummies Questions & Answers

List-to-Range of Numbers

Hello, I have two columns with data that look like this: Col1 Col2 ------ ----- a 1 a 2 a 3 a 4 a 7 a 8 a 9 a 10 a 11 b 6 b 7 b 8 b 9 b 14 (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

5. UNIX for Dummies Questions & Answers

Getting unique list of numbers using grep

Hi, I am going to fetch a list of numbers that starts with "0032" from a file with a format like the given below: " 0032459999 0032458888 0032457777 0032451111 0032452222 0032453333 0032459999 0032458888 0032457777 0032451111 0032452222 0032453333 " I want to get a unique... (6 Replies)
Discussion started by: tinku
6 Replies

6. Shell Programming and Scripting

adding a list of numbers 3 by 3

i have a list of numbers like this; 124 235 764 782 765 451 983 909 ... and i want to make a sum with the first 3 of them then the next 3 and so on. 124+235+764=1123 782+765+451=1998 ... some ideas? (4 Replies)
Discussion started by: Tártaro
4 Replies

7. Shell Programming and Scripting

how to write a script contain list of questions

hi i want to write a script that contain questions upto 50..when i run the script it has to ask the questions one by one with options like a,b,c,d and user has to answer the each question and finally result (contain how many user selected correctly) please help me...i need hints to do....... (8 Replies)
Discussion started by: srinivas2828
8 Replies

8. Shell Programming and Scripting

Add a list of numbers

I need to add a list of numbers contained in a file. For example, the file would look like this: 10 290 342 5409 I need to get a total sum of all the numbers in the list. Any ideas? Thanks! (2 Replies)
Discussion started by: TheCrunge
2 Replies
Login or Register to Ask a Question