Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Selective RS in AWK

Selective RS in AWK

Selective RS in AWK

Having an issue parsing some data. Below is how my data looks

Jul 2014: data disk -delim :

0:Sample_0:0:maps:online:0:Size 40GB15k:20.00GB:segment:3:location:::DA000000000000030:1:1:empty:1:no:0
1:Sample_1:0:maps:online:0:Size 40GB15k:20.00GB:segment:4:location:::DA000000000000031:1:1:empty:1:no:0
2:Sample_2:0:maps:online:0:Size 40GB15k:20.00GB:segment:5:location:::DA000000000000032:1:1:empty:1:no:0

Jul 2014: data network -delim :

0:Sample_3:0:maps:online:0:Size 60GB15k:10.00GB:segment:3:location:::DA000000000000030:1:1:empty:1:no:0
1:Sample_4:0:maps:online:0:Size 60GB15k:10.00GB:segment:4:location:::DA000000000000031:1:1:empty:1:no:0
2:Sample_5:0:maps:online:0:Size 60GB15k:10.00GB:segment:5:location:::DA000000000000032:1:1:empty:1:no:0 

The MOST important piece to note it "data disk" I am looking to go through a file (usually 6k lines long) and say: "look if you see this variable (data disk), read from beginning to end, and give me a sum of field 8" For example:

awk 'BEGIN { 2014 = ""} { if ($8 == “[0-9]GB") size = sum += $8"GB"; else … blah blah blah [0-9]MB}’ 

I thought about using RS e.g.:

awk -RS "2014" -F":" '/data disk/{ sum += $8 }' myfile

But it would never work because sometimes the amount may be GB, TB, MB. Any thoughts on this?

gawk -F: '
    $1 ~ /^[[:alpha:]]+ [[:digit:]]+$/ {
        if (sum) print sum
        do_sum = ($2 ~ /disk/)
        if (do_sum) printf "%s", $0
    /^[[:blank:]]*$/ {next} 
    do_sum {
        match($8,/([[:digit:].]+)(|GB|TB)/, a)
        if      (a[2] == "GB") { sum += a[1]*1000 } 
        else if (a[2] == "TB") { sum += a[1]*1000*1000 } 
        else                   { sum += a[1] }
    END {if (do_sum) print sum}
' << END



Also does not work. Any pointers?

RE: Selective RS in AWK

I see you're trying over at Stack Overflow, but I concur with them, that code yields:

Jul 2014: data disk -delim :60000 

...given your sample data. So something must be different with your real data. Did you make any progress since?

tgmlify - code syntax highlighting for your tek-tips posts

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close