3

I am trying to print collected data from a file where each set gets a new line (\n)

Code:

awk '/mail:|fullName:|uid:/{s=s", "$0} END{print substr(s,3)}' dump2.txt

Output now:

mail: [email protected] , fullName: Bogus Bogus, uid: 666  mail: [email protected] , fullName: Bogus2 Bogus2, uid: 667

Desired output:

mail: [email protected] , fullName: Bogus Bogus, uid: 666 \n
mail: [email protected] , fullName: Bogus2 Bogus2, uid: 667 \n

4 Answers 4

1

try

 awk '/mail:|fullName:/{s=s", "$0} /uid:/ {s=s", "$0 "\n" ;} END{printf substr(s,3)}' dump2.txt

assuming no % in field

other solution

 awk '/mail:|fullName:/{s=s", "$0} /uid:/ {print substr(s,3) ", "$0 ;s=""} ' dump2.txt

assuming uid: is last.

2
  • That did give me a new line. But it throws the alignment off. Do you know how to correct it? paste.ee/p/TQQiq NOTE: there are more than 3 fields. There are about 4 more I need to add. Commented Oct 2, 2014 at 11:10
  • I just ensured the last item was in the correct place. All good now! Commented Oct 2, 2014 at 11:17
1

you just need to add \n in desire place :

awk '/mail:|fullName:|uid:/{s=s", "$0}\n END{print substr(s,3)}' dump2.txt
1
  • awk '/mail:|fullName:|uid:/{s=s", "$0} END{print substr(s,3)}END{print '\n'}' dump2.txt fails. awk '/mail:|fullName:|uid:/{s=s", \n"$0} END{print substr(s,3)}' dump2.txt fails. awk '/mail:|fullName:|uid:/{s=s", "$0} END{print substr(s,3)'\n'}' dump2.txt fails. Where to I put it where it will work? I tried \n\n and \n\n\r as well. Commented Oct 2, 2014 at 11:03
1

Another technique: don't store it all up in one big string, print it out as you go along:

awk '$1=="mail:" || $1=="fullName:" {printf "%s, ", $0} $1=="uid:"' dump2.txt
0

Note: all of the below (obviously) assume that the uid: line will always be the last one of each group.  However, they all work with % characters in the data.

If you want to save up all the data and print it at the end, here’s a somewhat simple approach, using arrays:

awk 'BEGIN {count=0}
     /mail:|fullName:|uid:/ {s[count] = s[count] ", " $0} /uid:/ {count++}
     END {for (i=0; i<count; i++) print substr(s[i],3)}' dump2.txt

If you want to be able to handle the fields individually (at the end), use a two-dimensional array:

awk 'BEGIN {count=0}
     /mail:|fullName:|uid:/ {s[count][$1] = $0} /uid:/ {count++}
     END {for (i=0; i<count; i++) printf "%s, %s, %s\n",
                s[i]["mail:"], s[i]["fullName:"], s[i]["uid:"]}' dump2.txt

(Like glenn’s answer, this assumes that the mail:, fullName:, and uid: strings appear as the first field in the line.)

I don’t know what you meant when you mentioned alignment, but, from here, it’s easy to line up the fields in your output:

awk 'BEGIN {count=0}
     /mail:|fullName:|uid:/ {s[count][$1] = $0} /uid:/ {count++}
     END {for (i=0; i<count; i++) printf "%23s, %23s, %s\n",
                s[i]["mail:"], s[i]["fullName:"], s[i]["uid:"]}' dump2.txt

or

awk 'BEGIN {count=0}
     /mail:|fullName:|uid:/ {s[count][$1] = $0} /uid:/ {count++}
     END {for (i=0; i<count; i++) printf "%-23s, %-23s, %s\n",
                s[i]["mail:"], s[i]["fullName:"], s[i]["uid:"]}' dump2.txt

or

awk 'BEGIN {count=0}
     /mail:|fullName:|uid:/ {s[count][$1] = $0} /uid:/ {count++}
     END {for (i=0; i<count; i++) printf "%-23s %-24s %s\n",
                                    s[i]["mail:"] ",",
                                    s[i]["fullName:"] ",",
                                    s[i]["uid:"]}' dump2.txt

This answer shows how you can determine the maximum data length for each field.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.