Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix awk改进未格式化输入的格式_Unix_Awk - Fatal编程技术网

Unix awk改进未格式化输入的格式

Unix awk改进未格式化输入的格式,unix,awk,Unix,Awk,想知道如何将下面的未格式化输入改进为格式化输出。 我可以通过以下步骤做到这一点。。。 实际输入文件包含18个字段 $cat st_Input.txt Total No. of Records Displayed: 4 --------------------------------------------------------------------------------------- | Circle Desc.|Serial From |Serial To

想知道如何将下面的未格式化输入改进为格式化输出。 我可以通过以下步骤做到这一点。。。 实际输入文件包含18个字段

$cat st_Input.txt

Total No. of Records Displayed: 4
---------------------------------------------------------------------------------------
|   Circle Desc.|Serial From        |Serial To          |        Quantity|Plant Desc.  |
---------------------------------------------------------------------------------------
|   CCCC        |20282783701        |20282788700        |      5,000.000 |2220         |
|   CCCC        |5991421000742062451|5991421000742062477|         27.000 |2310         |
|   CCCC        |41700000906        |41700011005        |     10,100.000 |2210         |
|   CCCC        |5988888000742062478|5988888000742062564|         10.000 |2210         |
----------------------------------------------------------------------------------------
|  *            |                   |                   |      15,724.000|             |
----------------------------------------------------------------------------------------
5991421000742062451,5991421000742062477,   CCCC        ,         27.000 ,2310         ,,Format_st_Input.txt
20282783701        ,20282788700        ,   CCCC        ,      5000.000 ,2220         ,,Format_st_Input.txt
41700000906        ,41700011005        ,   CCCC        ,     10100.000 ,2210         ,,Format_st_Input.txt
5988888000742062478,5988888000742062564,   CCCC        ,         10.000 ,2210         ,,Format_st_Input.txt
Total No. of Records Displayed: 4,Format_st_Input.txt
---------------------------------------------------------------------------------------,Format_st_Input.txt
,   Circle Desc.,Serial From        ,Serial To          ,        Quantity,Plant Desc.  , ,Format_st_Input.txt
---------------------------------------------------------------------------------------,Format_st_Input.txt
----------------------------------------------------------------------------------------,Format_st_Input.txt
,  *            ,                   ,                   ,      15724.000,             , ,Format_st_Input.txt
----------------------------------------------------------------------------------------,Format_st_Input.txt
步骤1:将字段分隔符
从“|”格式化为“,”
为避免字段位置更改,示例数量
5000.000将更改为5000.000,而不是5和000.000

命令#1:

输出#1:

第二步:试过下面的命令

如果字段
$3~“5991421000”
打印到“Op22_st_Input.txt”和
$3~“[0-9]”
打印到“Op33_st_Input.txt”并将所有
其他垃圾字符打印到
Op44_st_Input.txt中

命令#2:

有没有最简单的方法来更改字段位置,从
Delete$1、$2==3、$3==4,然后打印所有剩余的字段项
不要键入
print$2、$3、$4,。。。直到18美元
,并避免执行许多步骤

#Op22_st_Input.txt的所需输出

Total No. of Records Displayed: 4
---------------------------------------------------------------------------------------
|   Circle Desc.|Serial From        |Serial To          |        Quantity|Plant Desc.  |
---------------------------------------------------------------------------------------
|   CCCC        |20282783701        |20282788700        |      5,000.000 |2220         |
|   CCCC        |5991421000742062451|5991421000742062477|         27.000 |2310         |
|   CCCC        |41700000906        |41700011005        |     10,100.000 |2210         |
|   CCCC        |5988888000742062478|5988888000742062564|         10.000 |2210         |
----------------------------------------------------------------------------------------
|  *            |                   |                   |      15,724.000|             |
----------------------------------------------------------------------------------------
5991421000742062451,5991421000742062477,   CCCC        ,         27.000 ,2310         ,,Format_st_Input.txt
20282783701        ,20282788700        ,   CCCC        ,      5000.000 ,2220         ,,Format_st_Input.txt
41700000906        ,41700011005        ,   CCCC        ,     10100.000 ,2210         ,,Format_st_Input.txt
5988888000742062478,5988888000742062564,   CCCC        ,         10.000 ,2210         ,,Format_st_Input.txt
Total No. of Records Displayed: 4,Format_st_Input.txt
---------------------------------------------------------------------------------------,Format_st_Input.txt
,   Circle Desc.,Serial From        ,Serial To          ,        Quantity,Plant Desc.  , ,Format_st_Input.txt
---------------------------------------------------------------------------------------,Format_st_Input.txt
----------------------------------------------------------------------------------------,Format_st_Input.txt
,  *            ,                   ,                   ,      15724.000,             , ,Format_st_Input.txt
----------------------------------------------------------------------------------------,Format_st_Input.txt
#Op33_st_Input.txt的所需输出

