Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

awk if else statement

# Basic syntax:
awk 'BEGIN {if(condition) outcome}' # if
awk 'BEGIN {if(condition_1) outcome_1; else outcome_2}' # if else
awk 'BEGIN {if(condition_1) outcome_1; else if(condition_2) outcome_2}' # if else if

# Example usage:
awk 'BEGIN {if(2>3) print "1_is_true"; else print 6}'
--> 6

# For multi-line and ternary operator syntax, see source
Comment

awk if

ls -l | awk '{ if($5==0) print $9}' 
-rw-rw----    1 nkumarachchi2019 nkumarachchi2019        0 Dec 15 16:47 jobs.cancel

## eg 1 ####################
awk '{
if($3 <= 40)
{
print "The salary of ",$1, " is ", $4, "
"
}
else
{
print "The age of ",$1, " is ", $3, "
"
}
 
}' employee.txt

###########################
## eg 2 #################
{
name=$1;
if ( name=="JACKSON" ) color="Blue";
else if (name=="MARTIN") color="Black";
else if (name=="LILY") color="Red";
else if (name=="ROBINSON") color="White";
else color="None";
 
if(color!="None") print "The favorite color of ", name, "is ", color;
else print "No person found";
 
}
#########################
https://linuxhint.com/conditional_statement_awk_command/



eg : 
ls -ltrd p_390.q_50 | awk '{split($9,a,"."); split(a[1],b,"_"); if(b[2]>360) c=b[2]-360; print("z_"c"."a[2])}'
 ls -ltrd p_390.q_50 | awk ' {split($9,a,"."); split(a[1],b,"_"); if(b[2]>360) {c=b[2]-360; print("mv "$9" z_"c"."a[2])} else {print ("mv "$9" "a[1]"."a[2])} }'
gives you : 
mv p_390.q_50 p_30.q_50
Comment

PREVIOUS NEXT
Code Example
Shell :: nvm install stable not found 
Shell :: GREPCC token 
Shell :: Stack found this candidate but arguments dont match 
Shell :: install apache on mac 
Shell :: how to list running processes in linux 
Shell :: install from tar gz file unix 
Shell :: dns_probe_finished_nxdomain linux ubuntu 
Shell :: java path ubuntu 20.04 
Shell :: list of files in git commit 
Shell :: git display unrelated histories 
Shell :: how to use dotenv in javascript 
Shell :: how to run deb file 
Shell :: git list all remote branches 
Shell :: see map size linux 
Shell :: how to make all directory 775 
Shell :: upgrade seaborn version 
Shell :: how to run a command on startup in linux ubuntu or centos 
Shell :: install docker fedora 
Shell :: git reset back to previous pushed commit 
Shell :: git force sync with remote 
Shell :: virtual host apache2 
Shell :: imagemagick add background to transparent png 
Shell :: epub linux reader 
Shell :: ubuntu large text 
Shell :: reset gui linux 
Shell :: docker update all images 
Shell :: composer xampp windows 
Shell :: sudo snap linux store 
Shell :: tar.gz files 
Shell :: ext-dom missing ubuntu 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =