Arithmetic (number-based) if condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arithmetic (number-based) if condition
# 1  
Old 03-27-2012
Arithmetic (number-based) if condition

Hi Folks

I'm looking for help with if statement.

I'm reading the file with header (starts with 0 on position 1 in the line) and data (starts with 1 on position 1 in the line).
I have to check if the number from header (should be number of data rows) equal actual count of the data rows.

When both equal than extrsct specific data and write sql statement into the file that will be further processed.

File is as follows:

Quote:
07
1ABC123456000COIN12345
1ABC123466000COIN12345
1ABC123476000COIN12345
1ABC123486000COIN12345
1ABC123496000COIN12345
1ABC123506000COIN12345
1LST123516000COIN12345
Script as follows

Code:
input_file="file.txt"
output_file="Query.sql"
table_name="file_history"

row_header=`head -1 $input_file | cut -c2-10`
row_count=`grep -c "^1" $input_file`

make_file()
{
echo "INSERT ALL"
grep "^1" $input_file | cut -c2-10 | while IFS= read;do printf "Into %s values ('%s',SYSTIMESTAMP) \n" $table_name "$REPLY";done
echo "SELECT * FROM dual;"
}

if [ $row_count -eq $row_header ]; then
{
grep "^1" $input_file | cut -c2-10 | while read line; do 
    echo $line
done
make_file > $output_file
echo "File has $row_count data rows"
}

elif [[ -e $output_file ]]; then
{
echo "Data corrupt. Incorrect number of rows in the file. Query file deleted"
rm $output_file
}

else
echo "Data corrupt. Incorrect number of rows in the file. Query file do not exist"

fi

When I run the script I'm getting following error

Quote:
: integer expression expected
Data corrupt. Incorrect number of rows in the file. Query file do not exist
however it should equal as:
row_header=`head -1 $input_file | cut -c2-10` is 7
row_count=`grep -c "^1" $input_file` is also 7.

When I change code to
Code:
if [ $row_count -eq 7 ]; then

than it is working fine but I can't hardcode this variable.

Thanks for help
# 2  
Old 03-27-2012
This will take an input file and only print those data blocks that meet the criteria you defined. Run against this file with one good block and two bad ones:
Code:
07
1ABC123456000COIN12345
1ABC123466000COIN12345
1ABC123476000COIN12345
1ABC123486000COIN12345
1ABC123496000COIN12345
1ABC123506000COIN12345
1LST123516000COIN12345
07
1ABC123466000COIN12345
1ABC123476000COIN12345
1ABC123486000COIN12345
1ABC123496000COIN12345
1ABC123506000COIN12345
1LST123516000COIN12345
05
1ABC123456000COIN12345
1ABC123466000COIN12345
1ABC123476000COIN12345
1ABC123486000COIN12345
1ABC123496000COIN12345
1ABC123506000COIN12345
1LST123516000COIN12345

gives
Code:
07
1ABC123456000COIN12345
1ABC123466000COIN12345
1ABC123476000COIN12345
1ABC123486000COIN12345
1ABC123496000COIN12345
1ABC123506000COIN12345
1LST123516000COIN12345

Code:
awk ' /^0/ { if(start && start==lcount){ printf("%02d\n%s", start,lines)}
             start=$0
             lines=""
             lcount=0
           } 
     /^1/ {lcount++; lines=lines sprintf("%s\n",$0) }
     END {if (start==lcount ){ printf("%02d\n%s", start,lines) }} ' filename

# 3  
Old 03-27-2012
Your data file is in DOS format (ie ^M on the end of lines). Try running it thru dos2unix first.

Or do it all with awk:

Code:
awk -v table=file_history -v outfile=Query.sql '
  /^0/ {c=substr($0,2) }
  /^1/ {L[g++]=substr($0,2)}
  END{
    if(g!=c)
        print "Data corrupt. Incorrect number of rows in the file"
    else {
      print "INSERT ALL" > outfile
      for(i=0;i<g;i++)
        printf "Into %s values (%c%s%c, SYSTIMESTAMP)\n",table,39,L[i],39 >> outfile
      print "SELECT * from dual;" >> outfile
  }}' file.txt


