Print/delete the lines between two pattern.

Hello,

I am having hard time figuring out how to print/delete the lines between two pattern. Here is the part of the file nastran1.bdf:

RBE3       48729           32232  123456 0.30000     123   59786   59787
           60114
RBE3       48732            1330     123 0.30000     123   10107   10108
           10109   10110   10113   10115   10120   10121   10122   10123
           10124   10126   10127   10131   10132   10133   10134   10164
           10167   10169   10170   10171   10176   10177   10178   10179
           10180   10181   10184   10189   10190   10191   10192   10193
           10198   10199   10200
CTRIA3     10161 2561010   23790   23791   23909

I need a way to get only this portion of the text:

RBE3       48729           32232  123456 0.30000     123   59786   59787
           60114
RBE3       48732            1330     123 0.30000     123   10107   10108
           10109   10110   10113   10115   10120   10121   10122   10123
           10124   10126   10127   10131   10132   10133   10134   10164
           10167   10169   10170   10171   10176   10177   10178   10179
           10180   10181   10184   10189   10190   10191   10192   10193
           10198   10199   10200

I am trying with this to take the lines between the pattern:

sed -n '/^RBE/,/^ [0-9]/p' nastran1.bdf > rbe.data

but unfortunately without success. After getting the lines in rbe.data I will need to delete them from nastran1.bdf. Any ideas will be highly appreciated. Thank you

Hi

To get rbe.data

awk '/^[A-Z]/ { k=match($1,/RBE/); } { if (k) print; }' nastran1.bdf >rbe.data

To generate new.nastran1.bdf and rbe.data

awk '/^[A-Z]/ { k=match($1,/RBE/); } { if (k) print >"rbe.data"; else print >"new.nastran1.bdf"; }' nastran1.bdf

Try...

awk '$1~/^RBE/{f=1}$1!~/^RBE/&&$1~/[A-Z]/{f=0}f' nastran1.bdf > rbe.data
1 Like

One last version just for fun that will create nastran1.bdf.deleted and nastran1.bdf.new

awk '/^[A-Z]/ { k=match($1,/RBE/); } { print >((k)?FILENAME".deleted":FILENAME".new"); }' nastran1.bdf
1 Like

thanks a ton guys :)!