Sponsored Content
Top Forums Shell Programming and Scripting Arithmetic (number-based) if condition Post 302613693 by viallos on Tuesday 27th of March 2012 04:28:31 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
ANSI2KNR(1)						      General Commands Manual						       ANSI2KNR(1)

NAME
ansi2knr - convert ANSI C to Kernighan & Ritchie C SYNOPSIS
ansi2knr [--filename filename] [input_file [output_file]] DESCRIPTION
--filename provides the file name for the #line directive in the output, overriding input_file (if present). If no input_file is supplied, input is read from stdin. If no output_file is supplied, output goes to stdout. There are no error messages. ansi2knr recognizes function definitions by seeing a non-keyword identifier at the left margin, followed by a left parenthesis, with a right parenthesis as the last character on the line, and with a left brace as the first token on the following line (ignoring possible intervening comments). It will recognize a multi-line header provided that no intervening line ends with a left or right brace or a semi- colon. These algorithms ignore whitespace and comments, except that the function name must be the first thing on the line. ansi2knr also recognizes adjacent string literals and concatenates them. The following constructs will confuse it: - Any other construct that starts at the left margin and follows the above syntax (such as a macro or function call). - Some macros that tinker with the syntax of the function header. - String literals whose concatenation requires rewriting their contents; e.g. "ab " "07c" is concatenated to "ab 07c", which is not correct. The --varargs switch is obsolete, and is recognized only for backwards compatibility. The present version of ansi2knr will always attempt to convert a ... argument to va_alist and va_dcl. AUTHOR
L. Peter Deutsch <ghost@aladdin.com> wrote the original ansi2knr and continues to maintain the current version; most of the code in the current version is his work. ansi2knr also includes contributions by Francois Pinard <pinard@iro.umontreal.ca>, Jim Avera <jima@net- com.com>, and Paul Eggert <eggert@twinsun.com>. 8 March 2000 ANSI2KNR(1)
All times are GMT -4. The time now is 10:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy