marge line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vaskarbasak
    New Member
    • Feb 2008
    • 27

    marge line

    test.text:-
    --------
    601215856422|CO M|0|0|CD|1|3.99
    601215868029|CO M|0|0|CD|1|3.99
    801472092322|CO M|0|0|CD|2|10.9 8
    802097013822|CO M|0|0|CD|2|10.9 8
    601215856422|MO M|0|0|CD|1|3.99
    601215856422|RN O|0|0|CD|1|3.99
    601215856422|SO M|0|0|CD|1|3.99

    ..........

    I have to do
    601215856422|CO M|0|CD|1|3.99|M OM|0|CD|1|3.99| RNO|0|CD|1|3.99
    ....

    using shell script

    pls help me.

    thanks!
    vaskar
  • kaarthikeyapreyan
    New Member
    • Apr 2007
    • 106

    #2
    Originally posted by vaskarbasak
    test.text:-
    --------
    601215856422|CO M|0|0|CD|1|3.99
    601215868029|CO M|0|0|CD|1|3.99
    801472092322|CO M|0|0|CD|2|10.9 8
    802097013822|CO M|0|0|CD|2|10.9 8
    601215856422|MO M|0|0|CD|1|3.99
    601215856422|RN O|0|0|CD|1|3.99
    601215856422|SO M|0|0|CD|1|3.99

    ..........

    I have to do
    601215856422|CO M|0|CD|1|3.99|M OM|0|CD|1|3.99| RNO|0|CD|1|3.99
    ....
    The code is bit complicating but i have provided you flexibility to do a lot of things,I also believe there are a lot more simpler ways to do it

    Code:
    a=601215856422
    filelengthmain=`wc -l input.txt |cut -d ' ' -f1`
    val1=1
    val1=`expr $val1 + 0`
    for ((j=1; j<=$filelengthmain; j++))
    do
    ln=`head -$val1 input.txt|tail -1`
    val=`echo $ln | cut -d '|' -f 1`
    if test $a = $val
    then
    fvals[$val]=`echo $ln | grep -e $a |grep -v SOM | cut -d '|' -f 2-3,5-7`
    b=`echo $b"|"${fvals[$val]}`
    fi
    val1=`expr $val1 + 1`
    done
    echo $a$b
    Output :

    601215856422|CO M|0|CD|1|3.99|M OM|0|CD|1|3.99| RNO|0|CD|1|3.99 |

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      You can use this script

      Code:
      FULL_LINE=""
      
      while read LINE
      do
        if [ "$FULL_LINE" == "" ]
        then
            FULL_LINE="$LINE"
        else
          FULL_LINE=$FULL_LINE"|"$LINE
        fi
      done < "ip.txt" 
      
      echo "FULL_LINE is : ${FULL_LINE}"
      Raghuram

      Comment

      • radoulov
        New Member
        • Jun 2007
        • 34

        #4
        If I understand correctly:

        Code:
        awk '{ 
        for (f=2; f<=NF; f++)
          if (f != 3)
            x[$1] = x[$1] FS $f
        } END {
        for (k in x)
          print k x[k]
        }' OFS='|' FS='|' file

        Comment

        • oberon
          New Member
          • Mar 2008
          • 14

          #5
          Here is a one-liner for the same job:
          Code:
          cat test.text | echo -n $(awk {'print $0 "|"'}) | sed -e 's/\ *//g'
          The -n is to remove newline, and the last sed is to remove a whitespace that would otherwise sneak in.

          Comment

          • vaskarbasak
            New Member
            • Feb 2008
            • 27

            #6
            Thanks all!

            601215856422|CO M|0|0|CD|1|3.99
            601215868029|CO M|0|0|CD|1|3.99
            801472092322|CO M|0|0|CD|2|10.9 8
            802097013822|CO M|0|0|CD|2|10.9 8
            601215856422|MO M|0|0|CD|1|3.99
            601215856422|RN O|0|0|CD|1|3.99
            601215856422|SO M|0|0|CD|1|3.99

            but i have to do

            601215856422|MO M|1|3.99|RNO|1| 3.99|SOM|1|3.99 |COM|1|3.99
            801472092322|CO M|0|0|CD|2|10.9 8
            601215868029|CO M|0|0|CD|1|3.99
            .....

            the file size is very large nearly 91 MB.So it takes lot of time.But i have to do it with in 5 min.

            Still i am trying to solve the problem.

            please help me...

            Comment

            • oberon
              New Member
              • Mar 2008
              • 14

              #7
              Originally posted by vaskarbasak
              601215856422|MO M|1|3.99|RNO|1| 3.99|SOM|1|3.99 |COM|1|3.99
              801472092322|CO M|0|0|CD|2|10.9 8
              601215868029|CO M|0|0|CD|1|3.99
              Your last post didn't really clearify what you are looking for. If you could please add a few lines of explanation, then you would have a greater chance of getting the right answer.

              Comment

              • vaskarbasak
                New Member
                • Feb 2008
                • 27

                #8
                i need concate all duplicate lines like

                794051400123|CO M|24|MOM|62|SOM |25|RNO|73
                794051413727|CO M|11||MOM|4|RNO |8
                ............

                and save it another file..

                Comment

                • gpraghuram
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1275

                  #9
                  Originally posted by vaskarbasak
                  i need concate all duplicate lines like

                  794051400123|CO M|24|MOM|62|SOM |25|RNO|73
                  794051413727|CO M|11||MOM|4|RNO |8
                  ............

                  and save it another file..

                  The first part of the string that is the 794051400123 is the token with which you find whether it is a duplicate token?

                  Raghuram

                  Comment

                  Working...