logo
down
shadow

AWK between 2 patterns - first occurence


AWK between 2 patterns - first occurence

By : user2956070
Date : November 22 2020, 10:54 AM
help you fix your problem An (g)awk way that doesn't need a set number of fields(although i have assumed that contains; will always be on the line you need the names from.
code :
(g)awk '(x+=/Z1/)&&match($0,/contains;([^|]+)/,a)&&gsub(";","\n",a[1]){print a[1];exit}' f
(x+=/Z1/)                       - Increments x when Z1 is found. Also part of a 
                                  condition so x must exist to continue.

match($0,/contains;([^|]+)/,a)  - Matches contains; and then captures everything after 
                                  up to the |. Stores the capture in a. Again a 
                                  condition so must succeed to continue.

gsub(";","\n",a[1])             - Substitutes all the ; for newlines in the capture 
                                  group a[1].


{print a[1];exit}'              - If all conditions are met then print a[1] and exit.
awk '(x+=/Z1/)&&/contains/{split($0,a,"|");y=split(a[2],b,";");for(i=3;i<=y;i++) 
      print b[i];exit}' file


Share : facebook icon twitter icon
awk print between nth occurence of matching patterns

awk print between nth occurence of matching patterns


By : Moreshwar V. Boraman
Date : March 29 2020, 07:55 AM
may help you . Here is an example that uses regular expressions. If there are minor variations in the text format, this will handle them. Also this collects all the values in an array; you could then do further processing (sort values, remove duplicates, etc.) if you wish.
code :
#!/usr/bin/awk -f

BEGIN {
    i_result = 0
    DQUOTE = "\""
}

{
    line = $0
    for (;;)
    {
        i = match(line, /CardDetail \[ **([^]]*) *\]/, a)
        if (0 == i)
            break
        # a[1] has the text from the parentheses
        s = a[1]
        # replace from this: a, b, c   to this:  "a","b","c"
        gsub(/ *, */, "\",\"", s)
        s = DQUOTE s DQUOTE

        results[i_result++] = s
        line = substr(line, RSTART + RLENGTH - 1)
    }
}

END {
    for (i = 0; i < i_result; ++i)
        print results[i]
}
#!/usr/bin/python

import re
import sys

DQUOTE = "\""

pat_card = re.compile("CardDetail \[ *([^]]*) *\]")
pat_comma = re.compile(" *, *")

results = []

def collect_cards(line, results):
    while True:
        m = re.search(pat_card, line)
        if not m:
            return
        len_matched = len(m.group(0))
        s = m.group(1)
        s = DQUOTE + re.sub(pat_comma, '","', s) + DQUOTE
        results.append(s)
        line = line[len_matched:]

if __name__ == "__main__":
    for line in sys.stdin:
        collect_cards(line, results)

    for card in results:
        print card
#!/usr/bin/awk -f

BEGIN {
    i_result = 0
    DQUOTE = "\""
    COMMA = ","
}

{
    line = $0
    for (;;)
    {
        i = match(line, /deviceID=([A-Za-z_0-9]*),/, a)
        s0 = DQUOTE a[1] DQUOTE
        i = match(line, /CardDetail \[ **([^]]*) *\]/, a)
        if (0 == i)
            break
        # a[1] has the text from the parentheses
        s = a[1]
        # replace from this: foo=a, bar=b, other=c   to this:  "a","b","c"
        gsub(/[A-Za-z_][^=,]*=/, "", s)
        # replace from this: a, b, c   to this:  "a","b","c"
        gsub(/ *, */, "\",\"", s)
        s = s0 COMMA DQUOTE s DQUOTE

        results[i_result++] = s
        line = substr(line, RSTART + RLENGTH - 1)
    }
}

END {
    for (i = 0; i < i_result; ++i)
        print results[i]
}
match occurence of 2 patterns with perl regex

match occurence of 2 patterns with perl regex


By : Martin
Date : March 29 2020, 07:55 AM
will be helpful for those in need Suppose I have 2 regex patterns [\d\d] and [a-zA-Z][a-zA-Z] and i want to check if a string has those patterns in any number and in any order and only those patterns separated by comma how can i do this with perl ? , Try this :
code :
^(([A-Za-z]{2}|\d\d),)*([A-Za-z]{2}|\d\d)$
finding multiple lines patterns and stop after first occurence

finding multiple lines patterns and stop after first occurence


By : Cousax
Date : March 29 2020, 07:55 AM
wish of those help I have got plenty of files in different location including */synth/debug/* in their path pattern, all files have got *.list extension. Files look like: , Using awk
code :
find . -name *.list -path "*/synth/debug/*" -type f -exec awk '/MODULE XXX/{xxx=1;next}xxx{xxx+=1}/SIGNED.*1/&&xxx<=4{print FILENAME; exit}xxx>4{nextfile;}ENDFILE{xxx=0}' {} +
find . -name *.list -path "*/synth/debug/*" -type f -exec awk '/MODULE/{current=$2; line=1; next}current in results{next;}{line+=1}/SIGNED.*1/&&line<=4{results[current]=FILENAME}END{for(module in results){print module, results[module]}}' {} +
ABC file2.txt
XXX file1.txt
XYZ file1.txt
Reduce consecutive duplicate patterns in list to single occurence

Reduce consecutive duplicate patterns in list to single occurence


By : edmond
Date : March 29 2020, 07:55 AM
will help you Please suggest an approach or algorithm reference for the following problem.
how to grep between 2 patterns - second occurence BASH

how to grep between 2 patterns - second occurence BASH


By : mustafasirki
Date : March 29 2020, 07:55 AM
This might help you You don't need to get as complicated as you describe just to print the File = lines in the target block. All you need is:
code :
$ awk '/Name = /{f=/"FileseT-XXXXX"/} f && /File =/' file
       File = /etc
       File = /usr/sbin
       File = /root
       File = /srv/
Related Posts Related Posts :
  • Security of su root in bash script
  • how to replace a string at a specific position in a csv file using bash
  • how to delete duplicate lines in a text file in unix bash?
  • Compare a variables in bash
  • How do I use sed on a string?
  • Using bash variables for range in sed
  • convert a DURATION HH:MM:SS to seconds in bash
  • Shell make system paths click-able when printed out by my shell script
  • I want to use parallel-ssh to run a bash script on multiple servers, but it simple prints the echo statements
  • Nginx on docker - daemonize and attach to bash
  • Delete or Replace nth line in a file after a pattern match using shell scripts
  • Actual return code for SCP
  • How to exit a bash function that expects a return
  • Bash script to split file into two files while changing output file during processing
  • Pass command line arguments via sbatch
  • variable substitution (what do you call/name this method)?
  • Call function from another shell script
  • How to Grep and Replace With Exclusions
  • shell command to remove characters after a special character in bash/shell
  • How do I run a script for 1st working day of every month in cron?
  • Bash Unexpected End of File Error After Installing RVM
  • Nested Loop with increment on inner loop?
  • video orientation detection in bash
  • Interrupt sleep in bash with a signal trap
  • Want file rename script to add number at end with fixed length
  • What's the DRYest way to replace a file with a symlink in bash
  • How to run Bitcoin's autogen.sh file?
  • Renaming files in folder and subfolders using a list- Bash Script/Unix
  • Bash command to compare more than two files
  • If It is a directory append text
  • What's wrong with my WHILE-DO loop, or why my script can't find the file I'm giving it on a plate?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org