Total No. of Records Displayed: 4
---------------------------------------------------------------------------------------
|   Circle Desc.|Serial From        |Serial To          |        Quantity|Plant Desc.  |
---------------------------------------------------------------------------------------
|   CCCC        |20282783701        |20282788700        |      5,000.000 |2220         |
|   CCCC        |5991421000742062451|5991421000742062477|         27.000 |2310         |
|   CCCC        |41700000906        |41700011005        |     10,100.000 |2210         |
|   CCCC        |5988888000742062478|5988888000742062564|         10.000 |2210         |
----------------------------------------------------------------------------------------
|  *            |                   |                   |      15,724.000|             |
----------------------------------------------------------------------------------------
5991421000742062451,5991421000742062477,   CCCC        ,         27.000 ,2310         ,,Format_st_Input.txt
20282783701        ,20282788700        ,   CCCC        ,      5000.000 ,2220         ,,Format_st_Input.txt
41700000906        ,41700011005        ,   CCCC        ,     10100.000 ,2210         ,,Format_st_Input.txt
5988888000742062478,5988888000742062564,   CCCC        ,         10.000 ,2210         ,,Format_st_Input.txt
Total No. of Records Displayed: 4,Format_st_Input.txt
---------------------------------------------------------------------------------------,Format_st_Input.txt
,   Circle Desc.,Serial From        ,Serial To          ,        Quantity,Plant Desc.  , ,Format_st_Input.txt
---------------------------------------------------------------------------------------,Format_st_Input.txt
----------------------------------------------------------------------------------------,Format_st_Input.txt
,  *            ,                   ,                   ,      15724.000,             , ,Format_st_Input.txt
----------------------------------------------------------------------------------------,Format_st_Input.txt
#Op44_st_Input.txt的所需输出

Total No. of Records Displayed: 4
---------------------------------------------------------------------------------------
|   Circle Desc.|Serial From        |Serial To          |        Quantity|Plant Desc.  |
---------------------------------------------------------------------------------------
|   CCCC        |20282783701        |20282788700        |      5,000.000 |2220         |
|   CCCC        |5991421000742062451|5991421000742062477|         27.000 |2310         |
|   CCCC        |41700000906        |41700011005        |     10,100.000 |2210         |
|   CCCC        |5988888000742062478|5988888000742062564|         10.000 |2210         |
----------------------------------------------------------------------------------------
|  *            |                   |                   |      15,724.000|             |
----------------------------------------------------------------------------------------
5991421000742062451,5991421000742062477,   CCCC        ,         27.000 ,2310         ,,Format_st_Input.txt
20282783701        ,20282788700        ,   CCCC        ,      5000.000 ,2220         ,,Format_st_Input.txt
41700000906        ,41700011005        ,   CCCC        ,     10100.000 ,2210         ,,Format_st_Input.txt
5988888000742062478,5988888000742062564,   CCCC        ,         10.000 ,2210         ,,Format_st_Input.txt
Total No. of Records Displayed: 4,Format_st_Input.txt
---------------------------------------------------------------------------------------,Format_st_Input.txt
,   Circle Desc.,Serial From        ,Serial To          ,        Quantity,Plant Desc.  , ,Format_st_Input.txt
---------------------------------------------------------------------------------------,Format_st_Input.txt
----------------------------------------------------------------------------------------,Format_st_Input.txt
,  *            ,                   ,                   ,      15724.000,             , ,Format_st_Input.txt
----------------------------------------------------------------------------------------,Format_st_Input.txt

不要。使用
cut

cut -d , -f 11,12,2-10,13-17
您可以使用此awk:

awk 'BEGIN{FS=OFS=","}
    {of=""}
    $3~/[0-9]/{of="Op33_st_Input.txt"} 
    $3~/5991421000/{of="Op22_st_Input.txt"}
    of{s=$2;$2=$3;$3=$4;$4=s;$1=""; print substr($0,2),FILENAME > of; next}
    {print $0, FILENAME > "Op44_st_Input.txt"}' Format_st_Input.txt
说明:
  • BEGIN
    部分将字段分隔符和输出字段分隔符设置为逗号
  • 为每行将变量的
    初始化为空字符串
  • 如果
    $3
    与regex
    [0-9]
    匹配,则设置为
    Op33\u st\u Input.txt
  • 如果
    $3
    与regex
    5991421000
    匹配,则设置为
    Op22\u st_Input.txt
  • 如果设置了
,则使用格式化输出并重定向到
的变量

  • 否则,将行、文件名打印到
    Op44\u st\u Input.txt

  • a)我可以帮你做这件事。b) 我不会帮你做这件事的。我怀疑许多其他人也会有同样的感受,因为当我们看到大量的输入和一堆复杂的信息,说明你认为应该如何处理这个问题时,这足以让人沮丧,以至于让人们转向下一个问题。展示一些小的、有代表性的输入和预期的输出,这些输出给出了一个答案,您可以在此基础上解决整个问题。在我前面提到的脚本中,你仍然有同样的新手错误。你试图将这个问题分解为一系列单独的命令。当您发现自己正在这样做并在相同的信息上操作时,可能是时候将所有命令组合成一个
    awk脚本了。这允许在处理数据时有相当大的灵活性,因为临时存储可以防止重新读取和重新解析信息。或者,您可以通过将信息读入bash脚本中的数组变量来完成重新格式化。重点是,读取一次,根据需要重新格式化。Ed Morton,对不起,我已将i/O示例减少到最小字段,基本上我是财务背景人员,而不是编程背景,有时很难理解代码以及如何更改命令,这就是为什么粘贴所有18个字段,这将在将来避免…Anubhava,非常感谢,它正在按预期工作,请您添加一些评论以便更好地理解!!!很高兴它成功了。我现在正在使用手机,几个小时后回到电脑时会加上解释。Anubhava,好的,我的实际输入文件包含$18个字段,从-11到-12,因此打印值为$11,$12,$2,$3,$4,$5,$6,$7,$8,$9,$10,$13,$14,$15,$16,$17,$18的文件名:仅供好奇,我添加了一个解释,并更新了
    awk
    命令以智能地打印整行,这样你就可以避免打印每个单独的字段。Anubhava,检查了脚本,它可以处理所有18个字段,似乎substr($0,2)被所有剩余的要打印的字段处理了,这有什么神奇之处,如果您有时间,请在视线内提供一些;)