Printing the differences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing the differences
# 1  
Old 02-08-2010
Printing the differences

Dear All,

I am having a file with thousands of columns. I would like to find the difference between adjacent columns (ie $i and $(i+1)) and perform the following action:

if the difference is less than -10, then i need to print "-2";
if the difference is between 0 and <-9, then i need to print "-1";
if the difference is between 0 and < 9, then i need to print "1"; and
if the difference is more than 10, then i need to print "2".

for this i have tried the following code, but its now working and giving me the syntax error at else position. Here is the code

Code:
awk '{for(i=1; i<NF; i++) {if(($i-$(i+1)) < -10); printf "-2 "; elsif(($i-$(i+1)) > -10 && ($i-$(i+1)) <0); printf "-1 "; elsif(($i-$(i+1))>0 && ($i-$(i+1))<10); printf "1 "; else printf "2 "}} {print "\n"}' inputfile > outputfile

Can anyone help me to sort out this problem?

Expecting your reply and thanks in advance.

Regards
Fredrick.

---------- Post updated at 03:23 PM ---------- Previous update was at 02:35 PM ----------

Sorry, there was a typo error. It should be like this:

It was not working and giving me syntax error.

regards
Fredrick.
# 2  
Old 02-08-2010
Maybe try this (I don't think elsif works in awk):
Code:
awk '{for(i=1; i<NF; i++) {if(($i-$(i+1)) < -10) {printf "-2 "} else if(($i-$(i+1)) > -10 && ($i-$(i+1)) <0) {printf "-1 "} else if(($i-$(i+1))>0 && ($i-$(i+1))<10) {printf "1 "} else {printf "2 "}}} {print "\n"}' inputfile > outputfile

# 3  
Old 03-02-2010
Thank you very much for your reply cmf1985. Its working fine.

Warm regards
Fredrick.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sco Unix printing : jobs hangs in queue - printing via lp versus hpnpf

Hi, We have a Unix 3.2v5.0.5. I installed a printer via scoadmin, HP network printer manager with network peripheral name (hostname and ipadres are in /etc/hosts). This is the configuration file : Code: root@sco1 # cat configurationBanner: on:AlwaysContent types: simpleDevice:... (0 Replies)
Discussion started by: haezeban
0 Replies

2. Windows & DOS: Issues & Discussions

Linux to Windows Printing: PDF starts printing from middle of page.

We are using Red Hat. We have a issue like this: We want to print from Linux, to a printer attached to a Windows machine. What we want to print is a PDF. It prints, but the printing starts from the middle of the page. In the report, there is no space at the top but still printing starts from the... (5 Replies)
Discussion started by: rohan69
5 Replies

3. Shell Programming and Scripting

{} and ( ) differences

Can u tell the diff between the 1) $a and ${a} 2)] and ( ) 3)" " and ' ' , ` ` 4) 'a' , "a", please explain with simple example (1 Reply)
Discussion started by: mrbinoy
1 Replies

4. Shell Programming and Scripting

Differences between 2 Flat Files and process the differences

Hi Hope you are having a great weeknd !! I had a question and need your expertise for this : I have 2 files File1 & File2(of same structure) which I need to compare on some columns. I need to find the values which are there in File2 but not in File 1 and put the Differences in another file... (5 Replies)
Discussion started by: newbie_8398
5 Replies

5. UNIX for Dummies Questions & Answers

shell differences?

What is the differene between sh and ksh, if i want to write function count() { ls | wc -l } How to use in both shell means how to run this function in both the shell. correct me if my question is wrong (2 Replies)
Discussion started by: hi2_t
2 Replies

6. UNIX for Dummies Questions & Answers

Differences

Hi all, I am using korn shell. I want to know what is the difference between hey and echo commands. (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

7. Shell Programming and Scripting

Sort differences

I have a file that contains /home /opt /stand /var /usr /tmp /opt /home I need to print the lines that are unique so the output would be /stand /var /usr /tmp and omit any filesystems that are duplicates. I searched the forums but did not find anything, although i searched on... (3 Replies)
Discussion started by: insania
3 Replies

8. Solaris

Differences between Solaris 2.5 and 9

Hi Can anybody tell what are the great differences in Solaris 2.5 and 9? I am seeking information like differences in libraries, User Interface, Configuraion files, daemons, Packaging and archiving tools, Hardware supported etc. Please help me, as this is little urgent. Even if you can refer... (4 Replies)
Discussion started by: charlcy
4 Replies

9. UNIX for Advanced & Expert Users

Printing Problems in unix ... ( Bar-cdoe - Ip Printing)

Hi guys ... i need ur help with some printing problem in unix ... first prob. : i wanna print from my NCR unix to an Win NT , Ip based printing server ( HP JetDirect ) . My issue , is it possible to print directly to an Ip address from unix ? How do i make it work to get any results ?... (3 Replies)
Discussion started by: QuickSilver
3 Replies
Login or Register to Ask a Question