For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2005 > modify strings in a file: L"foo" -> TEXT("foo")









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 modify strings in a file: L"foo" -> TEXT("foo")
Alexander Grau

2005-02-21, 3:57 pm

Hello-

I try to replace all occurences of L"foo" by TEXT("foo") in a file where
foo can be any text. This is what I thought should work:

#!/bin/bash

awk '{ gsub(/L"[^",]*"/, "TEXT(" substr("&", 2) ")" ); print }' testfile.txt

but it doesn't - it just shows TEXT() without "foo" in it...

what could be wrong here?

Thanks,
Akex

William James

2005-02-21, 3:57 pm


Alexander Grau wrote:
> Hello-
>
> I try to replace all occurences of L"foo" by TEXT("foo") in a file

where
> foo can be any text. This is what I thought should work:
>
> #!/bin/bash
>
> awk '{ gsub(/L"[^",]*"/, "TEXT(" substr("&", 2) ")" ); print }'

testfile.txt
>
> but it doesn't - it just shows TEXT() without "foo" in it...



*{ gsub(/L"[^",]*"/, "\1&\2" )
* gsub( /\1L/, "TEXT(" )
* gsub( /\2/, ")" )
*}
*1

William James

2005-02-21, 3:57 pm

*match($0,/L"[^",]*"/) {
* m = substr( $0, RSTART, RLENGTH )
* $0 = substr($0,1,RSTART-1) "TEXT(" substr(m,2) ")" \
* substr($0,RSTART+RLENGTH)
*}
*1

Alexander Grau

2005-02-21, 3:57 pm

William James wrote:

>
>
> Someday I'll get it right.
>
> *{ gsub(/L"[^",]*"/, "\1&)" )
> * gsub( /\1./, "TEXT(" )
> *}
> *1
>


So first you replace alle L"foo" by \1foo") where \1 is the ASCII
character 1 and after this all \1 characters by TEXT(" ??
This is clever :-)

William James

2005-02-21, 3:57 pm


Alexander Grau wrote:

> Now I try to add another conversion from L'x' to TCHAR('x') (where x


> any character):
>
> gsub(/L'.'/, "\2&)" )
> gsub( /\2./, "TCHAR(" )
>
> but that does not work... I tried to escape the inverted comma (')

and
> double inverted comma (''), but no success...very strange


Your code works for me:

*{ gsub(/L"[^",]*"/, "\1&)" )
* gsub( /\1./, "TEXT(" )
*
* gsub(/L'.'/, "\2&)" )
* gsub( /\2./, "TCHAR(" )
*}
*1

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com