Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

match pattern in string then write to new file (sed or awk)

Status
Not open for further replies.

Wrathchild

Technical User
Aug 24, 2001
303
US
greetings, I'm very green to sed & awk and am working on a project to read each line in a file and if it contains "START" or "END" then I want to capture those lines plus anything in between them. This is a log file so I'm looking to get the beginning and end of a transaction. Also I only want certain users' records, in this case USERID001 & USERID002.

If I understand correctly, sed is used for reading whole lines whereas awk is more for columns or fields. So I was thinking sed would be more applicable for this instance, but I'm not really sure.

Sample log file:
"Blah blah START blah USERID001"
"Blah blah blah"
"Blah blah blah"
"Blah END"
"this line shouldn't be included"
"Blah START USERID003"
"Blah"
"Blah END"
"Blah START USERID002"
"Blah"
"Blah END"

New file should read:
"Blah blah START blah USERID001"
"Blah blah blah"
"Blah blah blah"
"Blah END"
"Blah START USERID002"
"Blah"
"Blah END"

I've tried various things with sed & awk but most of the examples I see are for replacing text or parsing out just the pattern matched on.

thanks for the help as always
 
sed -n '/START.*USERID00[12]/,/END/p' file.log

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
awk '/START.*USERID00[12]/,/END/' file.log

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
man you make it look easy! thanks!

and if want to add more criteria, would it be something like:
sed -n '/START.*USERID00[12],*Blah/,/END/p' file.log
 
looks like it's just a space for multiple criteria...
sed -n '/START.*USERID00[12] *Blah/,/END/p' file.log
 
my last post was incorrect, for multiple criteria it should be:
sed -n '/START.*USERID00[12].*Blah/,/END/p' file.log

Also, if I wanted to include an OR operator (USERID001 OR NEWHIRE235) is there a way to do that on one line? I'm going to use multiple lines for now but I was just curious.
 
awk '/START.*(USERID001|NEWHIRE235)/,/END/' file.log

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top