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

Replace CRLF in a quoted string

Status
Not open for further replies.

rio23

Programmer
Dec 19, 2011
4
DE
Hi,
I am looking for a AWK program to substitute a crlf in a string with a different character, i.e chr(8).

So the input is like
11 "" 60000 0 "B2009<crlf>15301" "ER" 08/10/09 300,83 "H" 10 0
and the result should be like
11 "" 60000 0 "B2009<char(8)>15301" "ER" 08/10/09 300,83 "H" 10 0

Is there a difference between UNIX and WINDOWS crlf detection?

Help is very appreciated!
Thanks!
 

Try:
Code:
tr "\013\010" "\008" <in_file.txt >out_file.txt
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Hi

Not a good idea. Given that set1 and set2 have different length, two things may happen depending on your [tt]tr[/tt] implementation :
[ul]
[li]Repeat set1's last character to fill up set2 to set1's length, just as Perl's [tt]y///[/tt] function does. So for #13#10 you will get #8#8. This is GNU [tt]tr[/tt]'s behavior.[/li]
[li]Throw error, just as Sed's [tt]y///[/tt] command does. I would expect old Unix [tt]tr[/tt]'s to behave like this.[/li]
[/ul]
Beside that the \NNN notation expects octal character codes, so \008 is invalid ( octal has 8 valid digits, between 0 and 7 ) and \013 and \010 are actually character #11 and #8 respectively.

Supposing the entire input is what the OP posted, I would do it by slurping it in at once :
Code:
awk -vRS='' '[teal]{[/teal][COLOR=chocolate]gsub[/color][teal]([/teal][fuchsia]/\r\n/[/fuchsia][teal],[/teal][green][i]"[/i][/green][lime][i]\b[/i][/lime][green][i]"[/i][/green][teal])[/teal][teal]}[/teal][purple]1[/purple]' in_file.txt > out_file.txt
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

Feherke.
 


Ooops...[noevil]
.

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
ups ... [sadeyes]
Sorry I missed the correct end of my data line and this is what the question turns to be a little bit more complicated
So this ...
11 "" 60000 0 "B2009<crlf>15301" "ER" 08/10/09 300,83 "H" 10 0<crlf>
should go to ...
11 "" 60000 0 "B2009<char(8)>15301" "ER" 08/10/09 300,83 "H" 10 0<crlf>

Sorry for the missing crlf!
 
Btw
I am working on a Windows 7 machine and I have to modify the command this way to work for me:

Call
gawk.exe -v RS="" -f <prog> <in> > <out>

Prog
{
BINMODE=3
gsub(/\n/,"\b")
}
1

I dont know why but it only works this way for me ... It took me hours to figure that out with the binmode option.
What I would still need is the logic to replace only the crlf inside a string declaration [ponder]

Help is still veeery appreciated.
Thanks!
 
Hi

Supposing the double quotes ( " ) are balanced :
Code:
awk -vRS='\r\n' -F'"' '[teal]{[/teal][COLOR=chocolate]printf[/color][green][i]"%s%s[/i][/green][lime][i]\n\n[/i][/lime][green][i]"[/i][/green][teal],[/teal][navy]$0[/navy][teal],([/teal]f[teal]+=[/teal][blue]NF[/blue][purple]-1[/purple][teal])%[/teal][purple]2[/purple][teal]?[/teal][green][i]"[/i][/green][lime][i]\b[/i][/lime][green][i]"[/i][/green][teal]:[/teal][green][i]"[/i][/green][lime][i]\r\n[/i][/lime][green][i]"[/i][/green][teal]}[/teal]' in_file.txt > out_file.txt
Code:
gawk -vRS='' '[teal]{[/teal]n[teal]=[/teal][COLOR=chocolate]patsplit[/color][teal]([/teal][navy]$0[/navy][teal],[/teal]f[teal],[/teal][fuchsia]/"[^"]*"/[/fuchsia][teal],[/teal]s[teal]);[/teal][b]for[/b][teal]([/teal]i[teal]=[/teal][purple]0[/purple][teal];[/teal]i[teal]<=[/teal]n[teal];[/teal]i[teal]++)[/teal][COLOR=chocolate]printf[/color][green][i]"%s%s"[/i][/green][teal],[/teal][COLOR=chocolate]gensub[/color][teal]([/teal][fuchsia]/\r\n/[/fuchsia][teal],[/teal][green][i]"[/i][/green][lime][i]\b[/i][/lime][green][i]"[/i][/green][teal],[/teal][green][i]"g"[/i][/green][teal],[/teal]f[teal][[/teal]i[teal]]),[/teal]s[teal][[/teal]i[teal]][/teal][teal]}[/teal]' in_file.txt > out_file.txt
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].


Feherke.
 
Thank you Feherke (!!) but none of the solutions is working. The crlf stays there like set in stone. Something strange is going on here, in my previous post I mentioned that I had to correct the binmode option under Windows to make it fly. I suspect thats the same reason here. crlf is neither detected nore corrected. grrrr

Thank you anyway!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top