Err,'common' string manipulations? 
Author Message
 Err,'common' string manipulations?

Hi all,

I've new to (g)awk, written two spiffy scripts so far. :)  I am
wondering if there's an easy way to handle reformatting of strings, much
like a 'trim(string,char)', 'trimleft(string,char)', and
'trimright(string,char)' set of operations which trim a given string of
the given char -- I'm sure you all know what I mean.

The most basic example I have is:

"date /T" | getline mydate

This is WinNT's 'date', which also spits out a trailing space.
Similarly, WinNT's "time /T" command spits out a leading space if we're
<10 in the hour -- I'm stuck having to deal with it.  I'm looking for,
well, I guess a set of string manipulation routines for common functions
that you find in many C/C++ stuffs (hey, even Foxpro had them :-).

Or, am I just stuck with the s/ / stuff all the time?

thx,
-te

--
Troy Engel
CME, ABT Corporation



Sun, 28 Oct 2001 03:00:00 GMT  
 Err,'common' string manipulations?


Quote:
>Hi all,

>I've new to (g)awk, written two spiffy scripts so far. :)  I am
>wondering if there's an easy way to handle reformatting of strings, much
>like a 'trim(string,char)', 'trimleft(string,char)', and
>'trimright(string,char)' set of operations which trim a given string of
>the given char -- I'm sure you all know what I mean.

>The most basic example I have is:

>"date /T" | getline mydate

>This is WinNT's 'date', which also spits out a trailing space.
>Similarly, WinNT's "time /T" command spits out a leading space if we're
><10 in the hour -- I'm stuck having to deal with it.  I'm looking for,
>well, I guess a set of string manipulation routines for common functions
>that you find in many C/C++ stuffs (hey, even Foxpro had them :-).

>Or, am I just stuck with the s/ / stuff all the time?

You could write your own functions, and put them in a file.

Then you could either copy that file into your script, or
use the -f option to include the "library" file like this:

awk -f your_awk_script -f your_library_file infile

See the manual pages of gawk for functions.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sun, 28 Oct 2001 03:00:00 GMT  
 Err,'common' string manipulations?

Quote:

> You could write your own functions, and put them in a file.

> Then you could either copy that file into your script, or
> use the -f option to include the "library" file like this:

> awk -f your_awk_script -f your_library_file infile

Gotcha -- I was wondering if there's a comprehensive archive of .awk
files out there; I've been able to find some cool stuff around the web
(helps to read it, learn new things) but something like CPAN is what I'm
hoping to find.  Maybe some FTP site?

I've also picked up the ORA sed&awk book; just trying not to reinvent
the wheel. :)

-te

--
Troy Engel
CME, ABT Corporation



Sun, 28 Oct 2001 03:00:00 GMT  
 Err,'common' string manipulations?


% Hi all,

% wondering if there's an easy way to handle reformatting of strings, much
% like a 'trim(string,char)', 'trimleft(string,char)', and
% 'trimright(string,char)' set of operations which trim a given string of
% the given char -- I'm sure you all know what I mean.

There's the sub() function, which takes a regular expression to replace:
 sub(/^ +/, "", mytime)

I guess you could create trim functions for this if you found it easier:

 function trimleft(string, char)
 {
   sub("^" char "+", "", string)
   return string
 }

 function trimright(string, char)
 {
   sub( char "+$", "", string)
   return string
 }

 function trim(string, char)
 {
   gsub("^" char "+|" char "+$", "", string)
   return string
 }

 mytime = trim(mytime, " ")

This won't work for characters which are special within regular expressions.
--

Patrick TJ McPhee
East York  Canada



Mon, 29 Oct 2001 03:00:00 GMT  
 Err,'common' string manipulations?
Thanks. :)  Maybe we should start a CAAN project. :)

-te

Quote:



> % Hi all,

> % wondering if there's an easy way to handle reformatting of strings, much
> % like a 'trim(string,char)', 'trimleft(string,char)', and
> % 'trimright(string,char)' set of operations which trim a given string of
> % the given char -- I'm sure you all know what I mean.

> There's the sub() function, which takes a regular expression to replace:
>  sub(/^ +/, "", mytime)

> I guess you could create trim functions for this if you found it easier:

>  function trimleft(string, char)
>  {
>    sub("^" char "+", "", string)
>    return string
>  }

>  function trimright(string, char)
>  {
>    sub( char "+$", "", string)
>    return string
>  }

>  function trim(string, char)
>  {
>    gsub("^" char "+|" char "+$", "", string)
>    return string
>  }

>  mytime = trim(mytime, " ")

> This won't work for characters which are special within regular expressions.
> --

> Patrick TJ McPhee
> East York  Canada


--
Troy Engel
CME, ABT Corporation


Mon, 29 Oct 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Getting Ascii string from hex 'string'

2. STRING 'make'/'remake'

3. Date manipulation and Java 'interface' equivalents

4. COMMON isn't COMMON?

5. ADO Error Doesn't Set VBScript Err Object

6. redirecting compiler's std err to file under winNT

7. Common Lisp's for Sun 4's

8. g95 / Linux / 'shared global COMMON'

9. Error in 'string toupper' and 'string tolower'

10. TIP #188: Add 'string is wide' to the 'string is' Subcommand

11. unicode to 'normal' string conversion

12. Handling of 'national characters' in strings

 

 
Powered by phpBB® Forum Software