Find the answer to your Linux question:
Results 1 to 2 of 2
Hello, I need help with awk for a project, I don't know much about awk and have no idea how to do what I need: I have an input in ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    1

    Please help with awk search and move

    Hello, I need help with awk for a project, I don't know much about awk and have no idea how to do what I need:

    I have an input in the form of an output from truss/strace, for example:

    28131: open("2", O_RDONLY, <unfinished ...>
    28133: open("1", O_RDONLY|O_GASAS) = 0
    28134: openat("bin", O_RDONLY) = -1 ENOENT (No such file or directory)
    28135: open("ksh", O_RDONLY, <unfinished ...>
    28135: <... open resumed>) = -1 ENOENT (No such file or directory)
    28136: open("3",O_RDONLY,0666) = 0 (0x00)
    28131: <... open resumed>) = 0
    28137: open64("etc",O_RDONLY,0666) ERR#2 'No such file or direct'
    28138: open("4", O_RDONLY,0666) = 0

    It contains processes that were interrupted, hence the <unfinished ...> and <... .* resumed>

    I need to take the whole <... .* resumed> line and move it (e.g. copy and delete) behind the line with the matching PID, like this:

    28131: open("2", O_RDONLY, <unfinished ...>28131: <... open resumed>) = 0
    28133: open("1", O_RDONLY|O_GASAS) = 0
    28134: openat("bin", O_RDONLY) = -1 ENOENT (No such file or directory)
    28135: open("ksh", O_RDONLY, <unfinished ...>28135: <... open resumed>) = -1 ENOENT (No such file or directory)
    28136: open("3",O_RDONLY,0666) = 0 (0x00)
    28137: open64("etc",O_RDONLY,0666) ERR#2 'No such file or direct'
    28138: open("4", O_RDONLY,0666) = 0

    Assume PID reusing (e.g. one PID for different consecutive processes).
    And to make things even more confusing for me, it has to be written so that it works in a pipe as a line command in cat file | awk | grep | sed

    If you have any ideas how to do this, please explain it to me, I don't want to copy someone else's solution and the forget it, but I'd like to understand why and how it works.

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I'm sorry to say that homework problems are not allowed in this forum, so I am going to lock this thread.

    Before I do that, I will try to give you a bit of advice, though.

    awk has a feature called arrays that are actually more like hashmaps in other languages:
    The GNU Awk User's Guide

    These arrays allow you to map a key (in this case, for instance, a PID) to something else (perhaps, say, a string).

    Interestingly, awk also allows you to emulate multidimensional arrays, so you could imagine something like this:
    Code:
    array[28131, 0] = open("2", O_RDONLY, <unfinished ...>
    array[28131, 1] = <... open resumed>) = 0
    Which is to say, an array that maps PIDs to an array of strings.

    Now, how exactly you create this array is up to you. Also, depending on how much input you receive, this may not work, as it stores everything in memory. However, if you are going to be receiving these messages in any order, it's the simplest way that I know of to help you.

    Best of luck. If you have a more specific question in the future, please feel free to ask it then.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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