需要帮助 - Perl,sort,sed AWK和Bash

时间:2015-07-26 04:53:01

标签: bash perl sorting awk sed

我有一些使用AWK,sed或Perl的示例代码。

我有一个输入文本文件,它是以制表符分隔的。该文件具有N个列/行,列名称是唯一的。

第二列有一些数字或空值。在第二列中必须将零值替换为零,然后执行降序排序。

排序后,必须在开头插入新列,并将它们排列为1,2,3。在输出中,前三列始终是强制性的,与它们一起,应打印所需的列。

    itemNumber  Price   Mango   Apple   Bannana
    112201      purchased   need-to-plan    purchased
    112202  55  yet-to-buy  yet-to-buy  purchased
    112202  67  need-to-plan    purchased   purchased
    112203  456 need-to-plan    need-to-plan    need-to-plan
    112203  33  need-to-plan    yet-to-buy  need-to-plan
    112204  456 need-to-plan    yet-to-buy  need-to-plan
    112204      yet-to-buy  purchased   need-to-plan
    112205  77  yet-to-buy  purchased   need-to-plan
    112205  99  yet-to-buy  purchased   yet-to-buy
    112206  0   yet-to-buy  purchased   yet-to-buy

run命令将类似于:

runfile.sh 'filename' 'Apple' 'need-to-plan' 50

代码应选择“Mango”列并仅过滤“buy”并将输出限制为50行。

所需的输出(有TABS):

itemNumber  Price   Apple
112203  456 need-to-plan
112201  0   need-to-plan

1 个答案:

答案 0 :(得分:2)

你所尝试的是正确的,但有些时候AWK没有读取/识别标签来划分字段。

查看您修改后的代码版本:

cat fllename.txt | tr '\011' '~' | awk -F "~" '{ print $1, $2, $4}'