Remove all line below the pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove all line below the pattern
# 1  
Old 03-21-2012
Remove all line below the pattern

I have few files i want to delete all line before match and also print the match line. like

Quote:
Input:- less james1

aab
bbc
ccd
dde
eef
ffg
ggh
hhi

Output:-

"match pattern 'ccd' "

aab
bbc
ccd


I am using below command but it gives me output like

aab
bbc

sed -e '0,/ccd/d' tmp
Can anyone help.

Last edited by james1988; 03-21-2012 at 02:56 PM..
# 2  
Old 03-21-2012
Code:
awk '/ccd/ { P=1 } P'

# 3  
Old 03-21-2012
Thanks corona688.

But now my problem is i want to print my pattern also. as i mention above.


Quote:

Input:- less james1

aab
bbc
ccd
dde
eef
ffg
ggh
hhi

Output:-

"match pattern 'ccd' "

aab
bbc
ccd


I am using below command but it gives me output like

aab
bbc
Code:
sed -e '0,/ccd/d' tmp OR awk '/ccd/ { P=1 } !P' tmp

Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples, thank you.

Last edited by Franklin52; 03-21-2012 at 06:56 PM.. Reason: Please use code tags for data and code samples, thank you
# 4  
Old 03-21-2012
Code:
awk -v PAT="ccd" 'BEGIN { printf("match pattern '%s'\n\n", PAT); }; $0 ~ PAT { P=1 } P'

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 03-22-2012
Code:
 
$ awk '/ccd/{print;exit}1' test.txt
aab
bbc
ccd

This User Gave Thanks to itkamaraj For This Post:
# 6  
Old 03-22-2012
Code:
awk '1;/ccd/{exit}' file

Code:
sed '/ccd/q' file

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 03-22-2012
Quote:
Originally Posted by james1988
I am using below command but it gives me output like

aab
bbc
sed -e '0,/ccd/d' tmp
Try like..
Code:
sed '1,/ccd/!d' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove matching pattern on each line with number variations

Hello folks! I have a file containing lines like this Something text 18:37Remove This: 1,111"Keep this text" Some more text 19:37Remove This: 222"Keep this text" More text 20:50Remove This: 3,333Keep this text And more text 25:50Remove This: 44,444Keep this text I would like to... (4 Replies)
Discussion started by: martinsmith
4 Replies

2. Shell Programming and Scripting

Remove entire line from a file if 1st column matches a pattern

I have one requirement to delete all lines from a file if it matches below scenario. File contains three column. Employee Number, Employee Name and Employee ID Scenario is: delete all line if Employee Number (1st column) contains below 1. Non-numeric Employee Number 2. Employee Number that... (3 Replies)
Discussion started by: anshu ranjan
3 Replies

3. Shell Programming and Scripting

Remove duplicate line starting with a pattern

HI, I have the below input file /* ----------------- cmdsDlyStartFWJ -----------------*/ UNIX_JOB CMDS065J RUN ANY CMDNAME sleep 5 AGENT CMDSHP USER proddata RUN MON,TUE,WED,THU,FRI DELAYSUB 02:00 /* "Triggers daily file watcher jobs" */ ENVAR... (5 Replies)
Discussion started by: varun22486
5 Replies

4. Shell Programming and Scripting

awk to search for pattern and remove line

I am an awk beginner and need help figuring out how to search for a number in the first column and if it (or anything greater) exists, remove those lines. AM11400012012 2.26 2.12 1.98 2.52 3.53 3.01 3.62 5.00 3.65 7.95 0.79 3.88 0.00 AM11400012013 3.39 2.29 ... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

5. Shell Programming and Scripting

Search a pattern in a line and remove another pattern

Hi, I want to search a pattern in a text file and remove another pattern in that file. my text file look like this 0.000000 1.970000 F 303 - 1.970000 2.080000 VH VH + 2.080000 2.250000 VH VH + 2.250000 2.330000 VH L - 2.330000 2.360000 F H + 2.360000 2.410000 L VL - 2.410000 ... (6 Replies)
Discussion started by: sreejithalokkan
6 Replies

6. Shell Programming and Scripting

Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts, I want to remove first and last line after pattern match "vg" : I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data. - So it should check for the pattern vg if present , then it should delete the line ,... (5 Replies)
Discussion started by: rveri
5 Replies

7. Shell Programming and Scripting

awk delete/remove rest of line on multiple search pattern

Need to remove rest of line after the equals sign on search pattern from the searchfile. Can anybody help. Couldn't find any similar example in the forum: infile: 64_1535: Delm. = 86 var, aaga 64_1535: Fran. = 57 ex. ccc 64_1639: Feb. = 26 (link). def 64_1817: mar. = 3/4. drz ... (7 Replies)
Discussion started by: sdf
7 Replies

8. Shell Programming and Scripting

Remove if the above line matches pattern

but keep if does not I have a file: --> my.out foo: bar foo: moo blarg i am on vacation foo: goose foo: lucy foo: moose foo: stucky groover@monkey.org foo: bozo grimace@gonzo.net dear sir - blargo blargo foo: goon foo: sloppy foo: saudi gimme gimme gimme (3 Replies)
Discussion started by: spacegoose
3 Replies

9. Shell Programming and Scripting

Need to remove few characters from each line till a pattern is matched

Hi All, I want to remove first few characthers from starting of the line till ',' Comma... which needs to be done for all the lines in the file Eg: File content 1,"1234",emp1,1234 2,"2345",emp2,2345 Expected output is ,"1234",emp1,1234 ,"2345",emp2,2345 How can parse... (4 Replies)
Discussion started by: kiranlalka
4 Replies

10. Shell Programming and Scripting

shell script to remove all lines from a file before a line starting with pattern

hi,, i hav a file with many lines.i need to remove all lines before a line begginning with a specific pattern from the file because these lines are not required. Can u help me out with either a perl script or shell script example:- if file initially contains lines: a b c d .1.2 d e f... (2 Replies)
Discussion started by: raksha.s
2 Replies
Login or Register to Ask a Question