-1

Basically I am trying to print a table with 4 fields per row and the 5th field should go to the second row all the way to 8th field.

awk 'BEGIN{
        FS=" "
        print "<HTML><BR><h2><CENTER><BODY> DETAILS </BODY></CENTER> </h2> <BR><CENTER><table border=1> <CENTER>"
        print "<TR><TH>No</TH><TH>Name</TH><TH>task</TH><TH>location</TH></TR>"
     }
     {
        printf "<TR>"
        for(i=5;i<=NF;i++)

        printf "<TD><CENTER>%s</CENTER></TD>", $i
        print "</TR>"

     }
     END{
        print "</TABLE></HTML>"
     }' /home/xyz/found.txt

I don't want the below which I am getting now. Its printing on the same row itself.

 No     Name   task  Location

 1001         Mike     Texting    US    1002  Mark  Chatting  UK

It should be

 No     Name   task  Location

 1001         Mike     Texting    US    
 1002         Mark     Chatting   UK

Could you please advise.

  • Where are you printing fields 1 through 4? I don't see it in the awk script – Jeff Schaller May 17 '17 at 17:45
0

You can print your file in rows every 4th field using something like this:

$ cat file7
abc,def,ghi,nop,klm,one,two,three,four,five,six,nine

$ awk -v RS="," '{printf("%s%s",$0,(NR%4==0?"\n":RS))}' file7
abc,def,ghi,nop
klm,one,two,three
four,five,six,nine

Using a custom RS (Record Separator) , you can make use of NR (line number) and handle each field of original file as a separate record/separate line.

If you need a header you can print the header in BEGIN section of awk.

PS: If your fields are separated by space, adjust RS (Record Separator) accordingly:

$ awk -v RS=" " '{printf("%s%s",$0,(NR%4==0?"\n":RS))}' file7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.