-1

I'm trying to achieve the following.

  1. Find first '(' symbol (always located at the start of a new line) in a text file and make the hostname that follows a variable such as '(host' becomes 'host'.

  2. Copy text between first instance of '=-=-=' delimiter and second instance of the same delimiter ('=-=-=') to a new file whose name is the variable in item 1 such as host-messages.txt

  3. Copy text between second instance of '=-=-=' delimiter and third instance of the same delimiter to a new file whose name is the variable in item 1 such as host-df.txt

  4. Copy text between third instance of '=-=-=' delimiter and subsequent next instance of a '(' on the first character of a new line such as the next instance of a '(hostname',to a new file whose name is the variable in item such as host-dfa.txt

  5. Repeat steps 1 through 4 until end of file and no more '(host' are found.

Anyway what I'm trying to achieve is extract data such as:

(hostname1 : 056603)  1    
=-=-=
TEXT
TEXT
TEXT
=-=-=
TEXT2
TEXT2
TEXT2
=-=-=
TEXT3
TEXT3
TEXT3
(hostname2
=-=-=
etc...

Newly created files should be hostname1-messages.txt, hostname1-df.txt and hostname1-dfa.txt.

The best I've come up with so far is

awk '/=-=-=/{x="F"++i;next}{print > x;}' test.txt

but it's not working.

4

1 回答 1

1

Post some sample input and expected output to help us help you but something like this is what you want (untested, obviously):

awk '
BEGIN{ sfx[1]="-messages.txt"; sfx[2]="-df.txt"; sfx[3]="-dfa.txt" }
sub(/^\(/,"") { host=$0; nr=0; next }
/=-=-=/ { nr++; next }
{ print > host sfx[nr] }
' file
于 2012-10-22T01:27:08.423 回答