0

我有一个输入文件,其内容格式如下:

ABCD     XYZAB   1234
PQRSTUV  STU     SKIP
LMN      OPRQM   8966
RSTUV    OPM     SKIP
TUV      ZXU     SKIP

我想解析这个文件并将其输出到一个新文件,这样,最后一列为“SKIP”的行不会出现在最终输出文件中,如下所示:

ABCD     XYZAB   1234
LMN      OPRQM   8966

为了处理上述内容,我编写了以下 shell 脚本:

while read -r col1 col2 col3
do
    if [ "$col3" != "SKIP" ]
    then
    printf "%s %s %s\n" "$col1" "$col2" "$col3";
    fi
done <input.txt > output.txt

但是上面的脚本似乎不起作用,我的输出文件仍然继续包含所有行,最后一列中有“SKIP”。我不明白为什么“条件”失败了?

谢谢,vj

4

2 回答 2

1

Ok it seems that though the script is correct, the input file being edited was in Windows environment. As a result the line endings were "\r" and not "\n". So, there was a mismatch there. So, the conditional check failed as well. Once I converted the line endings to UNIX format, the output was obtained as desired. Thanks for everyone's help.

Vj

于 2013-03-30T02:53:19.940 回答
0

VJ - 我看不出你的剧本有什么问题。我粘贴了您的代码和输入,结果正是您想要的。请参阅我的笔记本电脑“ttt”的以下证明:

[ttt:/m/temp/stack] 猫 vjvj

    同时读取 -r col1 col2 col3
    做
        如果 [ "$col3" != "SKIP" ]
        然后
        printf "%s %s %s\n" "$col1" "$col2" "$col3";
        菲
    完成输出.txt

[ttt:/m/temp/stack] 猫输入.txt
ABCD XYZAB 1234
PQRSTUV STU 跳过
LMN OPRQM 8966
RSTUV OPM 跳过
TUV ZXU SKIP
[ttt:/m/temp/stack] 猫输出.txt
ABCD XYZAB 1234
LMN OPRQM 8966
[ttt:/m/temp/stack] 猫 /etc/lsb-release

    DISTRIB_ID=LinuxMint
    DISTRIB_RELEASE=13
    DISTRIB_CODENAME=玛雅
    DISTRIB_DESCRIPTION="Linux Mint 13 玛雅"

[ttt:/m/temp/stack]

于 2013-03-30T01:25:01.977 回答