Unix Shell 脚本 - 如何使用位置定界符逐行读取文件中的值

Unix Shell Scripting - How to read values from a file line by line with position delimeter

我是一名 java 开发人员,没有 shell 脚本编写经验 我在 Shell 脚本中有这段代码,它基本上读取文件

if [ -f $DATA_FILE ] ; then

    # Next - convert the data into insert statements
    nawk -F\^ '
    {
        #I believe 5 is the 115th record in the data file in each lines
        if ( 5 == "NULL" )
        {
            5 = "";
        }

        output = "INSERT INTO DLRINFO";
        output = output " (C_DCS_DLR_NBR,";
        output = output " C_DLR_NBR,";
        output = output " C_LOC_COMP,";
        output = output " T_STAMP_CREATD,";
        output = output " I_CREATOR)";
        output = output " VALUES";
        output = output " (ZZZ"  "ZZZ,";
        output = output " ZZZ" 5 "ZZZ,";
        output = output " ZZZ02ZZZ,";
        output = output " current timestamp,";
        output = output " ZZZC3RZZZ);";

        print output
    }' /tmp/$MY_NAME.1.out > /tmp/$MY_NAME.2.out

实际上是逐行读取文件,其中的值由“^”分隔

现在文件格式正在更改,我需要使用位置分隔它。

旧文件格式:

"70075"^"RIVERSIDE SUBARU"^"100 CHENALL RD"^"LIT RCK"^"AR"^"72223-5981"^"A"^"5019990000"^"5014999008"^"N/A"^""^"334"^"MBF - SOUTHERN"^"T1010DC"^"D CLAIRE"^"Y"^"OWNERS INSURANCE CO"^20150501^""^""^"THE WINROCK GROUP, INC."^""^"Y"^+100.^""^""^""^""^""^^""^""^""^""^""^^""^""^""^""^""^^""^""^""^""^""^^""^""^""^""^""^^""^""^""^""^""^^""^""^""^""^""^^""^""^""^""^""^^""^""^""^""^""^^""^""^""^""^""^^""^""^""^""^""^^""^""^""^""^""^^""^""^""^""^""^^""^""^""^""^""^^"Unknown"^"00"^"Z -M"^"A"^"MARKET #999 SPECIAL ENTITIES"^"D CLAIRE-T1900DC"^"98102"^"02"

(这是数据文件的第一行。)

现在新的文件格式应该是

136324 70051 2015-02-01 36224 70900 1DR 136326 70023 2015-02-01 36326 70050 YY1WS 136328 70024 2015-02-01 36328 70061 YY1WS

(这是数据文件的第一行。) *每条记录都是固定大小的,第一条记录有 5 个字符,第二条记录有 1 个字符,它们之间没有任何分隔符。

所需的输出: 我必须捕获前五个字符的记录,并从五个字符值的位置 30 开始记录。

谢谢。

谢谢@shelter

All you may need to know is that awk, just like java has a substring function. the prototype is substr("string", stPos, len). If you now have a fixed-length record, you change your 5 references to something like .... substr([=10=],300,3). (from the whole line ([=10=]), go to postion 300, and take 3 characters worth).

这就是我要找的。 substr("string", stPos, len) 就是我所需要的