You are not logged in.

#1 2013-04-13 06:37:55

Aegidius
Member
From: Italy
Registered: 2011-06-29
Posts: 288
Website

String replace in big files

Hello smile

I have a big text file and I have to replace the tab space with the ; character. I cannot do it with gedit cause it crashes.

How can I do?

Thank you smile

Egidio

PS: this is the file in question.

Last edited by Aegidius (2013-04-13 06:38:39)

Offline

#2 2013-04-13 07:05:42

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: String replace in big files

Use sed.

Offline

#3 2013-04-13 07:19:01

Aegidius
Member
From: Italy
Registered: 2011-06-29
Posts: 288
Website

Re: String replace in big files

I already tried with sed but I can't replace the tab space.

Offline

#4 2013-04-13 07:22:16

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: String replace in big files


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2013-04-13 09:25:01

Aegidius
Member
From: Italy
Registered: 2011-06-29
Posts: 288
Website

Re: String replace in big files

Thank you smile

I solved all the problems except for a thing.

I'm trying to use the PHP fgetcsv function. The new input csv has ; for delimiter character and \ for escape character.
When I call the function with

fgetcsv($inputFile, 0, ';', '"', '\\')

the script divides this line

978881159779;AL MERCATO CON JOANNE HARRIS. NUOVE RICETTE DALLA CUCINA DI ÇCHOCOLATÈ;F;GARZANTI LIBRI;;EDIZIONI SPECIALI;0;29,5;HARRIS JOANNE\; WARDE FRAN;00/00/00;;08/11/2007;;BA;;0;00/00/00;0;0;0;0;;n;9;MESSAGGERIE;;0;0;00/00/00;00/00/00;15/11/2010;00/00/00;0;;00/00/00;00/00/00;0;250;;1;960;200;252;20;0;C;;454690;Trd.GRANDI L.\13Fmt.8\13Ill.ILL., RIL.\13Dis.C\13;E;;

into this array

Array
(
    [0] => 978881159779
    [1] => AL MERCATO CON JOANNE HARRIS. NUOVE RICETTE DALLA CUCINA DI ÇCHOCOLATÈ
    [2] => F
    [3] => GARZANTI LIBRI
    [4] => 
    [5] => EDIZIONI SPECIALI
    [6] => 0
    [7] => 29,5
    [8] => HARRIS JOANNE\
    [9] =>  WARDE FRAN
    [10] => 00/00/00
    [11] => 
    [12] => 08/11/2007
    [13] => 
    [14] => BA
    [15] => 
    [16] => 0
    [17] => 00/00/00
    [18] => 0
    [19] => 0
    [20] => 0
    [21] => 0
    [22] => 
    [23] => n
    [24] => 9
    [25] => MESSAGGERIE
    [26] => 
    [27] => 0
    [28] => 0
    [29] => 00/00/00
    [30] => 00/00/00
    [31] => 15/11/2010
    [32] => 00/00/00
    [33] => 0
    [34] => 
    [35] => 00/00/00
    [36] => 00/00/00
    [37] => 0
    [38] => 250
    [39] => 
    [40] => 1
    [41] => 960
    [42] => 200
    [43] => 252
    [44] => 20
    [45] => 0
    [46] => C
    [47] => 
    [48] => 454690
    [49] => Trd.GRANDI L.\13Fmt.8\13Ill.ILL., RIL.\13Dis.C\13
    [50] => E
    [51] => 
    [52] => 
)

So it considers the ; in \; as a separator and does not skip it. Is that a PHP bug?

Thank you for the help smile

Egidio

Last edited by Aegidius (2013-04-13 09:29:34)

Offline

#6 2013-04-13 11:05:42

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: String replace in big files

Why can't you use sed?  This is exactly the type of task it is for.  tr might be an option to.

sed -i 's/\t/;/g' files

tr '\t' ';' < infile > outfile

Last edited by Trilby (2013-04-13 11:07:15)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2013-04-13 11:21:34

Aegidius
Member
From: Italy
Registered: 2011-06-29
Posts: 288
Website

Re: String replace in big files

Trilby thanks for the answer smile

Actually I just solved that problem. Now I have another problem, posted 2 messages above.

Egidio

Offline

#8 2013-04-14 09:25:03

Aegidius
Member
From: Italy
Registered: 2011-06-29
Posts: 288
Website

Re: String replace in big files

sad

Last edited by Aegidius (2013-04-14 09:25:24)

Offline

#9 2013-04-14 09:51:42

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: String replace in big files

Is it an option to use an in between string? sed -i 's/\\;/%escsemi%/g' files and then change it back later?

Offline

#10 2013-04-14 10:28:50

Aegidius
Member
From: Italy
Registered: 2011-06-29
Posts: 288
Website

Re: String replace in big files

Procyon thank you smile

I did it in that way, but I'm still curious why the escape character does not work ?¿?¿?¿

Offline

#11 2013-04-14 12:20:16

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: String replace in big files

Aegidius wrote:

sad

Please do not "bump".  It is against our policy
Draw attention by posting more information -- what you have learned, what you have read, what you have tried.

Thanks


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#12 2013-04-14 12:22:40

Aegidius
Member
From: Italy
Registered: 2011-06-29
Posts: 288
Website

Re: String replace in big files

I'm sorry sad

Offline

Board footer

Powered by FluxBB