sed find multiple expressions plus next next line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed find multiple expressions plus next next line
# 1  
Old 06-12-2018
Hammer & Screwdriver sed find multiple expressions plus next next line

Hello
I am trying to write sed code where it will look through the text for lines with specific expression "Started by user" and when found, will also add the following line, and also lines with another expression "Finished:"


Code:
sed -n '/Started by user/, +1p, /Finished:/' "${OUTPUT_DIR}/all_job_output"  > ${OUTPUT_DIR}/all_job_outputNew

I would appreciate your help, thank you
# 2  
Old 06-12-2018
Welcome to the forum.


Does the above work, or what goes wrong? And, add some sample input data plus your OS, shell, and sed versions.
# 3  
Old 06-12-2018
Hi yes it goes wrong
Code:
sed: -e expression #1, char 23: extra characters after command

It works if I only run first part without: , /Finished:/
Tried already without coma, with d; but also fails.

I am running it on Centos7 through Jenkins. GNU bash 4.2.46 x86_64, sed 4.2.2

---------- Post updated at 04:29 PM ---------- Previous update was at 03:51 PM ----------

Got It. Took me several hours but here it is:

Code:
sed -n '/\(Started by\|Building in\|Finished:\)/p' ${OUTPUT_DIR}/all_job_output > ${OUTPUT_DIR}/all_job_outputNew

Instead of doing plus one line I have searched for 3rd expression "Building in"

Thanks anyway.

Last edited by dlesny; 06-12-2018 at 11:52 AM.. Reason: added sed version
This User Gave Thanks to dlesny For This Post:
# 4  
Old 06-12-2018
Thanks for sharing.



There were two things wrong with your first command: a , in lieu of a ; , and a p command missing after second address expression. Try

Code:
sed -n '/Started by user/, +1p; /Finished:/p' file

Please not the +1 address form is not standard but a GNU extension.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 06-12-2018
Thank you! Awesome Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

2. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

3. Shell Programming and Scripting

Find 2 expressions then print in a single line

Guys, need help. I have a file that contains something like this: abc def ghi jkl I want to print the first and last line of the file and the output should be in a single line. so, output should be like this: abc jkl (3 Replies)
Discussion started by: solidhelix08
3 Replies

4. Shell Programming and Scripting

FIND w/ multiple expressions

For reference i am still a newb at scripting, but i have what i think is a complex issue. I need to run a FIND on / to identify all files and print them to >> $TEMPFILE I need to avoid MVFS I need to avoid /var/tmp/nastmp/ I was trying find / \( -type d -name nastmp -prune \) -a \( -fstype... (4 Replies)
Discussion started by: nitrobass24
4 Replies

5. Shell Programming and Scripting

using sed to find and replace multiple numbers

I have looked around and there are several examples of how to use sed, but I don't think any of them help me very much with what I am trying to do. I have a text file like this.... 1! SRCNAM = 00001 ! 1! X = 50.0000, 0.0000,... (10 Replies)
Discussion started by: mercury.int
10 Replies

6. Shell Programming and Scripting

SED multiple find and replace

Hi, searched through the forums and not really found what I am looking for. I am a bit of novice when it comes to anything above basic scripting and not even that when it comes to the sed command. I have been reading the tutorials online but still struggling to get what I need :wall: ... (10 Replies)
Discussion started by: colinwilson1303
10 Replies

7. Shell Programming and Scripting

Need to find a multiple line block and replace with a multiple line block

I would perfer to use cut and paste to do this but I can't find a GUI to do this with. What I want to do is to find a multiple line block of code like Exit Sub Log_Handler: then replace it with GoTo RSLogRtn Exit Sub Log_Handler: Basically it is just an insert, but I may want to... (8 Replies)
Discussion started by: Randem
8 Replies

8. Shell Programming and Scripting

sed find and replace multiple lines

I am new to linux and would like to modify the contents of a file preferably using a one line. The situation is as follows <start> some lines "I am the string" "replace string" more lines here <end> In the above example,On encountering "I am the string", the "replace string "should be... (6 Replies)
Discussion started by: supersimha
6 Replies

9. Shell Programming and Scripting

single line input to multiple line output with sed

hey gents, I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm... (8 Replies)
Discussion started by: mitch
8 Replies

10. Shell Programming and Scripting

Searching multiple files with multiple expressions

I am using a DEC ALPHA running Digital UNIX (formly DEC OSF/1) and ksh. I have a directory with hundreds of files that only share the extension .rpt. I would like to search that directory based on serial number and operation number and only files that meet both requirements to be printed out. I... (6 Replies)
Discussion started by: Anahka
6 Replies
Login or Register to Ask a Question