Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2012
    Posts
    5

    Unanswered: help on bash script to join similar lines together

    Hi,

    I have been thinking of how to script this but i have no clue at all..
    Could someone please help me out or give me some idea on this?

    I would like to group those lines with the same first variable in each line, joining the 2nd variables with commas.
    Let's say i have the following input.
    Code:
    aa c1
    aa c2
    aa c3
    cc d1
    dd e1
    dd e2
    ee f1
    I would like the output to be like this.
    Code:
    aa c1,c2,c3
    cc d1
    dd e1,e2
    ee f1
    Could this be easily done with bash script?
    Or should i try perl script then?
    I'm a beginner in bash script and perl.

    Thank you.

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Code:
    first="Y"                
    while read a b           
    do                       
    if [ "$first"  =  "Y"  ] 
    then                     
       first="N"             
       prev=$a               
       echo "$a $b\c"        
    else                     
    if [ "$a" != "$prev" ]   
    then                     
       echo "\n$a $b\c"      
       prev=$a               
    else                     
       echo ",$b\c"          
    fi                       
    fi                       
    done                     
    echo
    This assumes that the input file is sorted.

  3. #3
    Join Date
    Jun 2012
    Posts
    5
    Quote Originally Posted by kitaman View Post
    Code:
    first="Y"                
    while read a b           
    do                       
    if [ "$first"  =  "Y"  ] 
    then                     
       first="N"             
       prev=$a               
       echo "$a $b\c"        
    else                     
    if [ "$a" != "$prev" ]   
    then                     
       echo "\n$a $b\c"      
       prev=$a               
    else                     
       echo ",$b\c"          
    fi                       
    fi                       
    done                     
    echo
    This assumes that the input file is sorted.
    Sorry.. don't understand..
    what is a, b , F ..?

    Anyway i found a short solution.
    ${input} is the filename for the input file.
    Code:
    for m in `cat ${input} | awk '{print $1}' | sort | uniq `
    do
            var=`grep "^${m} " ${output} | awk '{print $2}' | tr '\n' ',' | sed '$s/,$//'`
            echo "${m} ${var}"
    done

  4. #4
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Code:
    sort ${input} | awk '{if (k==$1) { d=d "," $2} else {if (k!="") {print k " " d };k=$1;d=$2} } END {print k " " d }'

  5. #5
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1

    Anyway i found a short solution.
    ${input} is the filename for the input file.
    And can you explain how this works.
    Do you know how many times you read the input file?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •