extract string to recotd new file - Extraer un parrafo y grabarlo en nuevo archivo - Forum

Forum Navigation
You need to log in to create posts and topics.

extract string to recotd new file - Extraer un parrafo y grabarlo en nuevo archivo

Hello @Gaev, I think you are the person who can give me a hand for your great experience with neowin.

look at this string

'View zones' => 'View zones',

I need to build an application that reads a file.
take the number of lines, then in a loop read line by line (UP TO HERE I ASSEMBLE IT WITHOUT PROBLEMS)
and from each line it has to take what is to the right of the sign => 'View zones', and record it in a new file on the same line as the origin.

Where I'm stuck is how to make it take the string from the sign...
If you can give me a hand eternally grateful

 

ESPANISH

Si alguna persona  me puede dar una mano le estare muy agradecido

Mirar este parrafo que es una linea de un archivo

'View zones' => 'View zones',

yo necesito armar una aplicacion que lea un archivo.
tome la cantidad de lineas
luego en un loop lea linea por linea (HASTA ACA LO ARMO SIN PROBLEMAS)
y de cada linea tiene que tomar lo que esta a la derecha del signo => 'View zones', e ir grabandolo en un nuevo archivo en la misma linea que el origen.

Donde estoy trabado es en como hacer que tome el string a partir del signo...
Si me puedes dar una mano eternamente agradecido

 

Saludos Cordiales

 

@joferar333

I am confused by this phrase ...

from each line it has to take what is to the right of the sign => 'View zones'

... can you provide an example of (say 2 or 3 lines) of the content of the input file ... and the desired output to the new file ?

Hi @gaev

This file origin

 

'Category' => 'Category',
'Message' => 'Message',
'Translation' => 'Translation',
'Please make sure you save your changes before navigating away to a different page.' => 'Please make sure you save your changes before navigating away to a different page.',
'{name} translations' => '{name} translations',
'Translations' => 'Translations',
'Language' => 'Language',

 

And this is result to new file

 

'Category',
'Message',
'Translation',
'Please make sure you save your changes before navigating away to a different page.',
'{name} translations',
'Translations',
Language',

 

Thanks!

Try this

SetVar "[origen]" "joferar.txt"
SetVar "[destino]" "Resultadojoferar.txt"
FileLen "[origen]" "[origenLen]"
Loop "1" "[origenLen]" "[x]"
     FileRead "[origen]" "[x]" "[leido]"
     SearchStr "=>" "[leido]" "[pos]" ""
     SubStr "[leido]" "1" "[pos]-2" "[escribir]"
     FileWrite "[destino]" "Append" "[escribir],"
EndLoop

salu2

joferar333 has reacted to this post.
joferar333

@joferar333

Try this ...

SetVar "[inputFile]" "joferar333Input.txt"
SetVar "[outputFile]" "joferar333Output.txt"

FileExists "[outputFile]" "[outputExists]"
If "[outputExists]" "=" "True"
   FileErase "[outputFile]"
EndIf


FileLen "[inputFile]" "[inputLen]"
Loop "1" "[inputLen]" "[inputLineNumber]"
   FileRead "[inputFile]" "[inputLineNumber]" "[thisLine]"
   ... parse content
   StrParse "[thisLine]" "=>" "[thisLinePart]" "[junk]"
   ... [thisLinePart2] is what you want
   FileWrite "[outputFile]" "Append" "[thisLinePart2]"
EndLoop

AlertBox "All" "Done"

 

 

 

joferar333 has reacted to this post.
joferar333

@gaev

Worked perfectly.

What I would finally need now that I have the translations and replace the text after => by the translated line.

You must read the file line by line, and replace the text taking that text from the same line of the second file that contains the translation....

here example

source file to be replaced

'Amazon S3' => 'Amazon S3',
'Common' => 'Common',
'Customers' => 'Customers',
'Uploaded email templates' => 'Uploaded email templates',
'S3 access key' => 'S3 access key',
'S3 access secret' => 'S3 access secret',
'S3 region' => 'S3 region',

Resulting file after replacing the line after the => sign (the line to replace takes from an external file that is translated)

'Amazon S3' => 'Amazon S3',
'Common' => 'Común',
'Customers' => 'Clientes',
'Uploaded email templates' => 'Plantillas de correo electrónico cargadas',
'S3 access key' => 'Clave de acceso de S3',
'S3 access secret' => 'Secreto de acceso de S3',
'S3 region' => 'Región de S3',

Thanks a million master....

@joferar333

I don't understand why you need to do this line by line ... why not just ...

1) rename the source file (the one with blue text) using the FileRename command

2) copy the translated file (the one with red text) to be the new 'source file' using the FileCopy command

@gaev

No, I think you have not understood me

I need to replace what is after => the following, that is, the following paragraph with a new paragraph in each of the lines of the file.

 

'Amazon S3' => 'Amazon S3',
'Common' => 'Common',
'Customers' => 'Customers',
'Uploaded email templates' => 'Uploaded email templates',
'S3 access key' => 'S3 access key',
'S3 access secret' => 'S3 access secret',
'S3 region' => 'S3 region',

 

@joferar333

I need to replace what is after => the following, that is, the following paragraph with a new paragraph in each of the lines of the file.

If not as shown in the red text, what does the external file look like ?

@gaev

