基于bash条件的格式化

时间:2018-01-16 17:31:40

标签: bash unix formatting

这是我的代码:

files: [
    { pattern: path.resolve(__dirname, '.spec.ts'), watched: true }
]

我非常接近。上面的代码有效,但是从电话号码中删除了第一个数字。

我正在寻找一个bash解决方案来执行以下操作:

如果行不以数字开头,则组合两行。 如果该行以数字开头,则将前两行+该行与一行中3个字段的数字组合。

这是一个例子?

    jim.bob3@email.com
    Jim Bob
    jane.bob@email.com
    Jane Bob
    joebob1122@email.com
    Joe Bob
    555 555 5555
    jbob44@email.com
    Jeff Bob
    ....

    Results:
    jim.bob3@email.com Jim Bob
    jane.bob@email.com Jane Bob
    joebob1122@email.com Joe Bob 555 555 5555
    jbob44@email.com Jeff Bob

谢谢!

2 个答案:

答案 0 :(得分:1)

如果您的Input_file与显示的示例相同,那么关注awk解决方案可能对您有帮助。

awk '{printf("%s",$0~/^name/&&FNR>1?RS $0:FNR==1?$0:FS $0)} END{print ""}'  Input_file

输出如下。

name1@email.com Jim Bob
name2@email.com Jane Bob
name3@email.com Joe Bob 555 555 5555
name4@email.com Jeff Bob

说明: 以下代码仅用于理解目的而非运行,您可以使用上述代码进行运行。

awk '{printf(\         ##Using printf keyword from awk here to print the values etc.
"%s",\                 ##Mentioning %s means it tells printf that we are going to print a string here.
$0~/^name/&&FNR>1\     ##Checking here condition if a line starts from string name and line number is greater than 1 then:
?\                     ##? means following statement will be printed as condition is TRUE.
RS $0\                 ##printing RS(record separator) and current line here.
:\                     ##: means in case mentioned above condition was NOT TRUE then perform following steps:
FNR==1\                ##Checking again condition here if a line number is 1 then do following:
?\                     ##? means execute statements in case above condition is TRUE following ?
$0\                    ##printing simply current line here.
:\                     ##: means in case above mentioned conditions NOT TRUE then perform actions following :
FS $0)}                ##Printing FS(field separator) and current line here.
END{print ""}' file24  ##Printing a NULL value here to print a new line and mentioning the Input_file name here too.

答案 1 :(得分:0)

使用awk

awk '/@/{if(s)print s;s=""}{s=(s?s OFS:"")$0}END{if(s)print s}' infile

<强> 输入:

$ cat infile
jim.bob3@emaawk '/@/{if(s)print s;s=""}{s=(s?s OFS:"")$0}END{if(s)print s}' infileil.com
Jim Bob
jane.bob@email.com
Jane Bob
joebob1122@email.com
Joe Bob
555 555 5555
jbob44@email.com
Jeff Bob

<强> 输出:

$ awk '/@/{if(s)print s;s=""}{s=(s?s OFS:"")$0}END{if(s)print s}' infile
jim.bob3@email.com Jim Bob
jane.bob@email.com Jane Bob
joebob1122@email.com Joe Bob 555 555 5555
jbob44@email.com Jeff Bob

<强> 说明:

 awk '/@/{                    # ir row/line/record contains @
           if(s)print s;      # if variable s was set before print it.
           s=""               # nullify or reset variable s
      }
      {
           s=(s?s OFS:"")$0   # concatenate variable s with its previous content if it was set before, with   
                              # OFS o/p field separator and 
                              # current row/line/record ($0), 
                              # otherwise s will be just current record

      }
      END{                    # end block
           if(s)print s       # if s was set before print it
      }
     ' infile