1752503 Members
5203 Online
108788 Solutions
New Discussion юеВ

Re: AWK output on HP-UX

 
Anthony LAUNAY
New Member

AWK output on HP-UX

Hello,

I have a problem with AWK on HP-UX. I have a script which asks for a specific date and then shows everything related to this date :

[code]
#!/bin/ksh
awk 'BEGIN{printf("Entrez la date : "); getline date < "-"}
$0 ~ date {f=1;print;next}
/^[0-9][0-9]\//{f=0}
f' Planning.csv > Planning.txt
[/code]

The problem is that with this script all the input file (Planning.csv) is redirected to the output file (Planning.txt) but not the specific data for the date entered.

Would you know why ?

Thanks for your help
3 REPLIES 3
Dennis Handly
Acclaimed Contributor

Re: AWK output on HP-UX

>BEGIN{printf("Entrez la date : "); getline date < "-"}

I would never do this prompt in awk. This is best left to the shell and then have the shell pass it to awk:
echo "Entrez la date: \c"
read DATE
awk -v date="$DATE" ...

>f' Planning.csv > Planning.txt

It would be clearer if you had an explicit print:
f { print }' Planning.csv > Planning.txt

>not the specific data for the date entered.

Add a print at the top to trace what's happening in your script.
James R. Ferguson
Acclaimed Contributor

Re: AWK output on HP-UX

Hi:

Why not use a simple 'grep' ?

# grep 11/26 Planning.csv > Planning.txt

If you only want to match at the beginning of a line:

# grep ^11/26 Planning.csv > Planning.txt

And if you want to skip lines that begin with a 2-digits perhaps to isolate lines with the date you want somewhere in the middle of the line:

# grep -v ^[0-9][0-9] Planning.csv | grep 11/26 > Planning.txt

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: AWK output on HP-UX


>> all the input file (Planning.csv) is redirected to the output file

Truly all, or perhaps everything after the first matching date?

Looks like the script sets a flag on date match anywhere in the line.
It clears that flag when it sees a line which start with number-number-slash
If the flag is set, then the line is printed.

What does the exact input look like?
Care to provide a sample?

1) depending on the input you can drop the ';print;next' as the line will be printed by the final 'f'.
2) looks like the flag-off condition is never triggered. Check and/or show your match logic.

hth,
Hein