问题
Here is my code:
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.
I'm looking for a bash solution to do the following:
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.
Here's an example?
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
Thanks!
回答1:
Using 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
回答2:
If your Input_file is same as shown sample then following awk solution may help you in same.
awk '{printf("%s",$0~/^name/&&FNR>1?RS $0:FNR==1?$0:FS $0)} END{print ""}' Input_file
Output will be as follows.
name1@email.com Jim Bob
name2@email.com Jane Bob
name3@email.com Joe Bob 555 555 5555
name4@email.com Jeff Bob
Explanation: Following code is only for understanding purposes NOT for running you could use above code for running.
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.
来源:https://stackoverflow.com/questions/48286821/formatting-based-on-condition-in-bash