質問

This script splits one log file into some smaller files:

#!/bin/awk -f
/topic = / { topic = $NF }    
/ : / { print $3 >> topic "___" $1 }   # $1 is the field name

An input file example:

topic = foo
A   : 23
BB  : Text1
Zz  : 77

topic = bar
A   : 88
B   : 66

topic = foo
A   : 25
B   : 12
BB  : Text2

Example of generated output filenames:

foo___A
foo___B
foo___BB
foo___Zz
bar___A
bar___B

But now, I want to create output fifo instead of regular file.

The fifo (named pipe) should use the same filename as the current regular file. For example, one might write a shell script using the tool as follows:

mkfifo foo___A
mkfifo foo___B
mkfifo foo___BB
mkfifo foo___Zz
mkfifo bar___A
mkfifo bar___B    

common sense:

  • the script should not be aware about all possible topics and fields
  • the script does not require to create several times the same fifo

If is not suitable for this purpose, I am open to any other language as , , , , etc. ...

What programming language would you choose to implement this script? shell/awk/perl/python/ruby...
What do you propose as implementation?


EDIT: Kevin's answer is correct. There is also another alternative of his script:

#!/usr/bin/awk -f

/topic = / { topic = $NF }    
/ : / { 
    file = topic "___" $1
    system("test ! -e "file" && mkfifo "file)
    print $3 > file
}

You can still propose your idea based on awk or any other programming language ;)

To test your script, you can run this below shell command in another terminal:

while true; do find -name '*___*' -ls -exec head '{}' '+'& sleep 1; done
役に立ちましたか?

解決

#!/usr/local/bin/awk -f 

/topic = / { topic = $NF }    
/ : / { 
    file = topic "___" $1
    system("mkfifo "file)
    print $3 > file
    close(file)
    system("rm "file)
}   

Keep in mind that the print will block until you read from the fifo, so you need to either background the awk command or read them from another terminal.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top