The external file is exactly the same line by line, only that what it replaces is translated into Spanish The original file separated the strings and wrote a new file, that file was translated and now I finally have to replace the content of the original line with the translated line

@joferar333

This getting confusing (perhaps something is lost in translation).

Please provide detailed contents of all files (before and after), along with their (reference) names.

@gaev

I need to replace what is after the => sign in each line of the source file with the same line of the external file
SOURCE FILE
file1.txt
'Amazon S3' => 'Amazon S3',
'Common' => 'Common',
'Customers' => 'Customers',
'Uploaded email templates' => 'Uploaded email templates',
'S3 access key' => 'S3 access key',
'S3 access secret' => 'S3 access secret',
'S3 region' => 'S3 region',
*************************************************************
EXTERNAL FILE
external1.txt
'Amazon S3',
'Común',
'Clientes',
'Plantillas de correo electrónico cargadas',
'Clave de acceso de S3',
'Secreto de acceso de S3',
'Región de S3',
--------------------------------------------------------------------------
FILE FINAL
file3.txt
'Amazon S3' => 'Amazon S3',
'Common' => 'Común',
'Customers' =>'Clientes',
'Uploaded email templates' =>
'S3 access key' => 'Plantillas de correo electrónico cargadas',
'S3 access secret' => 'Clave de acceso de S3',
'S3 region' =>  'Región de S3',

 

@joferar333

1) the content of file3.txt seems messed up ...

a) 'Uploaded email templates' => is empty
b) 'S3 access key' appears to have the missing text for (a)
c) 'S3 access secret' appears to have the text for 'S3 access key'

... please verify and re-upload the correct desired content

2) Let us call the file resulting from the first step requested in this post to be file1b.txt

3) Since external1.txt does not have any of the prompts, the only way to merge with lines in file1b.txt would be to assume that line 1 of external1.txt is for line1 in file1b.txt (and so on for each of the lines) ... is that a correct assumption ?

4) Approximately how many such lines do you have in file1b.txt file ?

@gaev

both files contain the same number of lines and each
of the lines correspond to the same line in both file1 and file2

What I need is in file1 that the strings found after the sign => are replaced by the same line from file2

resulting in a file3 that will be the same as 1 but with the strings in both English and Spanish.

I must repeat this in the 108 files that I have to process
they are all the same process...

@gaev

example

FILE1

'Amazon S3' => 'Amazon S3',
'Common' => 'Common',
'Customers' => 'Customers',
'Uploaded email templates' => 'Uploaded email templates',
'S3 access key' => 'S3 access key',
'S3 access secret' => 'S3 access secret',
'S3 region' => 'S3 region',

FILE2

'xxxxxxxxxx',
'xxxxxxxxxx',
'xxxxxxxxxx',
'xxxxxxxxxx',
'xxxxxxxxxx',
'xxxxxxxxxx',
'xxxxxxxxxx',

FILE3

'S3 region' => 'REPLACE THIS LINE TO line FILE 2 xxxxxxxxxx ',

RESULT FINAL

'S3 region' => 'XXXXXXXXXX'

@joferar333

I have done the scripting, but I can not test it without copies of the source file and 1 copy of the external file (not just 'XXXX').

Please upload these two files.

@gaev

file1 and file2

I hope you can understand me now
here are two real files
I need to replace from file 1 what is written after =>
along the same line what file 2 contains

and this will write a new file with the result...

 

Uploaded files:
  • You need to login to have access to uploads.

@joferar333

This code works (except that due to my Windows 10 configuration of Canadian English, notepad.exe appears to not show some of the Spanish accented characters) ...

SetVar "[sourceFile]" "file1.txt"
SetVar "[externalFile]" "file2.txt"

SetVar "[outputFile]" "joferar333Output.txt"

FileExists "[sourceFile]" "[sourceExists]"
If "[sourceExists]" "=" "False"
   AlertBox "Alert" "Missing Source File"
   Return
EndIf

FileExists "[externalFile]" "[externalExists]"
If "[externalExists]" "=" "False"
   AlertBox "Alert" "Missing External File"
   Return
EndIf

FileExists "[outputFile]" "[outputExists]"
If "[outputExists]" "=" "True"
   FileErase "[outputFile]"
EndIf


FileLen "[sourceFile]" "[sourceLen]"
Loop "1" "[sourceLen]" "[sourceLineNumber]"
   FileRead "[externalFile]" "[sourceLineNumber]" "[thisExternalLine]"
   ... line from sourceFile
   FileRead "[sourceFile]" "[sourceLineNumber]" "[thisSourceLine]"
   ... parse content
   StrParse "[thisSourceLine]" "=>" "[thisSourceLinePart]" "[junk]"

   ... merge [thisSourceLinePart1] with [thisExternalLine]
   SetVar "[thisOutputLine]" "[thisSourceLinePart1] '=>' [thisExternalLine]"
   ... now save to file
   FileWrite "[outputFile]" "Append" "[thisOutputLine]"
EndLoop

AlertBox "All" "Done"

Please run this code and verify that all output lines are as expected.

Note that while I ...

- check for the presence of source and external files
- delete any leftover output file from previous runs

... in order to make it more robust, you could also check ...

- for a non-empty source file
- length of source and external files being the same

luiz has reacted to this post.
luiz

@gaev

perfect!!!!!!

Found OK!!!!!

thank!!!!!