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

awk with sed? 1

Status
Not open for further replies.

BIS

Technical User
Jun 1, 2001
1,893
NL
Suppose I have this line

Subject: Don't play with fire

and I want to remove the ' character (our database doesn't like it), could this be done by somehow combining

sed 's/'//g'

with awk /^Subject/ {$0=" can I put sed here? "}

or is there an easier way?
 
Hi

Sure is possible. At least with GNU Awk. However it is abit lengthy because the two way communication :
Code:
[teal]{[/teal]
    [b]print[/b] [i][green]"BEFORE"[/green][/i][teal],[/teal] [navy]$0[/navy]

    p [teal]=[/teal] [i][green]"sed [/green][/i][lime]\"[/lime][i][green]s/'//[/green][/i][lime]\"[/lime][i][green]"[/green][/i]

    [b]print[/b] [teal]|&[/teal] p
    [b]close[/b][teal]([/teal]p[teal],[/teal] [i][green]"to"[/green][/i][teal])[/teal]

    p [teal]|&[/teal] [b]getline[/b]
    [b]close[/b][teal]([/teal]p[teal],[/teal] [i][green]"from"[/green][/i][teal])[/teal]

    [b]print[/b] [i][green]"AFTER "[/green][/i][teal],[/teal] [navy]$0[/navy]
[teal]}[/teal]
Code:
[blue]master #[/blue] gawk -f ask-sed-to-remove-single-quote.awk <<< "Subject: Don't play with fire"
BEFORE Subject: Don't play with fire
AFTER  Subject: Dont play with fire

Or with any standard compliant Awk :
Code:
[b]gsub[/b][teal]([/teal][fuchsia]/'/[/fuchsia][teal],[/teal] [i][green]""[/green][/i][teal])[/teal]

But I strongly recommend to never do that - never alter the data just to satisfy the storage or transport layer.

What kind of database is that ? For SQL databases ( and most of the NoSQL databases too ) you must escape text data. Escaping may depend on the database type and internal encoding and beside the string delimiters may require the escaping of the escape character too. For this reason, escaping solutions are provided for most of the databases/drivers/libraries.

So better give use details on your database and the intended use.


Feherke.
feherke.ga
 
Sorry for the late reply...
The gsub was all I needed - many thanks for pointing me in the right direction.

I agree with you that this is not the proper way to go about the issue, but it is a quick and dirty solution that will work until they get their database issues resolved :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top