Visit Our UNIX and Linux User Community

sed print from last occurrence match until the end of last occurrence match

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed print from last occurrence match until the end of last occurrence match
# 1  
Old 12-07-2017
sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like:
03:11:30 a
03:11:40 b
03:13:30 eee
03:13:35 fff
03:14:30 eee
03:15:30 fff

I want the below output

03:14:30 eee
03:15:30 fff

Basically i want to display the lines between START and END of last match occurrence

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-07-2017 at 08:22 AM.. Reason: Added CODE tags.
# 2  
Old 12-07-2017
Welcome to the forum.

Any attempts / ideas / thoughts from your side? Does it have to be sed?
# 3  
Old 12-07-2017
Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework Forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

If you did not post homework, please explain the company you work for and the nature of the problem you are working on. And, please also tell us the operating system and shell you're using. And, as RudiC asked, why sed? This is much easier to do with ed or ex.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.
# 4  
Old 12-07-2017
I have tried below. But its displaying all lines between START and END
sed -n '/START/,/END/p' file.txt

Last edited by Don Cragun; 12-07-2017 at 08:57 AM.. Reason: Add CODE tags again.
# 5  
Old 12-07-2017
I repeat: Is this a homework assignment?

And, for the third time: Why is sed the only option for this task?
# 6  
Old 12-07-2017
I have tried using grep and finding the line numbers of matched strings which worked but its time consuming because in the original scenario there will be thousands of lines between START and END and reading all these lines will be time taking which effects the automation. Looking to resolve it primarily through "sed" and secondarily "awk"

---------- Post updated at 05:35 PM ---------- Previous update was at 05:34 PM ----------

It's not a homework assignment. Its part of automation work for file handling.
# 7  
Old 12-07-2017
You still haven't said why you can't use ed or ex for this. The following code is much simpler than anything you can do with awk or sed:
ed -s file.txt <<-"EOF"

But, of course, this depends on using a shell that uses Bourne shell syntax and may be depending on a particular version of ed (depending on what OS you're using). Telling us what OS and shell you're using anything you start of thread in these forums allows people trying to help you avoid making wild assumptions about your environment and helps you get an answer that will work in your environment faster.
This User Gave Thanks to Don Cragun For This Post:

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to get file content until 2 occurrence of pattern match

AWK command to get file content until 3 occurrence of pattern match, INPUT FILE: JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <custOptIn xmlns="http://com/walm/ta/cu/ccs/xml2"> <person>Romi</person> <appName>SAP</appName> </custOptIn> ... (4 Replies)
Discussion started by: prince1987
4 Replies

2. UNIX for Dummies Questions & Answers

Code for exact match to count occurrence

Hi all, I have an input file as below. I would like to count the occurrence of pattern matching 8th field for each line. Input: field_01 field_02 field_03 field_04 field_05 field_06 field_07 field_08 TA T TA T TA TA TA... (3 Replies)
Discussion started by: huiyee1
3 Replies

3. Shell Programming and Scripting

UNIX help to print 50 lines after every 3rd occurrence pattern till end of file

I need help with extract/print lines till stop pattern. This needs to happen after every 3rd occurrence of start pattern and continue till end of file. Consider below is an example of the log file. my start pattern will be every 3rd occurrence of ERROR_FILE_NOT_FOUND and stop pattern will be... (5 Replies)
Discussion started by: NSS
5 Replies

4. Shell Programming and Scripting

Egrep - Only Match First Occurrence

echo 'String#1 and String#2' | egrep -o -m 1 'String#.{1}' String#1 String#2 I'm trying to just match the first occurrence of 'String#' + 1 character. I thought the "-m 1" switch would do that for me. Instead I get both occurrences. Can somebody provide some insight? Thanks! (5 Replies)
Discussion started by: sudo
5 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Replace first occurrence after match

hey guys, i have been trying to work this thing out with sed with no luck :confused: i m looking for a way to replace only the first occurrence after a match for example : Cat Realized what you gotta do Dog Realized what you gotta do Sheep Realized what you gotta do Wolf Realized... (6 Replies)
Discussion started by: boaz733
6 Replies

6. Shell Programming and Scripting

sed print from last occurrence match until the end of file

Hi, i have file f1.txt with data like: CHECK a b CHECK c d CHECK e f JOB_START .... I want to match the last occurrence of 'CHECK' until the end of the file. I can use awk: awk '/^CHECK/ { buf = "" } { buf = buf "\n" $0 } END { print buf }' f1.txt | tail +2Is there a cleaner way of... (2 Replies)
Discussion started by: ysrini
2 Replies

7. Shell Programming and Scripting

SED to replace exact match, not first occurrence.

Lets say I have file.txt: (Product:Price:QuantityAvailable) (: as delimiter) Chocolate:5:5 Banana:33:3 I am doing a edit/update function. I want to change the Quantity Available, so I tried using the SED command to replace 5, but my Price which is also 5 is changed instead. (for the Banana... (13 Replies)
Discussion started by: andylbh
13 Replies

8. Shell Programming and Scripting

last occurrence of a match through multiple files

Hi all, I have a lot of files with extension ".o" and I would like to extract the 10th line after (last) occurrence of a given string in each of the files. I tried: $ grep "string_to_look_for" *.o -A 10 | tail -1 but it gives the occurrence in the last file with extension .o ... (1 Reply)
Discussion started by: f_o_555
1 Replies

9. Shell Programming and Scripting

Sed to print a string until the second occurrence of a character

Hi, I am totally new to shell scripting. I have a String "c:\working\html\index.txt.12-12-2009.bkp" I want to check if the string has more than one "." character. If it does I would like to retrieve only "c:\working\html\index.txt" i.e, discard the second occurrence of "." and the rest of the... (7 Replies)
Discussion started by: imr
7 Replies

10. Shell Programming and Scripting

Print last occurrence if first field match

Hi All, I have an input below. If the term in the 1st column is equal, print the last row which 1st column is equal.In the below example, it's " 0001 k= 27 " and " 0004 k= 6 " (depicted in bold). Those terms in 1st column which are not repetitive are to be printed as well. Can any body help me... (9 Replies)
Discussion started by: Raynon
9 Replies

Featured Tech Videos