Ani
Ani

Reputation: 1018

Selective string operation

I need help in string processing in CSH/TCSH script. I know basic processing, but need help understand how we can handle advance string operation requirements.

I have a log file whose format is something like this:

[START_A]

log info of A

[END_A]

[START_B]

log info of B

[END_B]

[START_C]

log info of C

[END_C]

My requirement is to selectively extract content between start and end tag and store them in a file. For example the content between START_A and END_A will be stored in A.log

Upvotes: 0

Views: 140

Answers (3)

potong
potong

Reputation: 58420

This might work for you:

sed '/.*\[START_\([^]]*\)\].*/s||/\\[START_\1\\]/,/\\[END_\1\\]/w \1.log|p;d' file |
sed -nf - file

Upvotes: 0

Kent
Kent

Reputation: 195059

this should work for you:

awk -F'_' '/\[START_.\]/{s=1;gsub(/]/,"",$2);f=$2".txt";next;}/\[END_.\]/{s=0} s{print $0 > f}' yourLog

test:

kent$  cat test
    [START_A]

    log info of A

    [END_A]

    [START_B]

    log info of B

    [END_B]

    [START_C]

    log info of C

    [END_C]


kent$  awk -F'_' '/\[START_.\]/{s=1;gsub(/]/,"",$2);f=$2".txt";next;}/\[END_.\]/{s=0} s{print $0 > f}' test

kent$  head *.txt
==> A.txt <==

    log info of A


==> B.txt <==

    log info of B


==> C.txt <==

    log info of C

Upvotes: 1

John3136
John3136

Reputation: 29265

Awk can do them 1 at a time:

cat log | awk '/[START_A]/,/[END_A]/'

Upvotes: 0

Related Questions