简体   繁体   中英

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

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

Awk可以一次执行1个操作:

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

This might work for you:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM