0

I have one file which contain name of mysql tables and its column names. I want to convert into following format

file: example.txt

Table Name        | Column Name
   ABC            |    123
   ABC            |    345
   XYZ            |    111
   FFF            |    222
   FFF            |    333
   FFF            |   4444

Output should be

ABC(123,345)
XYZ(111)
FFF(222,333,4444)

Please suggest me some example.

2
  • 2
    What did you try so far? Note it is not the complicated to find related answers in this site, did you check any? Commented Apr 15, 2014 at 10:41
  • I didn't find any useful can you please suggest me Commented Apr 15, 2014 at 10:46

3 Answers 3

3
$ cat t.awk
#!/usr/bin/awk -f

BEGIN { FS="[ \t|]+" }
NR>1 { a[$1]=a[$1]$2"," }
END {
    for (i in a)
        printf "%s(%s)\n", i, substr(a[i], 0, length(a[i])-1)
}

$ ./t.awk example.txt
ABC(123,345)
XYZ(111)
FFF(222,333,4444)
Sign up to request clarification or add additional context in comments.

Comments

2

Some like this:

awk -F"[ \t|]+" 'NR>2 {printf f==$1?"":")\n"} NR>1{printf (f==$1?","$2:$1"("$2)} {f=$1} END {print ")"}' file
ABC(123,345)
XYZ(111)
FFF(222,333,4444)

1 Comment

This is a good solution, but it assumes that all occurrences of the key in the first column appear contiguously. It is not clear that this is a valid assumption.
0

Pure bash (4.0+):

declare -A name                     # associative array

while read  a b c; do
   name[$a]="${name[$a]},$c" 
done < <( tail -n+2 "$infile" )     # skip first line

for nm in ${!name[@]}; do
  echo "$nm(${name[$nm]#,})"        # remove leading comma
done

The parameter substitution removes the leading comma

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.