sed - only get single occurrence

Roopak Vasa

I am using the below SED command:

sed '/cell.* '"alatch {"'/,/^}/p' -n file

Input file is as under:

cell abc { 
    pins on T {
        a b c
    }
    
}
cell xyz { 
    pins on T {
        x y z
    }
    
}

cell alatch { 
    pins on T {
        VSS VDDPI VDDP
    }
    pins on L {
        IN CT CB
    }
    pins on R {
        OUT
    }
    inputs {
        CB CT IN
    }
    outputs {
        OUT
    }
}
cell alatch { 
    pins on T {
        VSS VDDPI VDDP
    }
    pins on L {
        IN CT CB
    }
    pins on R {
        OUT
    }
    inputs {
        CB CT IN
    }
    outputs {
        OUT
    }
}


Output is as under:

cell alatch { 
    pins on T {
        VSS VDDPI VDDP
    }
    pins on L {
        IN CT CB
    }
    pins on R {
        OUT
    }
    inputs {
        CB CT IN
    }
    outputs {
        OUT
    }
}
cell alatch { 
    pins on T {
        VSS VDDPI VDDP
    }
    pins on L {
        IN CT CB
    }
    pins on R {
        OUT
    }
    inputs {
        CB CT IN
    }
    outputs {
        OUT
    }
}

Expected out is as under:

cell alatch { 
    pins on T {
        VSS VDDPI VDDP
    }
    pins on L {
        IN CT CB
    }
    pins on R {
        OUT
    }
    inputs {
        CB CT IN
    }
    outputs {
        OUT
    }
}

What is needed is that only the first occurrence should be the output. Any suggestion for command?

Kusalananda

Assuming you want the first of the two identical blocks:

$ sed '/cell alatch {/,/^}/!d; /^}/q' file
cell alatch {
    pins on T {
        VSS VDDPI VDDP
    }
    pins on L {
        IN CT CB
    }
    pins on R {
        OUT
    }
    inputs {
        CB CT IN
    }
    outputs {
        OUT
    }
}

The /cell alatch {/,/^}/ range is the range of lines that you want to get as output.

The sed expressions first deletes all lines not in this range, and then quits as soon as a } is found at the start of a line. The q instruction will cause sed to terminate after it outputs the current line, so the final } will get printed.

Executing the d instruction immediately skips to the next input line and branches back to the start of the editing script, so the q instruction has no way of executing unless it's in the range which does not cause d to execute.


With awk, achieving the same effect with code that should be reminiscent of the sed code above:

$ awk '/cell alatch {/,/^}/ { print; if ($0 ~ /^}/) exit }' file
cell alatch {
    pins on T {
        VSS VDDPI VDDP
    }
    pins on L {
        IN CT CB
    }
    pins on R {
        OUT
    }
    inputs {
        CB CT IN
    }
    outputs {
        OUT
    }
}

Actually, this is closer to the sed command

sed -n '/cell alatch {/,/^}/{ p; /^}/q; }' file

which does the same thing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get sed to only match to the first occurrence of a character?

From Dev

sed - Function to replace only the NTH occurrence

From Dev

How to use sed to replace only the first occurrence?

From Dev

preg_match is only matching single occurrence

From Dev

How to remove only the first occurrence of a line in a file using sed

From Dev

Want to replace a string only at first occurrence using sed

From Dev

How to *return* *only* the nth occurrence of a regex match using sed?

From Dev

how to insert file contents on match using sed - first occurrence only

From Dev

How to replace every nth occurrence of pattern using sed ONLY

From Dev

Get only 1 occurrence for the duplicate records

From Dev

Regex, get only first occurrence and stop

From Dev

Restrict sed to delete a single matching line only

From Dev

Vim / Sed: Delete Only Single Blank Lines

From Dev

Validate a string that only contains a single occurrence of some specific substring

From Dev

Get 403 only on a single machine

From Dev

Regular expression for matching single occurrence of ' (single quote) coming only between alphabets not on start or end of the string

From Dev

DOMXPath/PHP - Get a value only after specific occurrence

From Dev

R - Dplyr - get() not applying to every column, only uses first occurrence

From Dev

Powershell Get-Content, want only the first occurrence

From Dev

sed or grep to get only the values without numbers

From Dev

How to get only filename using sed

From Dev

Delete 'N' no lines only on the Nth occurrence of a pattern in a file using the sed/awk command

From Dev

Delete 'N' no lines only on the Nth occurrence of a pattern in a file using the sed/awk command

From Dev

Sed : replace words only with first occurrence of string in the line not till last match

From Dev

Sed in not honoring the $ address char when i try to only match last occurrence from within a script

From Dev

Using sed to replace last occurrence

From Dev

grep only one occurrence

From Dev

Replace only first occurrence

From Dev

Unix: Remove the filename, get only the extension and rename the extension using sed

Related Related

  1. 1

    How can I get sed to only match to the first occurrence of a character?

  2. 2

    sed - Function to replace only the NTH occurrence

  3. 3

    How to use sed to replace only the first occurrence?

  4. 4

    preg_match is only matching single occurrence

  5. 5

    How to remove only the first occurrence of a line in a file using sed

  6. 6

    Want to replace a string only at first occurrence using sed

  7. 7

    How to *return* *only* the nth occurrence of a regex match using sed?

  8. 8

    how to insert file contents on match using sed - first occurrence only

  9. 9

    How to replace every nth occurrence of pattern using sed ONLY

  10. 10

    Get only 1 occurrence for the duplicate records

  11. 11

    Regex, get only first occurrence and stop

  12. 12

    Restrict sed to delete a single matching line only

  13. 13

    Vim / Sed: Delete Only Single Blank Lines

  14. 14

    Validate a string that only contains a single occurrence of some specific substring

  15. 15

    Get 403 only on a single machine

  16. 16

    Regular expression for matching single occurrence of ' (single quote) coming only between alphabets not on start or end of the string

  17. 17

    DOMXPath/PHP - Get a value only after specific occurrence

  18. 18

    R - Dplyr - get() not applying to every column, only uses first occurrence

  19. 19

    Powershell Get-Content, want only the first occurrence

  20. 20

    sed or grep to get only the values without numbers

  21. 21

    How to get only filename using sed

  22. 22

    Delete 'N' no lines only on the Nth occurrence of a pattern in a file using the sed/awk command

  23. 23

    Delete 'N' no lines only on the Nth occurrence of a pattern in a file using the sed/awk command

  24. 24

    Sed : replace words only with first occurrence of string in the line not till last match

  25. 25

    Sed in not honoring the $ address char when i try to only match last occurrence from within a script

  26. 26

    Using sed to replace last occurrence

  27. 27

    grep only one occurrence

  28. 28

    Replace only first occurrence

  29. 29

    Unix: Remove the filename, get only the extension and rename the extension using sed

HotTag

Archive