Search code examples
unixcommandcut

How to variables to cut command in unix for -f option


I need to parse a sql file and pull all the statements.

For example sample.sql is like this

alter table add column(c1 varchar(20));
alter table add column(c2 varchar2(10));

Now i want to write a script in the below manner.

#!/bin/ksh
for file in sample.sql
do
    echo $file
    no_of_statement=`grep ";" $file|wc -l`
    echo $no_of_statement
    iterator=1
    statement=""
    while [ $iterator -le $no_of_statement]
        do
            echo "Inside While Loop"
            statement=`cat $file`
            statement1=`echo $statement | cut -d";" -f "${iterator}"`
            echo $statement1
            iterator=$iterator+1
        done
done

Solution

  • If you change [ $iterator -le $no_of_statement] to [ $iterator -le $no_of_statement ]

    and iterator=$iterator+1 to iterator=$(($iterator+1))

    It prints the queries:

    sample.sql
    2
    Inside While Loop
    alter table add column(c1 varchar(20))
    Inside While Loop
    alter table add column(c2 varchar2(10))