-1

the file like:

File /home/user/

int yl_init(void);

File /home/user/

int yl2_init(void);

I want use sed -n '/File/,/;/p' file but it shows that

File /home/user/

int yl_init(void);

File /home/user/

int yl2_init(void);

I only want to get the first match result like:

File /home/user/

int yl_init(void);

I only want sed .

choujayyl
  • 91
  • 1
  • 4
  • 12

2 Answers2

3

This might work for you (GNU sed);

sed '/File/,/;/!d;/;/q' file
  • /File/,/;/!d delete all lines not between File and ;
  • /;/q quit on encountering a line containing ;
potong
  • 55,640
  • 6
  • 51
  • 83
  • why delete all lines not between File and ';',can i sed the strings and then quit on encountering a line containing ; directly? – choujayyl Jan 16 '13 at 07:19
  • @choujayyl sed normally prints all lines that are passed though it, unless the `-n` switch is activated. The `-n` is grep-like in nature, suppressing the output of lines unless explicitly printed by a `p` or `P` command. So `sed -n '/File/,/;/p' file` and `sed '/File/,/;/!d' file` have the same result. – potong Jan 16 '13 at 10:32
2

You can use a the q command to cause sed to exit when the second pattern is matched:

sed -n '/File/,$p;/;/q'
Rob Davis
  • 15,597
  • 5
  • 45
  • 49
  • Print every line between `/File/` and the end of the file, but quit after it prints a line with a semicolon. – Rob Davis Jan 16 '13 at 07:17