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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract Lines from a file

Status
Not open for further replies.

nrastogi

Programmer
Jul 19, 2002
52
US
Hello guys,

New to unix scripting here.

Question 1:

I have this text (log) file that has lines that contains ** (two asterisk) in few lines. I would like to read the file and spit out only those lines that contains ** to a file with the line break.

Question 2:
Would like to do the same but this file has a large amount of data.
I would want to pass a Today's date (or any date) as an argument and find the first line that begins with that date , and then get all the lines that contains ** for that date.

The date format in log file is yy/mm/dd and the line starts like this [03/12/02@


Please let me know the solution to both of these scenarios. Your help is really appreciated.

Thanks a lot in advance,

NRastogi

 
Try something like this:
1) sed is your friend:
Code:
sed -n -e '/\*\*/p' /path/to/input >output
2) awk is your friend:
Code:
d="03/12/02"
awk -F@ '
$1~"^\[[0-9][0-9]/[0-9][0-9]/[0-9][0-9]"{f=0}
$1=="['$d'"{++f}
{if(f){print)}
' /path/to/input >output

Hope This Help
PH.
 
Thanks PH so very much.

#1 works like a charm.

I tried #2. Created a new x.sh and copy your snippet in there. Ran it from command line using . x.sh

got following errors:

awk: cmd. line:4: {if(f){print)}
awk: cmd. line:4: ^ parse error
awk: cmd. line:5: (END OF FILE)
awk: cmd. line:5: parse error

Does this looking for ** as well

Please help.
Thanks NR
 
Hi PH,

Tried to debug the errors and got success.

Here is what I tried.

d="03/12/01"
awk -F@ '
$1~"^\[[0-9][0-9]/[0-9][0-9]/[0-9][0-9]"{f=0}
$1=="['$d'" {++f}
{if(f) print}
' inputfile > esl.out

but, like I said, this one gives all the lines from 03/12/01. I would like to get the ones that has ** in it.

Also, in another script, how could I pass a date range to extract data. like from 03/12/01 to 03/12/02 or even 03/12/01 to 03/12/01.

I would like to do it as a separate script.

Thanks much in advance.

Regards, NR




 
>I would like to get the ones that has ** in it.
Code:
d="03/12/01"
awk -F@ '
$1~"^\[[0-9][0-9]/[0-9][0-9]/[0-9][0-9]"{f=0}
$1=="['$d'" {++f}
/\*\*/{if(f) print}
' inputfile > esl.out
>from 03/12/01 to 03/12/02.
d1="03/12/01";d2="03/12/02"
awk -F@ '
$1~"^\[[0-9][0-9]/[0-9][0-9]/[0-9][0-9]"{f=0}
$1>=&quot;['$d1'&quot; && $1<=&quot;['$d2'&quot;{++f}
/\*\*/{if(f) print}
' inputfile > esl.out[/code]

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top