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

sub function

Status
Not open for further replies.

hippo12

Technical User
Jan 25, 2007
4
US
Hi
I'm trying to

1. Replace a comma with a full stop. Tried using

gawk "sub(/,/,.)" 1.txt > 2.txt (failed)
gawk "sub(/,/,test)" 1.txt > 2.txt (it only delete away the comma but it doesn't replace it with test)

When I put numbers as replacement text, it works though.
Can anyone assist?

2. Need advice on converting elasped time to seconds.

Input is "2:13:05.687" need to convert to equivalent seconds.

Appreciate any help

Cheers
 
1.
gawk '{sub(/,/,".")}' 1.txt > 2.txt

2.
What have you tried so far ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV
1.
- gawk '{sub(/,/,".")}' 1.txt > 2.txt
gawk: cmd. line:1: ^ invalid char ''' in expression

Somehow my system doesnt allow me to use '.

- gawk '{sub(/,/,".")}' 1.txt > 2.txt
gawk: cmd. line:1:^ syntax error
gawk: cmd. line:1: fatal: 0 is invalid as number of arguments for sub

2. I didnt really tried much as I was still working on problem 1. I suspect there is some script/formula for this but can't find it in the web. Appreciate if anyone has can teach me as I'm a beginner in awk.

Cheers
 
Hi

hippo12 said:
Somehow my system doesnt allow me to use '.
Can you tell us the name and maybe version of your operating system, shell and [tt]awk[/tt] ?

PHV's code must work in typical Unix/Linux/CygWin environments. Although, it will not produce output.
Code:
gawk '{sub(/,/,".")}[red]1[/red]' 1.txt > 2.txt
That will replace only the first comma ( , ). If you want to replace all occurrences, ise [tt]gsub()[/tt].
Code:
gawk '{[red]gsub[/red](/,/,".")}1' 1.txt > 2.txt


Feherke.
 
Silly me...i work on Win XP, MSDOS cmd shell with Gawk3.1.3

Tried your hint to work on cygwin and it works!(with your command: gawk '{sub(/,/,".")}1' 1.txt > 2.txt) What puzzle me is why is there a need for the extra 1 after }?

Will work on the other problem.

Thanks to both of you!
 
Hi

The [tt]awk[/tt] script are built from blocks of [red]pattern[/red] and [blue]action[/blue] pairs, for example :
Code:
[red]i<10[/red][blue]{i++}[/blue]
But they are not always paors, because both the pattern and the action can be omitted. If not present, the default is assumed. The default pattern is true, for matching all records, the default action is [tt]print[/tt], for printing the entire record. Of course, can not be omitted both. While from the following codes with similar effect the first is the shorter, I suggested that.
Code:
gawk '{gsub(/,/,".")}1' 1.txt > 2.txt

[gray]# or[/gray]

gawk '{gsub(/,/,".")}{print}' 1.txt > 2.txt

[gray]# or[/gray]

gawk '{gsub(/,/,".")}1{print}' 1.txt > 2.txt
Of course, you can write the statements in the same action block, as they have to be executed together.
Code:
gawk '{gsub(/,/,".");print}' 1.txt > 2.txt

[gray]# or in GNU awk only way[/gray]

gawk '{print gensub(/,/,".","g")}'1.txt > 2.txt

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top