Basically the user will open a file and grep for the ACTNUMBER.
I've gotten as far as getting the file open with exec.
What I want the script to do is to grep for the ACTNUMBER and only print the line (with echo) if the line contains a START and the ACTNUMBER. I also want the script to echo all the data in the lines after until it reaches the word FINISH
I'm looking for the results in a numbered format output to display, with a carriage return between the results.
There's a twist though. Sometimes theres a nested START and FINISH within a START and FINISH and I need to have the script look for that as well. So it it finds two STARTS then find two ENDS. So if it finds one START, look for another START, if it finds another START look for two FINISH's.
I know grep only does one line at a time,
so if i do a grep -n START | grep -n ACTNUMBER=$ACTNUMBER
it will output lines with both the START and ACTNUMBER, and I need to get it to print all the lines after until it finds the line that read FINISH.
So what I need it to do is look for a START and ACTNUMBER. If it finds it then echo that particular line. Then search next line, even if it doesnt find it, to print that line, and the next, and if it reaches a FINISH, print the line, then start loop over.
I have a feeling some counters might be involved here but I'm really not sure how to implement this.
grep is not really a good tool to use for this kind of thing. I suggest you look at either awk, perl or sed.
For example, here is one way of printing all lines between START/ACTNUMBER and FINISH using sed:
It does not handle the nested case. For that you need to use a tool like awk or perl.
For your sample data this should work:
As for the nested START/FINISH, please post example of such case and the desired output.
Thanks..
There are multiple instances of START to FINISH with the ACTNUMBER within. This script needs to display all of those instances.
However some have another START and FINISH within the START and FINISH.
So sometimes, it will look like this
xxxxxx START xxxxxxxxxxxxxxxx ACTNUMBER=1234 xxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxx FINISH xxxxxxxxxxxxxxxxxxxxxxxxxx
This is a long log file so there will be multiple instances of these log snippets then I need the script to display to output in a numbered format with a carriage return between each instance
Someone mentioned grep and using a counter to look for the first and second instance of START and the first and second instance of FINISH if there is one.
Basically what I'm looking for is
for it to find START , if theres a start look for the ACTNUMBER
print or echo that particular line
Look for the same on next line. if this is false, still print the line
Look for the same on next line. if this is false, still print the line
Look for FINISH. If this is true, print the line, then I need it to loop recursively.
For the past week I've been in the grep state of mind. I guess I was way off lol
grep -n START | grep -n ACTNUMBER=$ACTNUMBER gets me the START and ACTNUMBER but I just couldn't figure out how to get those lines to print until FINISH
---------- Post updated at 10:59 AM ---------- Previous update was at 10:57 AM ----------
Quote:
Originally Posted by fpmurphy
grep is not really a good tool to use for this kind of thing. I suggest you look at either awk, perl or sed.
For example, here is one way of printing all lines between START/ACTNUMBER and FINISH using sed:
It does not handle the nested case. For that you need to use a tool like awk or perl.
Thanks.. what does the (.*\) mean? Does that mean everything between START and ACTNUMBER?
Sometimes for ACTNUMBER 1234 it may have once instance of just a START and FINISH with the ACTNUMBER within and sometimes it may be embedded with in a nested START/FINISH
So this script basically needs to filter out all the jibberish before the START and after THE FINISH.
This log has many different ACTNUMBERS. Some maybe 1234 another may be 4778 , and so on and so on.
The sample data within the nested START/FINISH will be the ACTNUMBER.
Thanks so much for your help so far. I hope this helped a little.
---------- Post updated at 01:07 PM ---------- Previous update was at 01:04 PM ----------
and if its not nested the output should look like this
For this sample data (with START/START and FINISH/FINISH being on the same line), code that was already posted will work:
It will be more useful, if you tried that code on your real data and checked which parts are not processed correctly, then post example of those parts here.
My file (the output of an experiment) starts off looking like this,
_____________________________________________________________
Subjects incorporated to date: 001
Data file started on machine PKSHS260-05CP
**********************************************************************
Subject 1,... (9 Replies)
so im searching the process table with:
ps -ef | awk -F"./rello.java" '{ print substr($0, index($0,$2)) }'
I only want it to print everything that's infront of the "./rello.java". That's because im basically getting the arguments that was passed to the rello.java script.
this works.
... (2 Replies)
Hi,
I need to find all rows in 1st col of one file in another file (first occurrence) and replace the 1st col of first file with the grep result (the entire line). For example search AA from file 1 in file 2 and replace in file 1 by entire line found.
File1
AA BB CC DD
BB AA CC DDFile2
... (2 Replies)
Right now, my code is:
s/Secondary Ins./Secondary Ins.\
1/g
It's adding a 1 as soon as it finds Secondary Ins.
Primary Ins.: MEDICARE B DMERC Secondary Ins.
1: CONTINENTAL LIFE INS
What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Hi,
I have two variables x and y.
i need to find a particular string in a file, a workflow name and then insert the values of x and y into the next lines of the workflow name.
basically it is like as below
wf_xxxxxx
$$a=
$$b=
$$c= figo
$$d=bentley
i need to grep the 'wf_xxxx' and then... (6 Replies)
I know how to grep, copy and paste a string from a line. Now, what i want to do is to find a string and print a string from the line below it. To demonstrate:
Name 1: ABC Age: 3
Sex: Male
Name 2: DEF Age: 4
Sex: Male
Output:
3 Male
I know how to get "3". My biggest problem is to... (4 Replies)
I've seen several examples of grep showing the filename the string was found in, but what I really need is grep to show the file details in long format (like ls -l would).
scenario is:
grep mobile_number todays_files
This will show me the string I'm after & which files they turn up in, but... (2 Replies)
Hello all
Im performing find + grep operation that looks like this :
find . -name "*.dsp" | xargs grep -on Project.lib | grep -v ':0'
and I like to add to this one liner the possibility to replace the string " Project.lib" that found ( more then once in file ) with "Example.lib"
how can I do... (0 Replies)