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
tell(1T)						       Tcl Built-In Commands							  tell(1T)

__________________________________________________________________________________________________________________________________________________

NAME
tell - Return current access position for an open channel SYNOPSIS
tell channelId _________________________________________________________________ DESCRIPTION
Returns an integer string giving the current access position in channelId. This value returned is a byte offset that can be passed to seek | in order to set the channel to a particular position. Note that this value is in terms of bytes, not characters like read. The value returned is -1 for channels that do not support seeking. ChannelId must be an identifier for an open channel such as a Tcl standard channel (stdin, stdout, or stderr), the return value from an | invocation of open or socket, or the result of a channel creation command provided by a Tcl extension. EXAMPLE
Read a line from a file channel only if it starts with foobar: # Save the offset in case we need to undo the read... set offset [tell $chan] if {[read $chan 6] eq "foobar"} { gets $chan line } else { set line {} # Undo the read... seek $chan $offset } SEE ALSO
file(1T), open(1T), close(1T), gets(1T), seek(1T), Tcl_StandardChannels(3TCL) KEYWORDS
access position, channel, seeking ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +--------------------+-----------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +--------------------+-----------------+ |Availability | SUNWTcl | +--------------------+-----------------+ |Interface Stability | Uncommitted | +--------------------+-----------------+ NOTES
Source for Tcl is available on http://opensolaris.org. Tcl 8.1 tell(1T)
All times are GMT -4. The time now is 02:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy