根据bash中的条件进行格式化 [英] Formatting based on condition in bash
本文介绍了根据bash中的条件进行格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
grep -E -o "\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b|first_name.{0,40}|[(]?[2-9]{1}[0-9]{2}[)-. ]?[2-9]{1}[0-9]{2}[-. ]?[0-9]{4}" file.txt | awk -v ORS= '
NR>1 && !/,/ {print "\n"}
{print}
END {if (NR) print "\n"}' | sed -e :a -e '$!N;s/\n[0-9]{3}/,/;ta' -e 'P;D' | sed '$!N;s/\n\s*[0-9]//;P;D'
我很近.上面的代码有效,但是正在从电话号码中删除第一位数字.
I'm pretty close. The above code works, but is removing the first digit from phone number.
我正在寻找一种bash解决方案来执行以下操作:
I'm looking for a bash solution to do the following:
如果两行不以数字开头,则合并两行.如果该行以数字开头,则将前两行+该行与一行中3个字段的数字合并.
Combine two lines if the lines do not start with a number. If the line starts with a number, combine the previous two lines + the line with the number for 3 fields in one line.
这是一个例子吗?
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
谢谢!
推荐答案
使用awk
awk '/@/{if(s)print s;s=""}{s=(s?s OFS:"")$0}END{if(s)print s}' infile
输入:
Input:
$ 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
输出:
Output:
$ 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
说明:
Explanation:
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
这篇关于根据bash中的条件进行格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文