Find the answer to your Linux question:
Results 1 to 3 of 3
I have a text file(replicateTotals.txt) with a bunch of lines in this form: Code: 2009/10/21-12:15:58 avtar Stats <6559>: 111.5 KB in 1,838 messages sent to server, 4.541 MB in 1,832 ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    11

    Extracting Substring with SED

    I have a text file(replicateTotals.txt) with a bunch of lines in this form:

    Code:
    2009/10/21-12:15:58 avtar Stats <6559>: 111.5 KB in 1,838 messages sent to server, 4.541 MB in 1,832 messages received from server.
    2009/10/21-19:44:40 avtar Stats <6559>: 22.88 GB in 2,781,764 messages sent to server, 157.2 MB in 2,779,968 messages received from server.
    2009/10/21-19:44:40 avtar Stats <6559>: 72.55 MB in 1,338,246 messages sent to server, 22.80 GB in 1,336,448 messages received from server.
    What I would like to do is extract just the data amount which appears right after the '<6559>'. So, the output I would want from the lines above would be:

    Code:
    111.5 KB
    22.88 GB
    72.55 MB
    I'm not very experienced with sed, but here's the command that I came up with to grab this data:

    Code:
    # cat replicateTotals.txt | grep \<6559\> | sed 's/.*\<6559\>\: \([[:digit:]]*\.[[:digit:]]*(KB|MB|GB)\).*/\1/'
    This command is not working. Can anyone tell me where I'm going wrong?

  2. #2
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    Code:
    grep '<6559>' replicateTotals.txt | sed -r 's/.*<6559>: ([[:digit:]]*.[[:digit:]]* (KB|MB|GB)).*/\1/'
    Several problems:

    When you have a string within single quotes you don't have to escape characters. They \'s were being searched for as part of the string.

    You were missing a space between [[:digit:]]*.[[:digit:]]* and (KB|MB|GB).

    Use of (KB|MB|GB) requires extended regular expressions so you have to use the -r option.

  3. #3
    Just Joined!
    Join Date
    Dec 2008
    Posts
    11
    You rock!!

    Thanks, man.

Posting Permissions

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