Announcement

Collapse
No announcement yet.

Awk / sed

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Awk / sed

    Anyone have any experince with sed or AWK?

    im running a BASH script that collect data from MYSQL and echo's the contents to a tab delimited file, before the data is echo to the file i pipe the output through sed to add formatting.

    im having some trouble with trying to add a new line after a certain pattern e.g

    Refill Liquid "2010/08/21 12:59" 00:06:00
    Refill Liquid "2010/08/21 11:57" 00:06:00
    Refill Liquid Im "2010/08/21 11:57" 00:06:00
    Preassure fault "2010/08/21 11:57" 00:06:00
    Refill Liquid "2010/08/21 14:57" 00:06:00
    Refill Liquid Im "2010/08/21 14:57" 00:06:00
    Pressure Fault "2010/08/21 14:57" 00:06:00


    Fields are tab delimited, im trying to add a line space where the timestamp field is different e.g

    Refill Liquid 2010/08/21 12:59 00:06:00

    Refill Liquid 2010/08/21 11:57 00:06:00
    Refill Liquid Im 2010/08/21 11:57 00:06:00
    Preassure fault 2010/08/21 11:57 00:06:00

    Refill Liquid 2010/08/21 14:57 00:06:00
    Refill Liquid Im 2010/08/21 14:57 00:06:00
    Pressure Fault 2010/08/21 14:57 00:06:00


    Thanks in advance.
    Last edited by ikon; 7th October 2010, 14:33.
    MCSE 2003; MCTS Vista; Sec+; CCNA
    Attitude Makes The Difference!
    in other words you got to WANT to do it..


  • #2
    Re: Awk / sed

    If your input is in input.txt, this will print out what you need

    Code:
    TIME1=""
    TIME2=""
    while read LINE
        do 
            TIME1=$(echo "$LINE" | awk -F'"' '{ print $2 }'|awk -F' ' '{ print $2 }')
            if [ "$TIME2" == "" ]; then TIME2=$TIME1; fi
            if [ "$TIME1" != "$TIME2" ]
            then
                TIME2=$TIME1
                echo
            fi
            echo "$LINE"
        done < input.txt
    Real stupidity always beats Artificial Intelligence (c) Terry Pratchett

    BA (BM), RHCE, MCSE, DCSE, Linux+, Network+

    Comment


    • #3
      Re: Awk / sed

      Thanks for your input.

      it doesn't quite work i believe that to be my own fault as i stated that the field delimiter is TAB however the time stamp field (field 2) does not contain "" i put them in becuase there is a space inbetween DATE^TIME.

      so each line in a file called gas.txt actually looks like

      Refill Liquid 2010/08/21 12:59 00:06:00

      Where spaces are actually tabs except for '2010/08/21 12:59' the space here is actuall a space and not a new field.

      is your existing code editable to contain with this?

      thanks
      MCSE 2003; MCTS Vista; Sec+; CCNA
      Attitude Makes The Difference!
      in other words you got to WANT to do it..

      Comment


      • #4
        Re: Awk / sed

        can you paste the actual text you are trying to parse?
        Real stupidity always beats Artificial Intelligence (c) Terry Pratchett

        BA (BM), RHCE, MCSE, DCSE, Linux+, Network+

        Comment


        • #5
          Re: Awk / sed

          Code:
          
          Oxygen Refill Liquid    19/09/2010 08:02    00:00:16
          Oxygen Refill Liquid Immediately    17/09/2010 01:09    00:02:52
          Oxygen Refill Liquid Immediately    19/09/2010 08:02    00:00:16
          Oxygen Reserve Low    19/09/2010 08:02    00:00:16
          Oxygen Pressure Fault    19/09/2010 08:02    00:00:16
          
          Bare in mind, the first fields text changes all the time, depending on what gas is being reported on.
          Last edited by ikon; 11th October 2010, 10:34.
          MCSE 2003; MCTS Vista; Sec+; CCNA
          Attitude Makes The Difference!
          in other words you got to WANT to do it..

          Comment


          • #6
            Re: Awk / sed

            Code:
            TIME1=""
            TIME2=""
            while read LINE
                do 
                    TIME1=$(echo "$LINE" | cut -f2 |cut -f2 -d' ')
                    if [ "$TIME2" == "" ]; then TIME2=$TIME1; fi
                    if [ "$TIME1" != "$TIME2" ]
                    then
                        TIME2=$TIME1
                        echo
                    fi
                    echo "$LINE"
                done < input.txt
            if the file is really tab delimited, `cut` is the easiest way to go
            Real stupidity always beats Artificial Intelligence (c) Terry Pratchett

            BA (BM), RHCE, MCSE, DCSE, Linux+, Network+

            Comment


            • #7
              Re: Awk / sed

              I found a solution with AWK

              input | awk -F'\t' 'NR>1 && $2!=s{printf "\n"}{s=$2;print}'

              Thanks for your help.
              MCSE 2003; MCTS Vista; Sec+; CCNA
              Attitude Makes The Difference!
              in other words you got to WANT to do it..

              Comment

              Working...
              X