Perform Operations on One File Conditional on Data in Another File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perform Operations on One File Conditional on Data in Another File
# 1  
Old 09-19-2011
Perform Operations on One File Conditional on Data in Another File

Hello all,

I am looking for a solution to the following problem. Perl or python solutions also welcome.

Given this input:

Quote:
0.788 0.901 0.500
0.675 0.812 1
And this input:

Quote:
A A A B B B
B B B A A A
I want to get this output.

Quote:
0 0 A B 0 0
0 0 0 0 A A
The rule being that if the number in the first file is < 0.9, then the corresponding two columns on the same line in the other file are set to 0.

Thanks in advance.
# 2  
Old 09-19-2011
Code:
awk '{ ORIG=$0
          getline <"numbers"
          if ($1 < 0.9)
          {
                  $0=ORIG
                  $1=0
                  $2=0
          }
          else { $0=ORIG }

          print }' < characters

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-20-2011
Quote:
Originally Posted by Corona688
Code:
awk '{ ORIG=$0
          getline <"numbers"
          if ($1 < 0.9)
          {
                  $0=ORIG
                  $1=0
                  $2=0
          }
          else { $0=ORIG }
 
          print }' < characters

When I ran this code the output was:

Quote:
0 0 A B B B
0 0 B A A A
I ended up kludging this together:

Code:
 
awk '{ split($0,characters," ")
getline x < "numbers.txt"
split(x,numbers," ")
for (i in numbers) {
if (numbers[i] < .9) {
characters[((i*2)-1)]=0
characters[(i*2)]=0 }else{
} }
for(i = 1; i <= NF; i++) {
printf characters[i]" "
}
print ""
}' characters.txt

Which outputs:

Quote:
0 0 A B 0 0
0 0 0 0 A A
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to find data in three file and perform replace operation

Have three files. Any other approach with regards to file concatenation or splitting, etc is appreciated If column55(billngtype) of file1 contains YMNC or YPBC then pick the value of column13(documentnumber). Now find this documentnumber in column1(Billdoc) of file2 and grep the corresponding... (4 Replies)
Discussion started by: as7951
4 Replies

2. Shell Programming and Scripting

How to perform multiple operations on a number before storing to a variable?

(I am using bash) I have a command that will find the line number in a file by searching for a string where it exists. linenumber=$(grep -n "string" $FILENAME | cut -d : -fi) This returns the line number and removes the string. Now that I have the line number I want to subtract 4 from it and... (5 Replies)
Discussion started by: prodigious8
5 Replies

3. Shell Programming and Scripting

Filter on one column and then perform conditional calculations on another column with a Linux script

Hi, I have a file (stats.txt) with columns like in the example below. Destination IP address, timestamp, TCP packet sequence number and packet length. destIP time seqNo packetLength 1.2.3.4 0.01 123 500 1.2.3.5 0.03 44 1500 1.3.2.5 0.08 44 1500 1.2.3.4 0.44... (12 Replies)
Discussion started by: Zooma
12 Replies

4. Shell Programming and Scripting

Getting lines between patterns and perform operations

Hi, I'm currently dev'ing using awk and I'm currently stuck. Here's the file, with comments on "<--- ": Record <--- First Pattern Amount 1 <--- Amount on first transaction TotalSales 0 <--- Total Sales Prior from previous transactions Time 1:00:00 <--- Time... (9 Replies)
Discussion started by: Jin_
9 Replies

5. Shell Programming and Scripting

awk - sed / reading from a data file and doing algebraic operations

Hi everyone, I am trying to write a bash script which reads a data file and does some algebraic operations. here is the structure of data.xml file that I have; 1 <data> 2 . 3 . 4 . 5 </data> 6 <data> 7 . 8 . 9 . 10</data> etc. Each data block contains same number of lines (say... (4 Replies)
Discussion started by: hayreter
4 Replies

6. Shell Programming and Scripting

Perform operations as a non root user

Hi All, I need a help in the below scenario. I want to perform few operations as a non root user but those operations can be performed only by the root user. For example I need to modify /etc/hosts file as a non root user. This is just one scenario. Could you please provide... (3 Replies)
Discussion started by: kalpeer
3 Replies

7. Shell Programming and Scripting

How to search and append words in the same file using unix scripting file operations

Hi , I have a file myhost.txt which contains below, 127.0.0.1 localhost 1.17.1.5 atrpx958 11.17.10.11 atrpx958zone nsybhost I need to append words only after "atrpx958" like 'myhost' and 'libhost' and not after atrpx958zone. How to search the word atrpx958(which is hostname) only,... (5 Replies)
Discussion started by: gsreeni
5 Replies

8. Shell Programming and Scripting

Perform Lookup File In Perl From One input File

Hi Perl Guru, I'm new to Perl Programming & need some help. I need to have some kind of similar code template so I can start to work on it. I'm been reading some forums and did not find anything that are similar so I start to work on it. I've been struggling on how to start it. Any... (1 Reply)
Discussion started by: devvvt168
1 Replies

9. Shell Programming and Scripting

Can sed perform editing operations ONLY in the matched region?

Hi: Let's suppose I want to replace all the | by > ONLY when | is between . Usually (and it works) I would do something like sed -e 's/\(\*\)|\(*\]\)/\1>\2/g' where I have to "save" some portions of the matched region and use them with the \n metacharacter. I was wondering if I could... (2 Replies)
Discussion started by: islegmar
2 Replies

10. UNIX for Dummies Questions & Answers

File operations

Hi I have a tab delimited file with 3 fields. I need to sort this file on the first field and remove all the records where the first field has dulplicates. For eg my file is 133|arrfdfdg|sdfdsg 234|asfsdgfs|aasdfs 133|affbfsde|dgfg When this file gets sorted I need the result to be ... (2 Replies)
Discussion started by: monks
2 Replies
Login or Register to Ask a Question