Results 1 to 3 of 3
Hi all,
I'm currently trying to analyse a rather large file that has returned data for numerous machines. Each record starts with "MachineTag=" then has a variable number of lines ...
- 09-23-2011 #1Just Joined!
- Join Date
- Sep 2011
- Posts
- 2
Sed/Awk - quit after match found
Hi all,
I'm currently trying to analyse a rather large file that has returned data for numerous machines. Each record starts with "MachineTag=" then has a variable number of lines of data after.
So, as a really high-level example:
MachineTag=abc001 daata=here data=here, etc...
more data=here more data=here
even more=here
MachineTag=abc002 data=here
MachineTag=abc003 data=here data=here data=here data=here
more data=here more data=here
even more data=here
much more data=here much more data=here
MachineTag=abc004
(and so on)
I have a list of all the "MachineTag" values that I'm going through one at a time and assigning as a variable, so at the moment I'm using sed to pull all the lines of data for each machine. Example:
sed -n '/MachineTag=$var/,/MachineTag/p' ./my_data_file |head -n -1
So, for example, this returns:
MachineTag=abc003 data=here data=here data=here data=here
more data=here more data=here
even more data=here
much more data=here much more data=here
...for machine abc003. Perfect.
My problem is thus; there are hundreds of thousands of records in the text file and I want to shave whatever time I can. To that end, I've been trying to find a way to get sed to quit after it's returned the above data, instead of continuing through to the end of the file. I've used:
sed -n '/MachineTag=$var/,/MachineTag/{p;q;}' ./my_data_file
...but it seems to stop after the first line of the match. So, using the machine abc003 example above, this becomes just:
MachineTag=abc003 data=here data=here data=here data=here
I can't get it to quit after the full pattern match instead of after just the first line. If that makes any sense?
I've tried AWK but, because the search strings both have MachineTag at the start, it only ever prints out that first line. I think. My AWK line is as follows:
awk -v awkvar=$var '/MachineTag=awkvar/,/MachineTag/ {print;exit}' ./my_data_file
...but as I said, even if I drop the print;exit command, it only ever returns the first line of the dataset.
Any help on getting sed to stop searching once it's returned ALL lines would be massively appreciated!
Sorry if this isn't clear enough...
- 09-23-2011 #2Just Joined!
- Join Date
- Feb 2007
- Posts
- 6
I would think adding 1 to the begining of the sed statement would make it only look for the first match, and then stop..
Code:sed -n '1,/MachineTag=$var/,/MachineTag/p' ./my_data_file |head -n -1
- 09-25-2011 #3Just Joined!
- Join Date
- Sep 2011
- Posts
- 2
I've tried that and it doesn't seem to like being used alongside the /,/ in the middle


Reply With Quote