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?
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.
Comments