awksedgrepcut

Separate lines with keys and store in different files


How to separate (get) the entire line related to hexadecimal number keys and the entire line for DEBUG in a text file, then store in different file, where the key is in this format: "[ uid key]"? i.e. ignore any lines that is not DEBUG.

in.txt:

  [ uid 28fd4583833] DEBUG web.Action
  [ uid 39fd5697944] DEBUG test.Action
  [ uid 56866969445] DEBUG test2.Action
  [ uid 76696944556] INFO  test4.Action
  [ uid 39fd5697944] DEBUG test7.Action
  [ uid 85483e10256] DEBUG testing.Action

The output files are named as "out" + i + ".txt", where i = 1, 2, 3, 4. i.e.

out1.txt:

  [ uid 28fd4583833] DEBUG web.Action

out2.txt:

  [ uid 39fd5697944] DEBUG test.Action
  [ uid 39fd5697944] DEBUG test7.Action

out3.txt:

  [ uid 56866969445] DEBUG test2.Action

out4.txt:

  [ uid 85483e10256] DEBUG testing.Action

I tried:

awk 'match($0, /uid ([^]]+)/, a) && /DEBUG/ {print > (a[1] ".txt")}' in.txt

Solution

  • If your file format is consistent as you show, you can just do:

    awk '
        $4!="DEBUG" { next }
        !($3 in f) { f[$3] = "out" (++i) ".txt" }
        { print > f[$3] }
    ' in.txt
    

    Or to avoid the open file issue mentioned in the comment:

    awk '
        $4!="DEBUG" { next }
        !($3 in f) { f[$3] = "out" (++i) ".txt" }
        {
            o = f[$3]
            if (o!=p && p) close(p)
            print >> o
            p = o
        }
    ' in.txt