0

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
    StackOverflow is about helping people fix their existing programming code. Edit your Q to show your best attempt at coding a solution to your problem. Sorry, but requests for tutorials, research, tools, recommendations, libraries, and code are off-topic. ***Please*** read https://stackoverflow.com/help/on-topic , https://stackoverflow.com/help/how-to-ask , https://stackoverflow.com/help/dont-ask , https://stackoverflow.com/help/mcve and take the [tour](https://stackoverflow.com/tour) before posting more Qs here. Good luck – shellter Jan 16 '18 at 19:00

2 Answers2

1

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.
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Thanks, but this is just an example. The email addresses will vary based on previous results, so this does not work. – Dustin Mooney Jan 16 '18 at 17:45
  • 3
    Sorry to say but if you will NOT let us know the complete details how come possible we could provide a near to your need answer? Above answer is based on your post made. Please edit your question with proper details and let me know on same then. – RavinderSingh13 Jan 16 '18 at 17:48
0

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
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
  • THIS WORKS! Thank you – Dustin Mooney Jan 18 '18 at 16:37
  • @DustinMooney Glad to know it worked out, [you may mark the answer as accepted by clicking on tick mark on top-left of this answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) – Akshay Hegde Jan 18 '18 at 16:41
  • Can you add an explanation for me to understand better? Trying to get better at this stuff. – Dustin Mooney Jan 18 '18 at 17:16
  • IF line contains `@` and if variable `s` was not null, print variable `s`, `s="'` means nullify variable s. `s=(s?s OFS:"")$0` means, variable `s` will be concatenated with its own content, if it was set before with current row/line/record read. and in end block if `s` was set print variable `s` – Akshay Hegde Jan 18 '18 at 17:33
  • @DustinMooney added explanation – Akshay Hegde Jan 18 '18 at 17:39