Search Pattern and Print lines in Single Column

Hi Experts

I have small query where I request the into a single file
Suppose:

File1: {Unique entries}

AA
BB
CC
DD

FileB:

AA, 123
AA, 234
AA, 2345
CC, 123
CC, 5678
DD,123
BB, 7890

Output Required:

AA, 123,234,2345
CC,123,5678
DD,123
BB,7890

Regards
Navkanwal

This seems to be similar to a request you submitted a year and a half ago.

Can you modify the solution you were given for that problem to solve this problem too? (You never did say what script solved that issue.) What have you tried?

Hi Don

Thanks for the information.
The correction worked fine, last time around and I replied as well

Originally Posted by navkanwal View Post
The command is working fine and output was generated in less than 5 minutes.

However, the request is different this time as, I have multiple lines starting with the same alphabets, so it has become tricky as the previous command is not providing the desired output.

Regards
Navkanwal

Hello Navkanwal,

Kindly use code tags for commands/codes you are using in your posts as per forum rules. Following solution may help you in same.

awk 'FNR==NR{X[$1]=X[$1]?X[$1] OFS $2:$2;next} ($1 in X){Z=$1 OFS X[$1];gsub(/, /,",",Z);print Z}' FS=, OFS=, file2 file1

Output will be as follows.

AA,123,234,2345
BB,7890
CC,123,5678
DD,123

Hope this helps.

Thanks,
R. Singh

1 Like

You didn't quite answer Don Cragun's questions about your attempts to modify/adapt the solution given in that former thread to solve your actual problem.
And, you don't seem to care about how the spaces in your input file(s) are being handled in the output file.
However, try

awk 'NR==FNR {T[$1]; next} !($1 in T) {next} $1 != A {printf "%s%s", NL, $1; A=$1; NL="\n"} {printf ",%s", $2} END {printf NL}' FS="," file1 file2
AA, 123, 234, 2345
CC, 123, 5678
DD,123
BB, 7890
1 Like

Thanks Experts !!

The command is working fine and as desired.

Regards
Navkanwal