Last edited by Chubler_XL; 03-27-2012 at 11:52 PM..
# 4  
Old 03-28-2012
Thank you both.

dos2unix seems to resolve my initial issue, however both of you suggested to use awk.

I tested scripts and both work fine.

Is there any advantage to use awk rather then other commands?
# 5  
Old 03-28-2012
awk offers a slight performance improvement, as each time the shell executes an external command like head, grep, cut and with some shell/OS printf, the program code must be loaded from disk and a new process forked to execute it, this consumes resources. Also in your current solution, the data file is being opened a read in at least four times by various commands, so there is some wasted effort there.

However, if the data file isn't overly large and the script isn't executed frequently there is no real issue. What is much more important is that you understand how your script works and can easily support/change it in the future.

It looks like you have a pretty good grip on shell programming so I doubt your going to find awk confusing or difficult and I'm sure if you start using it regularly you quickly grow to love it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List files with number to select based on number

Hi experts, I am using KSH and I am need to display file with number in front of file names and user can select it by entering the number. I am trying to use following command to display list with numbers. but I do not know how to capture number and identify what file it is to be used for... (5 Replies)
Discussion started by: mysocks
5 Replies

2. Shell Programming and Scripting

Copy down based on condition

Hello: I need to copy down some data from the previous record in to the next record based on the below conditions If position 41- 59 of the current record is same as the previous record and the value of position 62 is not equal to 1 then copy the previous records value for positions... (1 Reply)
Discussion started by: techedipro
1 Replies

3. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

4. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

5. Shell Programming and Scripting

How do I assign number with some condition?

My data file looks like this location_z 2399 167 1 9 72.92 i 17-Oct-2011 20:11:00 location_z 2399 167 2 9.75 72.77 i 18-Oct-2011 00:25:00 location_z 2399 167 3 10.57 72.75 i 18-Oct-2011 18:24:00 location_b ... (10 Replies)
Discussion started by: Akshay Hegde
10 Replies

6. Shell Programming and Scripting

Comment based on a condition

I want to comment 2 lines based on a condition. If THEN occurs immediately after WHEN then i have to comment both the lunes For example : $cat file1.txt CASE WHEN THEN 1 WHEN c1= 'I' AND c2= '0' THEN 2 So in this example i want to... (2 Replies)
Discussion started by: ashwin3086
2 Replies

7. Shell Programming and Scripting

Condition based concatenation.

Hello, I am looking for concatenating the lines based on conditions. Below are the contents of the file: Infile: ----- Test1.PO_Itm COLUMN GAC_DT. Test1.PO_Itm COLUMN (PRODTCD ,PLNTCD). Test1.PO_Itm COLUMN PLNTCD. Test1.PO_Itm COLUMN ACTVIND. Test2.RgnToTerrtryGPI COLUMN... (3 Replies)
Discussion started by: indrajit_u
3 Replies

8. Programming

multiplying a number by two without using Arithmetic and Shift operators

Hi All, Is it possible to multiply a number by two without using Arithmetic and Shift operators? _Thanks (5 Replies)
Discussion started by: rvan
5 Replies

9. Shell Programming and Scripting

transpose based on condition

Hi, I have the oracle table coulns in an order like date, state1, state2....state9 and i need to prepare data from the script output for loading in to this table The script is #!/bin/ksh /usr/xpg4/bin/awk -F"-" '{print $2,$4}' /aemu/ErrorLogs/data/MissingCGIcount.txt |... (5 Replies)
Discussion started by: aemunathan
5 Replies

10. Shell Programming and Scripting

searching and storing unknown number of lines based on the string with a condition

Dear friends, Please help me to resolve the problem below, I have a file with following content: date of file creation : 12 feb 2007 ==================== = name : suresh = city :mumbai #this is a blank line = date : 1st Nov 2005 ==================== few lines of some text this... (7 Replies)
Discussion started by: swamymns
7 Replies
Login or Register to Ask a Question