Awk / sed

Home Forums Other Open Source Awk / sed

This topic contains 6 replies, has 2 voices, and was last updated by Avatar ikon 9 years, 3 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • Avatar
    ikon
    Member
    #151383

    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.

    Avatar
    DYasny
    Member
    #289390

    Re: Awk / sed

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

    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[/CODE][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[/CODE]

    Avatar
    ikon
    Member
    #354278

    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

    Avatar
    DYasny
    Member
    #289391

    Re: Awk / sed

    can you paste the actual text you are trying to parse?

    Avatar
    ikon
    Member
    #354279

    Re: Awk / sed

    [B]
    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
    [/B][/CODE]

    Bare in mind, the first fields text changes all the time, depending on what gas is being reported on.[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
    [/CODE]

    Bare in mind, the first fields text changes all the time, depending on what gas is being reported on.

    Avatar
    DYasny
    Member
    #289393

    Re: Awk / sed

    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[/CODE]
    if the file is really tab delimited, cut is the easiest way to go[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[/CODE]
    if the file is really tab delimited, cut is the easiest way to go

    Avatar
    ikon
    Member
    #354280

    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.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.