Script regarding non numerical or empty value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script regarding non numerical or empty value
# 1  
Old 02-15-2017
Script regarding non numerical or empty value

Hello Team,

I need bash script to check if my output is non numerical or empty. if its then my output should display default value as 0

basically, I am reading value from txt file. most of numerical value, in case there is no numerical value or its empty, then my output should be 0.

example like below:
Code:
output=`sed '1!d' $SOURCE_FOLDER/$datafile |awk '{print $6}'`
          data="lines1=$output "

          output=`sed '1!d' $SOURCE_FOLDER/$datafile |awk '{print $7}'`
          data+="lines2=$output "

          echo "file value|$date"

Br,Pradeep

Last edited by ghpradeep; 02-15-2017 at 07:32 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 02-15-2017
In lieu of two sed invocations followed by an awk each, you could try a single awk:
Code:
awk '{print "lines1=" ($6==$6+0?$6:0) " lines2=" ($7==$7+0?$7:0); exit}' file3
lines1=41531.0 lines2=52529.0
awk '{print "lines1=" ($6==$6+0?$6:0) " lines2=" ($7==$7+0?$7:0); exit}' logfile
lines1=0 lines2=0


Last edited by RudiC; 02-15-2017 at 06:09 PM.. Reason: typo
This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-15-2017
Another approach using awk:

Code:
awk '{printf("lines1=%d lines2=%d\n", $6+0, $7+0); exit }' infile

Originally I was going to use NR==1 to match the first line but RudiC's approach using exit should be much more efficient when the input file has a large number of lines.

Replace %d format specifiers with %f, %.2f, etc if you have non-integer values.
This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 02-15-2017
Nice approach! In fact, with the %d format specifier, you don't need the +0 as both an empty or a string field would result in a 0 to be printed:
Code:
cf file[1-3]
file1:
/ home k abc txt ABC DEF 
file2:
/ home k abc txt 92 1023
file3:
AB CD EF
awk '{printf("lines1=%d lines2=%d\n", $6, $7); exit }' file1
lines1=0 lines2=0
awk '{printf("lines1=%d lines2=%d\n", $6, $7); exit }' file2
lines1=92 lines2=1023
awk '{printf("lines1=%d lines2=%d\n", $6, $7); exit }' file3
lines1=0 lines2=0

The only drawback is when a field has a string starting with numeric characters like
Code:
cat file4
A B C D E F23 24G H

Your approach:
Code:
awk '{printf("lines1=%d lines2=%d\n", $6+0, $7+0); exit }' file4
lines1=0 lines2=24

My approach:
Code:
awk '{print "lines1=" ($6==$6+0?$6:0) " lines2=" ($7==$7+0?$7:0); exit}' file4
lines1=0 lines2=0

The OP needs to decide which one would satisfy his/her needs.
# 5  
Old 02-16-2017
Thanks all for reply, I have quick question here. how about having multiple lines in single file like below.
.
Code:
e.g
100 4  5 101 400
 900 1245 789 P

I want to read multiple lines from files and display output related each field.
my output should be like this.
Code:
process1= 100
process2 = 900
process3 = 789 
process4 = 0

process4 will be 0 since its having non numerical value.

# 6  
Old 02-16-2017
Awk has a built in variable NR which matches the row number of the file so you can test this when extracting your process numbers:

Code:
awk '
NR==1 { printf("process1= %d\n", $1+0) }
NR==2 { 
   printf("process2 = %d\n", $1+0)
   printf("process3 = %d\n", $3+0)
   printf("process4 = %d\n", $4+0)
   exit
}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to remove empty files

Hi All, Can anyone please write a shell script to remove the empty files using an if condition. please help me out , urgent thanks (6 Replies)
Discussion started by: muthi_murali
6 Replies

2. UNIX for Advanced & Expert Users

$HOSTNAME empty while invoking script using SSH

Hi, I am unable to get $HOSTNAME value in a remote script when executed through SSH. Also the scripts hangs and doesnt return to the calling environment. For instance command - ssh user1@box2 "cd /var/home/bin ; ./generateScript.sh" Can any one plese throw light on the issue and... (5 Replies)
Discussion started by: mihirvora16
5 Replies

3. Shell Programming and Scripting

Script sometimes creates an empty file

I have a script that runs ditto for me, and occasionally (if I exit the script while ditto is running in the background) it will leave an empty file named 0 in the script's directory. The next time I run the script, it generates incorrect data because of this file. I know I can easily insert a... (1 Reply)
Discussion started by: reid
1 Replies

4. Shell Programming and Scripting

Script sometimes creates an empty file

I have a script that runs ditto for me, and occasionally (if I exit the script while ditto is running in the background) it will leave an empty file named 0 in the script's directory. The next time I run the script, it generates incorrect data because of this file. I know I can easily insert a... (1 Reply)
Discussion started by: reid
1 Replies

5. Shell Programming and Scripting

Script to delete empty files

I'm trying to write a shell script to files of zero length in a specified directory, but I keep getting errors. Would anybody be kind enough to look it over for issues? Thanks a bunch in advance. #!/bin/sh if then if then find $1 -type f -size 0 -print|xargs rm exit 0... (1 Reply)
Discussion started by: ScriptingIssues
1 Replies

6. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

7. Shell Programming and Scripting

script to sort a string of numerical data in set fields

So, I will be working with someone and basically we are trying to build a form that is submitted most likely via the web and the data is just a string of numbers. like: 19383882872201110929282821818182827349190102837364718191001932873711 Now, each number is part of a numerical value of... (4 Replies)
Discussion started by: tlarkin
4 Replies

8. Shell Programming and Scripting

Script error with when input is empty

Hi, I created the following shell script to lookup multiple name servers for a domain. #!/bin/bash echo -n "Enter the domain name and press : " read domain result1=`dig +short $domain @4.2.2.1` revresult1=`host $result1 | cut -f5 -d " "` echo "test1" result2=`dig +short... (3 Replies)
Discussion started by: mohitmoudgil
3 Replies

9. UNIX for Dummies Questions & Answers

Getting same exit status for empty and non empty file

Hi All, I am checking for a empty input file to do some further action , but I am getting exit status 0 in both the cases , for empty and non empty file both. The value of $? is coming 0 in if part also and else part too. #!/bin/ksh if ]; then echo "data" # exit 0 echo "$?" else... (4 Replies)
Discussion started by: mavesum
4 Replies

10. UNIX for Dummies Questions & Answers

Shell Script using Join, empty field help!

Deleted#### (1 Reply)
Discussion started by: tibbyuk
1 Replies
Login or Register to Ask a Question