Home > Archive > AWK > January 2008 > remove extra numeric field
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 |
remove extra numeric field
|
|
| wong_powah@yahoo.ca 2008-01-05, 6:58 pm |
| How to remove the extra numeric field in a text file?
e.g. for the following text file, I want to remove $2,299.11 from the
first line, $2,292.86 from the second line, $2,170.08 from the fourth
line and $2,286.08 from the last line.
Oct. 01, 2007 CITY OF
VANCOUVER $456.48 $2,299.11
Property taxes
Oct. 02, 2007 TD GEN INS
$66.25 $2,292.86 Car insurance
Oct. 09, 2007 BELAIR HABITAT. BELAIR INS/ASS.
$40.95 Home insurance
Oct. 09, 2007 ENBRIDGE ENBRIDGE
$45.91 $2,170.08 Home heating
Nov. 05, 2007 CANADIAN TIRE #
$22.74 Home general maintenance
Dec. 10, 2007 CANADIAN TIRE #
$44.21 Home general maintenance
Dec. 19, 2007 CANADIAN TIRE #
$71.48 $2,286.08 Home general maintenance
| |
| Ed Morton 2008-01-05, 6:58 pm |
| On 1/5/2008 9:21 AM, wong_powah@yahoo.ca wrote:
> How to remove the extra numeric field in a text file?
> e.g. for the following text file, I want to remove $2,299.11 from the
> first line, $2,292.86 from the second line, $2,170.08 from the fourth
> line and $2,286.08 from the last line.
>
> Oct. 01, 2007 CITY OF
> VANCOUVER $456.48 $2,299.11
> Property taxes
> Oct. 02, 2007 TD GEN INS
> $66.25 $2,292.86 Car insurance
> Oct. 09, 2007 BELAIR HABITAT. BELAIR INS/ASS.
> $40.95 Home insurance
> Oct. 09, 2007 ENBRIDGE ENBRIDGE
> $45.91 $2,170.08 Home heating
> Nov. 05, 2007 CANADIAN TIRE #
> $22.74 Home general maintenance
> Dec. 10, 2007 CANADIAN TIRE #
> $44.21 Home general maintenance
> Dec. 19, 2007 CANADIAN TIRE #
> $71.48 $2,286.08 Home general maintenance
>
>
Were the above lines wrapped by your posting (i.e. does each line really start
with a date)? Are all of the spaces above the same or are some of them tabs to
separate fields?
Ed.
| |
| Janis Papanagnou 2008-01-05, 6:58 pm |
| wong_powah@yahoo.ca wrote:
> How to remove the extra numeric field in a text file?
> e.g. for the following text file, I want to remove $2,299.11 from the
> first line, $2,292.86 from the second line, $2,170.08 from the fourth
> line and $2,286.08 from the last line.
Your description and line numbers do not match your data below.
>
> Oct. 01, 2007 CITY OF
> VANCOUVER $456.48 $2,299.11
> Property taxes
> Oct. 02, 2007 TD GEN INS
> $66.25 $2,292.86 Car insurance
> Oct. 09, 2007 BELAIR HABITAT. BELAIR INS/ASS.
> $40.95 Home insurance
> Oct. 09, 2007 ENBRIDGE ENBRIDGE
> $45.91 $2,170.08 Home heating
> Nov. 05, 2007 CANADIAN TIRE #
> $22.74 Home general maintenance
> Dec. 10, 2007 CANADIAN TIRE #
> $44.21 Home general maintenance
> Dec. 19, 2007 CANADIAN TIRE #
> $71.48 $2,286.08 Home general maintenance
>
>
It seems that you want to remove an optionally second "price" value?
The following might do...
awk '{ i=index($0,"$") ; match(substr($0,i+1),/\$[0-9.,]*/)
print substr($0,1,i+RSTART-1) substr($0,i+RSTART+RLENGTH+1) }
'
Janis
| |
| loki harfagr 2008-01-05, 6:58 pm |
| On Sat, 05 Jan 2008 07:21:38 -0800, wong_powah wrote:
> How to remove the extra numeric field in a text file? e.g. for the
> following text file, I want to remove $2,299.11 from the first line,
> $2,292.86 from the second line, $2,170.08 from the fourth line and
> $2,286.08 from the last line.
>
> Oct. 01, 2007 CITY OF
> VANCOUVER $456.48 $2,299.11
> Property taxes
> Oct. 02, 2007 TD GEN INS
> $66.25 $2,292.86 Car insurance
> Oct. 09, 2007 BELAIR HABITAT. BELAIR INS/ASS. $40.95 Home
> insurance
> Oct. 09, 2007 ENBRIDGE ENBRIDGE $45.91 $2,170.08 Home
> heating
> Nov. 05, 2007 CANADIAN TIRE #
> $22.74 Home general maintenance
> Dec. 10, 2007 CANADIAN TIRE #
> $44.21 Home general maintenance
> Dec. 19, 2007 CANADIAN TIRE #
> $71.48 $2,286.08 Home general maintenance
Supposing the given conditions are the only problem,
this would do what I understood you wanted to say ;-)
$ gawk '!/\$.*\$/; /\$.*\$/{sub(/^.*[-+,\.0-9]/,"",$3);print}' FS=$
(I put gawk as I'm in a global major refitting of my machine
and I can't check on other systems/installations/wot at the moment :-)
| |
| Ed Morton 2008-01-08, 6:59 pm |
| Ed Morton wrote:
> On 1/5/2008 9:21 AM, wong_powah@yahoo.ca wrote:
>
>
>
> Were the above lines wrapped by your posting (i.e. does each line really start
> with a date)? Are all of the spaces above the same or are some of them tabs to
> separate fields?
In the absence of any more information that could lead to a more
concises solution, try this:
$ cat file
Oct. 01, 2007 CITY OF VANCOUVER $456.48 $2,299.11 Property taxes
Oct. 02, 2007 TD GEN INS $66.25 $2,292.86 Car insurance
Oct. 09, 2007 BELAIR HABITAT. BELAIR INS/ASS. $40.95 Home insurance
Oct. 09, 2007 ENBRIDGE ENBRIDGE $45.91 $2,170.08 Home heating
Nov. 05, 2007 CANADIAN TIRE # $22.74 Home general maintenance
Dec. 10, 2007 CANADIAN TIRE # $44.21 Home general maintenance
Dec. 19, 2007 CANADIAN TIRE # $71.48 $2,286.08 Home general maintenance
$ awk '{sub(/\$/,"$$");sub(/[ \t]+\$[0-9,.]+/,"");sub(/\$/,"")}1' file
Oct. 01, 2007 CITY OF VANCOUVER $456.48 Property taxes
Oct. 02, 2007 TD GEN INS $66.25 Car insurance
Oct. 09, 2007 BELAIR HABITAT. BELAIR INS/ASS. $40.95 Home insurance
Oct. 09, 2007 ENBRIDGE ENBRIDGE $45.91 Home heating
Nov. 05, 2007 CANADIAN TIRE # $22.74 Home general maintenance
Dec. 10, 2007 CANADIAN TIRE # $44.21 Home general maintenance
Dec. 19, 2007 CANADIAN TIRE # $71.48 Home general maintenance
Regards,
Ed.
| |
| Ed Morton 2008-01-08, 6:59 pm |
| loki harfagr wrote:
> On Sat, 05 Jan 2008 07:21:38 -0800, wong_powah wrote:
>
>
>
>
> Supposing the given conditions are the only problem,
> this would do what I understood you wanted to say ;-)
>
> $ gawk '!/\$.*\$/; /\$.*\$/{sub(/^.*[-+,\.0-9]/,"",$3);print}' FS=$
>
> (I put gawk as I'm in a global major refitting of my machine
> and I can't check on other systems/installations/wot at the moment :-)
>
Doesn't work for me as it deletes the "$" from the first value and adds
white space on the lines being modified:
$ cat file
Oct. 01, 2007 CITY OF VANCOUVER $456.48 $2,299.11 Property taxes
Oct. 02, 2007 TD GEN INS $66.25 $2,292.86 Car insurance
Oct. 09, 2007 BELAIR HABITAT. BELAIR INS/ASS. $40.95 Home insurance
Oct. 09, 2007 ENBRIDGE ENBRIDGE $45.91 $2,170.08 Home heating
Nov. 05, 2007 CANADIAN TIRE # $22.74 Home general maintenance
Dec. 10, 2007 CANADIAN TIRE # $44.21 Home general maintenance
Dec. 19, 2007 CANADIAN TIRE # $71.48 $2,286.08 Home general maintenance
$ gawk '!/\$.*\$/; /\$.*\$/{sub(/^.*[-+,\.0-9]/,"",$3);print}' FS=$ file
Oct. 01, 2007 CITY OF VANCOUVER 456.48 Property taxes
Oct. 02, 2007 TD GEN INS 66.25 Car insurance
Oct. 09, 2007 BELAIR HABITAT. BELAIR INS/ASS. $40.95 Home insurance
Oct. 09, 2007 ENBRIDGE ENBRIDGE 45.91 Home heating
Nov. 05, 2007 CANADIAN TIRE # $22.74 Home general maintenance
Dec. 10, 2007 CANADIAN TIRE # $44.21 Home general maintenance
Dec. 19, 2007 CANADIAN TIRE # 71.48 Home general maintenance
Regards,
Ed.
| |
| wong_powah@yahoo.ca 2008-01-08, 10:01 pm |
| On Jan 5, 11:01 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 1/5/2008 9:21 AM, wong_po...@yahoo.ca wrote:
>
>
>
>
>
> Were the above lines wrapped by your posting (i.e. does each line really start
> with a date)? Are all of the spaces above the same or are some of them tabs to
> separate fields?
>
> Ed.
The above lines were wrapped by my posting (i.e. each line really
start with a date).
There are tabs and spaces.
Thank you all for your solutions!
| |
| Ed Morton 2008-01-09, 3:58 am |
| wong_powah@yahoo.ca wrote:
> On Jan 5, 11:01 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
> The above lines were wrapped by my posting (i.e. each line really
> start with a date).
> There are tabs and spaces.
> Thank you all for your solutions!
If there are tabs in specific locations (i.e. between fields) there
could be a much simpler solution.
Ed.
|
|
|
|
|