If Condition in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If Condition in awk
# 1  
Old 05-03-2016
If Condition in awk

Hi All,

I have the below Input:

Code:
1  700 1200  400      1300
2 2000 1000 2000 1500  600
3 1400  200 1000 1000 1200
4 1300  500  600       200


I want to modify the field 5 and field 4 as below.
If value in field 5 is null then value of field 4 should be in field 5. and then the value of field 4 should change to field 2 - field 5.

My output should look like this:

Code:
1  700 1200  300  400 1300
2 2000 1000  500 1500  600
3 1400  200  400 1000 1200
4 1300  500  700  600  200

I have tried the below code but could not bale to get results.

Code:
nawk '{
 if($5 == "") then '$5=$4';
  else
  '$5=$5';
   $4=$2-$5;
   }' sample

Could any one help me on this ?

Thanks in advance.
am24
# 2  
Old 05-03-2016
You are mixing up shell and awk command syntax.

In awk it looks like this:
Code:
if( ... ) {
   ...
}
else {
   ...
}

There is also superfluous single quotes that will give Problems when being executed.

It might be correct this way:
Code:
nawk '
{
    if($5 == "") {
        $5=$4
    }
    else {
        $5=$5
        $4=$2-$5
    }
}
' sample


Last edited by zaxxon; 05-03-2016 at 11:27 AM..
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 05-03-2016
awk can't determine empty fields with the default field separator. So make sure to find to use a reasonable one. With e.g. <TAB> as an FS, try
Code:
awk '{$5=$5?$5:$4; $4=$2-$5}1' FS="\t" OFS="\t" file
1	700	1200	300	400	1300
2	2000	1000	500	1500	600
3	1400	200	400	1000	1200
4	1300	500	700	600	200

This User Gave Thanks to RudiC For This Post:
# 4  
Old 05-03-2016
Hi Zaxxon,

I have tried as you suggested. Got an error message as below.

Code:
Unmatched '

Hi Rudi,
I have tried the code you suggested. here is the below output.

Code:
nawk '{$5=$5?$5:$4; $4=$2-$5}1' FS="\t" OFS="\t" sample
1  700 1200  400      1300                      0
2 2000 1000 2000 1500  600                      0
3 1400  200 1000 1000 1200                      0
4 1300  500  600       200                      0

Is there any other way to achieve the results ?

Regards,
am24
# 5  
Old 05-03-2016
That result is impossible with your input sample in post#1.
# 6  
Old 05-03-2016
Hi Rudi,

I agree with your comments. Here i am posting the original Input. In this input i am looking for 25th col and 24th col. I have tried the code on this as well. But not getting the results.
The output values are same as Input but with one extra column at the end containing zeros.

Input:

Code:
500  380590    8740  371850   87100  117700   88220   87570     350       0   83970     270       0  144540   44170       0   59290     220   47780  300170   80420   63180  317410  340010               0  380590  142290  238300                                                                               
501 7368320  155610 7212710 1483970 2106680 1658560 2119110   28920       0 4896920   11330       0 1308100  429470       0  272710       0  420870 5290120 2078200  549320 6819000  189030 7179290    1120 7367200 3421740 3946580 3234590 4133730 4921880 1308170    3960   11260 1123050 7009340     480    7180  351320
502  127770    3030  124740   22970   35430   29360   40010      90       0   30380     120       0   47600   16540       0   20240     130   12670   96780   30990   21670  106100  112580               0  127770   52670   75100                                                                               
503  232240    3810  228430   25300   54990   56900   95050     970       0   41030     120       0   46670   68090       0   48920     250   26190  170600   61640   61350  170890  173640               0  232240  110540  121700                                                                               
504 2917920   54770 2863150  405410  756020  731830 1024660   10050       0 1665380    1490       0  663930  196930       0  164680    3270  212190 2249400  668520  241240 2676680  130640 2787280       0 2917920 1387320 1530600 1269950 1647970 1672030  657710    3400    7710  577070 2744490     170    9180  164080
505 1828230   32700 1795530  256510  432880  459310  679530    2120       0  793870       0       0  393210  212210    5050  173290    2160  246320 1401260  426970  236130 1592100  148980 1679250       0 1828230 1039230  789000  814260 1013970  794600  389420    1390    3790  639030 1703210      20    1620  123380

Output:

Code:
500  380590    8740  371850   87100  117700   88220   87570     350       0   83970     270       0  144540   44170       0   59290     220   47780  300170   80420   63180  317410  340010               0  380590  142290  238300                                                                                                                                                                                 0
501 7368320  155610 7212710 1483970 2106680 1658560 2119110   28920       0 4896920   11330       0 1308100  429470       0  272710       0  420870 5290120 2078200  549320 6819000  189030 7179290    1120 7367200 3421740 3946580 3234590 4133730 4921880 1308170    3960   11260 1123050 7009340     480    7180  351320                                                                                         0
502  127770    3030  124740   22970   35430   29360   40010      90       0   30380     120       0   47600   16540       0   20240     130   12670   96780   30990   21670  106100  112580               0  127770   52670   75100                                                                                                                                                                                 0
503  232240    3810  228430   25300   54990   56900   95050     970       0   41030     120       0   46670   68090       0   48920     250   26190  170600   61640   61350  170890  173640               0  232240  110540  121700                                                                                                                                                                                 0
504 2917920   54770 2863150  405410  756020  731830 1024660   10050       0 1665380    1490       0  663930  196930       0  164680    3270  212190 2249400  668520  241240 2676680  130640 2787280       0 2917920 1387320 1530600 1269950 1647970 1672030  657710    3400    7710  577070 2744490     170    9180  164080                                                                                         0
505 1828230   32700 1795530  256510  432880  459310  679530    2120       0  793870       0       0  393210  212210    5050  173290    2160  246320 1401260  426970  236130 1592100  148980 1679250       0 1828230 1039230  789000  814260 1013970  794600  389420    1390    3790  639030 1703210      20    1620  123380                                                                                         0

Can you look into this once?

Thanks & Regards,
am24

Last edited by am24; 05-04-2016 at 04:16 AM..
# 7  
Old 05-03-2016
I can't replicate the behaviour you describe. In your new input sample, $24 is NOT empty in any line. $24 already HAS the difference between $2 and $25. And, as said before, with multiple spaces as field separators, you can't detect empty fields.

I'm afraid I can't help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If condition on awk

Hi All, Would you guys help me? I have a file that consists of several unstructured fields. in this file I will take the code field and count_berry field. but the position of the count_berry field is always changing.the column for code is always structured, which is found in column 6 I have... (4 Replies)
Discussion started by: kivale
4 Replies

2. Shell Programming and Scripting

Using like operator in awk if condition

Hello All, I have developed a script which selects a particular filed from a file ,trims it,searches for a particular pattern and then mail it when found. cat test_file.txt |sed -n '5,$p'|sed -e 's/ //g'|awk -F'|' '{if ($4 !="Alive") print $1,$2,$3,$4}' >> proc_not_alive.txt It is... (4 Replies)
Discussion started by: karthik adiga
4 Replies

3. Shell Programming and Scripting

awk condition

Hi Gurus, one of my current script, there is awk statement as below: awk '{a=a?a" "$3:$3}END{for (i in a) print i,a}' I don't understand what's "{a=a?a" "$3:$3}" mean? can anybody give me a brief explaination. thanks in advance. (3 Replies)
Discussion started by: ken6503
3 Replies

4. UNIX for Dummies Questions & Answers

awk - if else condition

Need help in awk command. Need to check 4th column of a file, if it is CAR then awk should print as is and if 4th column is not present, awk should print BIKE Input File content : 1,abc,55,CAR 3,bb,dd,CAR 5,ddd,tttt 8,ee,55 --------------------- Out put will look like: 1,abc,55,CAR... (6 Replies)
Discussion started by: vegasluxor
6 Replies

5. Shell Programming and Scripting

awk - Division with condition

Hi Friends, I have an input file like this cat input chr1 100 200 1 2 chr1 120 130 na 1 chr1 140 160 1 na chr1 170 180 na na chr1 190 220 0 0 chr1 220 230 nd 1 chr2 330 400 1 nd chr2 410 450 nd nd chr3 500 700 1 1 I want to calculate the division of 4th and 5th columns. But, if... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

7. Shell Programming and Scripting

awk if condition help

Hi, I need little help with awk's if condition statement. I have following code: $ ssh myRemotehost 'ps ww -fu tomcat ' | awk ' { if ($1 == "tomcat") print "tomcat (pid " $2 ") is running... "; else print "tomcat stopped or dead" }' Prints: tomcat stopped or dead tomcat (pid 12345) is... (6 Replies)
Discussion started by: davidtd
6 Replies

8. Shell Programming and Scripting

awk with multiple condition

Hi Guys, I just wanted to print all the lines execpt 1st and 3rd line. For that i wrote a awk command, awk 'NR != 1 || NR != 3 {print $0}' c.out the command is working if i give an equal to instead of not equal to. In the case of not equal to, it gives me the entire file. Can you... (18 Replies)
Discussion started by: mac4rfree
18 Replies

9. Shell Programming and Scripting

Problem with IF condition in awk

Hi all; I'm stuck with this simple awk script,i need to group the lines which the position of 28 length 3 that contains "688" into 1 group and other than "688" into another group. My problem is the script only read other than "688" and ignores the lines which contains "688". The file look... (2 Replies)
Discussion started by: ashikin_8119
2 Replies

10. Shell Programming and Scripting

awk and condition help

Hi... i just want to write a script for the follwing command "display status" and the normal output is... AN100> display status 12.13.2006 12:03:25 AN-2000-1 CC NOT PRESENT 16.50.80.49 status: Status for PACKET GLI A in the TOP / LEFT shelf of frame 0: status: The current active LAN is... (5 Replies)
Discussion started by: gini
5 Replies
Login or Register to Ask a Question