Print/delete the lines between two pattern.


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print/delete the lines between two pattern.
# 1  
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:
PHP Code:
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:

PHP Code:
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:
Code:
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
# 2  
Hi

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

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

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

This User Gave Thanks to Ygor For This Post:
# 4  
One last version just for fun that will create nastran1.bdf.deleted and nastran1.bdf.new

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

This User Gave Thanks to Chirel For This Post:
# 5  
thanks a ton guys Smilie!
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #602
Difficulty: Medium
MySQL 8.0 incorporates a transactional data dictionary that stores information about database objects.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete all lines before a particular pattern when the pattern is defined in a variable?

I have a file Line 1 a Line 22 Line 33 Line 1 b Line 22 Line 1 c Line 4 Line 5 I want to delete all lines before last occurrence of a line which contains something which is defined in a variable. Say a variable var contains 'Line 1', then I need the following in the output. ... (21 Replies)
Discussion started by: Soham
21 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

4. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

5. Shell Programming and Scripting

print lines up to pattern excluding pattern

11 22 33 44 55 66 77 When pattern 55 is met, print upto it, so output is 11 22 33 44 (1 Reply)
Discussion started by: anilcliff
1 Replies

6. Shell Programming and Scripting

sed pattern to delete lines containing a pattern, except the first occurance

Hello sed gurus. I am using ksh on Sun and have a file created by concatenating several other files. All files contain header rows. I just need to keep the first occurrence and remove all other header rows. header for file 1111 2222 3333 header for file 1111 2222 3333 header for file... (8 Replies)
Discussion started by: gary_w
8 Replies

7. Shell Programming and Scripting

Delete Lines between the pattern

Hi All, Below is my requirement. Whatever coming in between ' ', needs to delete. Input File Contents: ============== This is nice 'boy' This 'is bad boy.' Got it Expected Output =========== This is nice This Got it (4 Replies)
Discussion started by: susau_79
4 Replies

8. Shell Programming and Scripting

Sed to delete exactly match pattern and print them in other file

Hi there, I need help about using sed. Iam using sed to delete and print lines that match the port number as listed in sedfile. I am using -d and -p command for delete match port and print them respectively. However, the output is not synchonize where the total deleted lines is not similar with... (3 Replies)
Discussion started by: new_buddy
3 Replies

9. Shell Programming and Scripting

SED: delete and print the only exact matched pattern

I am really need help with the regular expression in SED. From input file, I need to extract lines that have the port number (sport or dport) as defined. The input file is something like this time=1209515280-1209515340 dst=192.168.133.202 src=208.70.8.23 bytes=2472 proto=6 sport=80 dport=1447... (6 Replies)
Discussion started by: new_buddy
6 Replies

10. UNIX for Dummies Questions & Answers

How to delete lines do NOT match a pattern

On Unix, it is easy to get those lines that match a pattern, by grep pattern file or those lines that do not, by grep -v pattern file but I am editing a file on Windows with Ultraedit. Ultraedit support regular expression based search and replace. I can delete all the lines that match a... (1 Reply)
Discussion started by: JumboGeng
1 Replies

Featured Tech Videos