shell script to find filename & line count of each file, now insert this record into Oracle table

前端 未结 4 1881
暗喜
暗喜 2021-01-23 12:59

I have to find the filename available in a folder with each file line count. And then, i will have kind of two column data.

Now i have to insert this record into a orac

4条回答
  •  后悔当初
    2021-01-23 13:40

    You have two choices. Both I tested with this table structure:

    SQL> create table tbl_files(fileName varchar(20), lineCount number);
    

    First choice is generate sql script with generate insert SQL commands, and run sqlplus command line utility. I little bit modified your shell script:

    wc -l *| egrep -v " total$" | awk '{q=sprintf("%c", 39); print "INSERT INTO TBL_FILES(fileName, lineCount) VALUES (" q$2q ", " $1 ");";}' > sqlplusfile.sql
    

    After run this script file "sqlplusfile.sql" have this content:

    INSERT INTO TBL_FILES(fileName, lineCount) VALUES ('File1.txt', 10);
    INSERT INTO TBL_FILES(fileName, lineCount) VALUES ('File2.txt', 20);
    INSERT INTO TBL_FILES(fileName, lineCount) VALUES ('File3.txt', 30); 
    

    Now you can run directly sqlplus command with this file in parametr:

    sqlplus username/password@oracle_database @sqlplusfile.sql
    

    After run this script, table look like this:

    SQL> select * from tbl_files;
    
    FILENAME              LINECOUNT
    -------------------- ----------
    File1.txt                    10
    File2.txt                    20
    File3.txt                    30
    

    Note: At the end of file "sqlplusfile.sql" must be present "commit;" otherwise, you will not see data in the table.

    Second choice is to use sqlldr command line tool (this tool is part of the installation of Oracle Client)

    Again, little bit modified your script:

    wc -l *| egrep -v " total$" | awk '{print "\""$2"\"" "," $1}' > data.txt
    

    Content of file "data.txt" look like this:

    "File1.txt",10
    "File2.txt",20
    "File3.txt",30
    

    In the same directory I created the file "settings.ctl" with this content:

    LOAD DATA
    INFILE data.txt
    INSERT
    INTO TABLE TBL_FILES
    FIELDS TERMINATED BY ','
    OPTIONALLY ENCLOSED BY '"'
    (fileName, lineCount)
    

    Now you can run this command, which loads data into the database:

    sqlldr userid=user/passwd@oracle_database control=settings.ctl
    

    sqlldr utility is better choice, but in some Oracle Client installation is not present.

提交回复
热议问题