2

I've been trying to pull a field from a row in a file although each row may have plus or minus 2 or 3 fields per row. They aren't always equal in the number of fields per row.

Here is a snippet:

A        orarpp 45286124        1     1   0 20  60   Nov 25  9-16:42:32    01:04:58 11176 117056      0   - oracleXXX (LOCAL=NO)
A        orarpp 45351560        1     1   3 20  61   Nov 30  5-03:54:42    02:24:48  4804 110684      0   - ora_w002_XXX
A        orarpp 45548236        1     1  22 20  71   Nov 26  8-19:36:28    00:56:18 10628 116508      0   - oracleXXX (LOCAL=NO)
A        orarpp 45679190        1     1   0 20  60   Nov 28  6-23:42:20    00:37:59 10232 116112      0   - oracleXXX (LOCAL=NO)
A        orarpp 45744808        1     1   0 20  60 10:52:19    23:08:12    00:04:58 11740 117620      0   - oracleXXX (LOCAL=NO)
A          root 45810380        1     1   0 --  39   Nov 25  9-19:54:34    00:00:00   448   448      0   - garbage

In the case of the first line, I'm interested in 9-16:42:32 and the similar fields for each row.

I've tried to pull it by using ':' as the field separator and then filter from there however, what I am trying to accomplish is to do something if the number before the dash (in the example it's 9) is greater than one.

cat file.txt | grep oracle | awk -F: '{print substr($1, length($1)-5)}'

This is because the number of fields on either side of the actual field I need can be different from line to line.

Definitely not the most efficient but I've been trying to do this with an awk one liner.

Hints or a direction would be appreciated to get me moving again. I am not opposed to doing in a better way than awk.

Thanks.

3 Answers 3

4

Maybe cut is the right tool for this job? For example, with your snippet:

$ cut -c 62-71 file.txt
9-16:42:32
5-03:54:42
8-19:36:28
6-23:42:20
  23:08:12
9-19:54:34

The arguments tell cut to snip columns (-c) 62 through 71.

For additional processing, you can pipe it to awk.

You can also accomplish the whole thing in awk by accepting entire lines and then using substr to extract the columns you want. For example, this awk command produces the same output as the cut command above:

awk '{ print substr($0, 62, 10) }' file.txt

Whether you create a pipeline or do the processing entirely in awk is at least in part a matter of personal taste / style.

1
  • This fits much like the one below. Dec 6, 2013 at 14:23
2

Would this do?

awk -F: '/oracle/ {print substr($0,62,10)}' file.txt
9-16:42:32
8-19:36:28
6-23:42:20
  23:08:12

This search for oracle and then print 10 characters starting from position 62

1
  • This fits perfectly. For some reason I didn't figure to notice field I was looking for started at character 61 no matter the number of fields in the files. Thanks a bunch. Dec 6, 2013 at 14:23
2

You can grab those identifiers with one of

grep -o '[[:digit:]]\+-[[:digit:]]\{2\}:[[:digit:]]\{2\}:[[:digit:]]\{2\}'
grep -oP '\d+-\d\d:\d\d:\d\d'    # GNU grep 

It sounds like you want to do something with the lines, not just find the ids. Please elaborate.

Using GNU awk:

gawk --re-interval '
    /oracle/ && \
    match($0, /([[:digit:]]+)-([[:digit:]]{2}:){2}[[:digit:]]{2}/, a) && \
    a[1]>1 {
        # do something with the matching line
        print
    }
' file
1
  • I should have mentioned gawk wasn't an option. Not using a linux based system. Dec 6, 2013 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.