2

What I need to accomplish:

I want to recursively match everything from string1 to fixed_string (both included) on text_files and output the results onto a result_file.

My command is as follow:

sed -nf /path/to/patterns_file /path/to/lists/* > result_file

For instance:

  • string1 in patterns is: 4321/14.
  • fixed_string in patterns is: ------------------------------------------------------

The content of patterns_file is as following:

/\<4321\/14\>/,/------------------------------------------------------/p
/\<4319\/14\>/,/------------------------------------------------------/p
/\<5183\/14\>/,/------------------------------------------------------/p
/\<487\/08\>/,/------------------------------------------------------/p
/\<8767\/14\>/,/------------------------------------------------------/p
/\<8768\/14\>/,/------------------------------------------------------/p
/\<8769\/14\>/,/------------------------------------------------------/p
/\<10270\/01\>/,/------------------------------------------------------/p
/\<8354\/14\>/,/------------------------------------------------------/p
/\<8883\/14\>/,/------------------------------------------------------/p
/\<3978\/13\>/,/------------------------------------------------------/p
/\<6344\/13\>/,/------------------------------------------------------/p
/\<3882\/09\>/,/------------------------------------------------------/p
/\<8881\/14\>/,/------------------------------------------------------/p
/\<9913\/14\>/,/------------------------------------------------------/p
/\<6596\/13\>/,/------------------------------------------------------/p

My test_files are as following(located in lists directory)

JUZGADO_CIVIL_10_PRO_2014-10-24.TXT
JUZGADO_CIVIL_11_PRO_2014-10-24.TXT
JUZGADO_CIVIL_12_PRO_2014-10-24.TXT
JUZGADO_CIVIL_13_PRO_2014-10-24.TXT
JUZGADO_CIVIL_14_PRO_2014-10-24.TXT
JUZGADO_CIVIL_15_PRO_2014-10-24.TXT
JUZGADO_CIVIL_16_PRO_2014-10-24.TXT
JUZGADO_CIVIL_17_PRO_2014-10-24.TXT
JUZGADO_CIVIL_18_PRO_2014-10-24.TXT
JUZGADO_CIVIL_19_PRO_2014-10-24.TXT
JUZGADO_CIVIL_1_PRO_2014-10-24.TXT
JUZGADO_CIVIL_20_PRO_2014-10-24.TXT
JUZGADO_CIVIL_21_PRO_2014-10-24.TXT
JUZGADO_CIVIL_22_PRO_2014-10-24.TXT
JUZGADO_CIVIL_23_PRO_2014-10-24.TXT
JUZGADO_CIVIL_2_PRO_2014-10-24.TXT
JUZGADO_CIVIL_3_PRO_2014-10-24.TXT
JUZGADO_CIVIL_4_PRO_2014-10-24.TXT
JUZGADO_CIVIL_5_PRO_2014-10-24.TXT
JUZGADO_CIVIL_6_PRO_2014-10-24.TXT
JUZGADO_CIVIL_7_PRO_2014-10-24.TXT
JUZGADO_CIVIL_8_PRO_2014-10-24.TXT
JUZGADO_CIVIL_9_PRO_2014-10-24.TXT
JUZGADO_NOTARIAL_PRO_2014-10-24.TXT

The content of one txt file uploaded here: get JUZGADO_CIVIL_18_PRO_2014-10-24.TXT

The rest of the files are similar to this one.

The problem is that when there is another matching expression between string1 and fixed_string I get duplicated or triplicated lines as output, like this:

[...output omitted...]

Expte. N°: 8769/14        -Foja: 9- JARA MELAGRANI, DIEGO
               C/MUNICIPALIDAD DE RESISTENCIA S/EJECUCION DE HONORARIOS -
estesestese

                CARLOS D. LUGON
         -Abogado  Secretario -
Juzgado Civil y Comercial Nº 18

 "2014 Año de la concordia, el diálogo y la paz - S.S. Francisco"

Expte. Nº 8769/14 - MMMN
Resistencia, 20 de octubre de 2014.
Téngase al Dr. DIEGO JARA MELAGRANI por presentado, parte,
con el patrocinio letrado del Dr. MAURO DANIEL FOGLIATTI, con domicilio
legal constituido, dándosele en autos la intervención que por derecho
corresponda. A lo demás, estése a lo resuelto en el Expte.Nº 8768/14 que
corresponda. A lo demás, estése a lo resuelto en el Expte.Nº 8768/14 que
corre por cuerda.- NOT.-
corre por cuerda.- NOT.-


OMAR A. MARTINEZ
OMAR A. MARTINEZ
- Juez -
- Juez -
Juzgado Civil y Comercial Nº 18
Juzgado Civil y Comercial Nº 18


NOTA DE SECRETARIA:
NOTA DE SECRETARIA:
                                Se deja constancia que a fs.12/13 del Expte.Nº
                                Se deja constancia que a fs.12/13 del Expte.Nº
8768/14 caratulado "JARA MELAGRANI, DIEGO C/ MUNICIPALIDAD DE RESISTENCIA
8768/14 caratulado "JARA MELAGRANI, DIEGO C/ MUNICIPALIDAD DE RESISTENCIA
S/ EJECUCION DE HONORARIOS", se ha ordenado la acumulación de la presente
S/ EJECUCION DE HONORARIOS", se ha ordenado la acumulación de la presente
causa a aquellos actuados.- CONSTE. SECRETARIA, 20 de octubre de 2014.-
causa a aquellos actuados.- CONSTE. SECRETARIA, 20 de octubre de 2014.-

[...output omitted...]

------------------------------------------------------

[...output omitted...]

Here for instance, there is '8768/14' between

'8769/14' and '------------------------------------------------------'.

If it's not clear what I want to achieve or what the problem is, I can try to further clarify the issue.

How can fix that problem? Is there a better and more efficient solution to this?

  • HARD to understand what do you mean is(for ME). Please clarify exactly what do you want to want/change from which input file and what exactly do you want in output. Please give us an example fom input and output. – αғsнιη Oct 27 '14 at 5:36
  • Hi KasiyA, thanks for reading this. What I want is to avoid having duplicated or triplicated lines in the output of the search.sh script. If you run the script you'll notice what I mean by 'duplicated or triplicated lines' in the output. – mariano Oct 27 '14 at 5:42
  • The input file are all the files in the lists/* directory, which can be seen above. – mariano Oct 28 '14 at 2:31
  • I've tried @glennjackman's answer but got nothing so far. I've aslo read your linked post but I don't think it applies to what I looking for here, but thanks anyway. – mariano Oct 29 '14 at 3:15
2

Because inside the 8769/14 "document" you'll find the string "8768/14", so 2 patterns are matching the same lines. You'll need to be more specific about the starting range, like

/^Expte. N°: 8769\/14\>/,/------------------------------------------------------/p

That only matches at the beginning of a line.

  • Hi Glenn, tnx for answering. I tried your suggestion but I've got this error: – mariano Oct 28 '14 at 2:07
  • sed: file patterns line 1: unknown command: `1' – mariano Oct 28 '14 at 2:19
  • 1
    I forgot to escape the / -- fixed – glenn jackman Oct 28 '14 at 2:35
  • 1
    What's up with the \>? How come it works with it when there's no literal > character O.o? – Alaa Ali Oct 28 '14 at 3:00
  • 2
    That's an end-of-word marker in sed's basic regular expressions – glenn jackman Oct 28 '14 at 10:09
1

Nevermind, I found a solution to this issue adding the command 'uniq' as in

sed -nf /path/to/patterns_file /path/to/lists/* | uniq > result_file

Thanks anyway to those that took the time to read and help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.