Home > Archive > AWK > June 2007 > Get field on previous line
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Get field on previous line
|
|
| Atropo 2007-06-27, 9:57 pm |
| Hi all.
I have not been able to figure out how to get a field in the previous
line. if there is a match.
what I want is : if I search for the string "status date is greater
than or equal to the" I would like to output to a file "date.out"
with (555-596-7021) and (555-221-3054).
and if the pattern is "inactive subscription" output to file
"inactive.out" with (555-138-0423).
...
[1513-1530]: [1530-1547]: [1547-1564]: [1564-1581]: [1581-1598]:
[1598-1615]: [1615-1632]: [1632-1649]: [1649-1666]: [1666-1683]:
[1683-1700]: [1700-1717]: [1717-1734]: [1734-1751]: [1751-1768]:
[1768-1785]: [1785-1802]: [1802-1819]: [1819-1836]: [1836-1853]:
[1853-1870]: [1870-1887]: [1887-1904]: [1904-1921]: [1921-1938]:
[1938-1955]: [1955-1972]: [1972-1989]: Process BVT order request for:
(555-596-7021)
Exception from createAnOrder: Exception: STN 555-199-0166 status date
is greater than or equal to the BTN order effective date, Aborting the
"O" order creation!
[0-17]: [17-34]: [34-50]: Process BVT order request for:
(555-572-3220)
Exception from createAnOrder: Exception Out order 220964/6005 already
exists for the TN(s) in this order - order not created.
[0-17]: Process BVT order request for: (555-138-0423)
Exception from createAnOrder: Order request for inactive subscription
(555-138-0423) received. No order being created.
[17-34]: Process BVT order request for: (555-221-3054)
Exception from createAnOrder: Exception: Subscription status date is
greater than or equal to the order effective date, Abort the "O" order
creation!
...
any help will be highly appreciated
TIA.
| |
| Janis Papanagnou 2007-06-27, 9:57 pm |
| Atropo wrote:
> Hi all.
>
> I have not been able to figure out how to get a field in the previous
> line. if there is a match.
>
> what I want is : if I search for the string "status date is greater
> than or equal to the" I would like to output to a file "date.out"
> with (555-596-7021) and (555-221-3054).
>
> and if the pattern is "inactive subscription" output to file
> "inactive.out" with (555-138-0423).
>
> ..
> [1513-1530]: [1530-1547]: [1547-1564]: [1564-1581]: [1581-1598]:
> [1598-1615]: [1615-1632]: [1632-1649]: [1649-1666]: [1666-1683]:
> [1683-1700]: [1700-1717]: [1717-1734]: [1734-1751]: [1751-1768]:
> [1768-1785]: [1785-1802]: [1802-1819]: [1819-1836]: [1836-1853]:
> [1853-1870]: [1870-1887]: [1887-1904]: [1904-1921]: [1921-1938]:
> [1938-1955]: [1955-1972]: [1972-1989]: Process BVT order request for:
> (555-596-7021)
> Exception from createAnOrder: Exception: STN 555-199-0166 status date
> is greater than or equal to the BTN order effective date, Aborting the
> "O" order creation!
> [0-17]: [17-34]: [34-50]: Process BVT order request for:
> (555-572-3220)
> Exception from createAnOrder: Exception Out order 220964/6005 already
> exists for the TN(s) in this order - order not created.
> [0-17]: Process BVT order request for: (555-138-0423)
> Exception from createAnOrder: Order request for inactive subscription
> (555-138-0423) received. No order being created.
> [17-34]: Process BVT order request for: (555-221-3054)
> Exception from createAnOrder: Exception: Subscription status date is
> greater than or equal to the order effective date, Abort the "O" order
> creation!
> ..
> any help will be highly appreciated
>
> TIA.
>
The main logic to get the last field from the previous line is...
awk '
/pattern/ { print last >"your_outfile" }
{ last = $NF }
' your_infile
Add as many conditions as you want...
awk '
/inactive subscription/ { print last >"inactive.out" }
/status date is .../ { print last >"date.out" }
{ last = $NF }
'
Janis
| |
| Benjamin Esham 2007-06-27, 9:57 pm |
| * Atropo:
> I have not been able to figure out how to get a field in the previous
> line. if there is a match.
>
> what I want is : if I search for the string "status date is greater than
> or equal to the" I would like to output to a file "date.out" with
> (555-596-7021) and (555-221-3054).
>
> and if the pattern is "inactive subscription" output to file
> "inactive.out" with (555-138-0423).
Maybe something along these lines:
/[0-9]{3}-[0-9]{3}-[0-9]{4}/ { previous = substr($0, match($0, /[0-9]{3}-[0-9]{3}-[0-9]{4}/)) }
/status data is greater than/ { print previous > "date.out" }
/inactive subscription/ { print previous > "inactive.out" }
I'm guessing there's a much simpler way to do what I did on the first line,
but this should get you started, at least.
HTH,
--
Benjamin D. Esham
bdesham@gmail.com | AIM: bdesham128 | Jabber: same as e-mail
"But the [ocean] Goddess, as quite a few New Agers fail to grok,
is not just the comforting image of "Mother Nature"... sometimes
she is an earthquake, tornado, and hurricane-throwing /XXXXX/."
— localroger on kuro5hin
| |
| Atropo 2007-06-27, 9:57 pm |
|
Benjamin Esham ha escrito:
> * Atropo:
>
>
> Maybe something along these lines:
>
> /[0-9]{3}-[0-9]{3}-[0-9]{4}/ { previous = substr($0, match($0, /[0-9]{3}-[0-9]{3}-[0-9]{4}/)) }
>
> /status data is greater than/ { print previous > "date.out" }
>
> /inactive subscription/ { print previous > "inactive.out" }
>
> I'm guessing there's a much simpler way to do what I did on the first line,
> but this should get you started, at least.
>
> HTH,
Thanks a lot Benjamin, I'll give a try to your suggestion and
vassilis's one. but as a newbie I don't understand so much about
regular expressions. would you clarify me the logic behind the
/[0-9]{3}-[0-9]{3}-[0-9]{4}/ { previous = substr($0, match($0, /[0-9]
{3}-[0-9]{3}-[0-9]{4}/)) }
| |
| Atropo 2007-06-27, 9:57 pm |
|
Janis Papanagnou ha escrito:
> Atropo wrote:
>
> The main logic to get the last field from the previous line is...
>
> awk '
> /pattern/ { print last >"your_outfile" }
> { last = $NF }
> ' your_infile
>
> Add as many conditions as you want...
>
> awk '
> /inactive subscription/ { print last >"inactive.out" }
> /status date is .../ { print last >"date.out" }
> { last = $NF }
> '
>
>
> Janis
I've just tried your script and it works great...
| |
| Atropo 2007-06-27, 9:57 pm |
|
Janis Papanagnou ha escrito:
> Atropo wrote:
>
> The main logic to get the last field from the previous line is...
>
> awk '
> /pattern/ { print last >"your_outfile" }
> { last = $NF }
> ' your_infile
>
> Add as many conditions as you want...
>
> awk '
> /inactive subscription/ { print last >"inactive.out" }
> /status date is .../ { print last >"date.out" }
> { last = $NF }
> '
>
>
> Janis
One last thing janis.. i have to process hundreds of files. with
this command
find . -name "OUT-2007*" -exec awk -f busca.awk {} \;
but it will overwrite the new created files.. could I use the ">>"
to append instead ??
| |
| Benjamin Esham 2007-06-27, 9:57 pm |
| * Atropo:
> Thanks a lot Benjamin, I'll give a try to your suggestion and
> vassilis's one. but as a newbie I don't understand so much about
> regular expressions. would you clarify me the logic behind the
>
> /[0-9]{3}-[0-9]{3}-[0-9]{4}/
I'm happy to go through this expression, though you should probably read the
(g)awk manual, and especially [1]. I forgot to mention before that this a
gawk solution, and may not necessarily work in other kinds of awk.
The slashes indicate that this is a regular expression. We use [0-9] to
mean that any digit is matched; the {3} indicates that three such digits
must be matched. The dash is just part of the phone number. You can see
that this pattern matches a string of three digits, a dash, another three
digits, another dash, and four digits.
> { previous = substr($0, match($0, /[0-9]{3}-[0-9]{3}-[0-9]{4}/)) }
This is the awk command carried out for any lines that match the regular
expression above—i.e. any lines that contain phone numbers. The match()
command returns the position of the phone number within the line; the
substr() call then gets the relevant portion of the line, so that this
command sets the value of "previous" to the phone number on this line.
After rereading this code, I realize that the call needs to be
previous = substr($0, match($0, /[0-9]{3}-[0-9]{3}-[0-9]{4}/), 12)
Adding the "12" at the end makes sure that only the phone number itself will
be stored into the variable previous.
I hope this explanation helps! Good luck with your project.
[1] http://www.gnu.org/software/gawk/ma...exp.html#Regexp
Cheers,
--
Benjamin D. Esham
bdesham@gmail.com | AIM: bdesham128 | Jabber: same as e-mail
"Men never do evil so completely and cheerfully as when they
do it from religious conviction." — Blaise Pascal
| |
| Atropo 2007-06-27, 9:57 pm |
|
Benjamin Esham ha escrito:
> * Atropo:
>
>
> I'm happy to go through this expression, though you should probably read the
> (g)awk manual, and especially [1]. I forgot to mention before that this a
> gawk solution, and may not necessarily work in other kinds of awk.
>
> The slashes indicate that this is a regular expression. We use [0-9] to
> mean that any digit is matched; the {3} indicates that three such digits
> must be matched. The dash is just part of the phone number. You can see
> that this pattern matches a string of three digits, a dash, another three
> digits, another dash, and four digits.
>
>
> This is the awk command carried out for any lines that match the regular
> expression above-i.e. any lines that contain phone numbers. The match()
> command returns the position of the phone number within the line; the
> substr() call then gets the relevant portion of the line, so that this
> command sets the value of "previous" to the phone number on this line.
> After rereading this code, I realize that the call needs to be
>
> previous = substr($0, match($0, /[0-9]{3}-[0-9]{3}-[0-9]{4}/), 12)
>
> Adding the "12" at the end makes sure that only the phone number itself will
> be stored into the variable previous.
>
> I hope this explanation helps! Good luck with your project.
>
> [1] http://www.gnu.org/software/gawk/ma...exp.html#Regexp
>
> Cheers,
> --
> Benjamin D. Esham
> bdesham@gmail.com | AIM: bdesham128 | Jabber: same as e-mail
> "Men never do evil so completely and cheerfully as when they
> do it from religious conviction." - Blaise Pascal
Excellent Benjamin.. it really helps. i'll give a try with no
much hope, 'coz I'm on an old AIX. it has nawk but I don't know which
version.
| |
| Janis Papanagnou 2007-06-27, 9:57 pm |
| Atropo wrote:
> Janis Papanagnou ha escrito:
>
>
>
> One last thing janis.. i have to process hundreds of files. with
> this command
> find . -name "OUT-2007*" -exec awk -f busca.awk {} \;
>
> but it will overwrite the new created files.. could I use the ">>"
> to append instead ??
>
Yes, that's possible, exactly as you expected it to work...
{ print last >>"date.out" }
etc.
Janis
| |
| Atropo 2007-06-27, 9:57 pm |
|
Janis Papanagnou ha escrito:
> Atropo wrote:
>
> Yes, that's possible, exactly as you expected it to work...
>
> { print last >>"date.out" }
>
> etc.
>
> Janis
A new element has arise while tweaking this..
as i said before i have several files from the same day and i want to
process only for today
TodayDate=`date +"%Y%m%d"`
find . -name "OUT-${TodayDate}*.out" -exec awk -f busca.awk -v
varfecha=$TodayDate {} \;
where busca.awk has basically
/pattern/ {print "myfile" varfecha ".dat"
'coz I want to create myfile20070627.dat but it creates myfile.dat
so i guess the awk var "varfecha" has no value or what else ??
|
|
|
|
|