Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

233
Vistas
Grep with multiple strings and wildcard

I'm trying to get matches from a log file with multiple strings and a wildcard. This is how the log looks like

test.log

abc|07Jan2016:sessionId=F4DF
<<random log lines>>
def|08Jan2016:sessionId=5415
<<random log lines>>
abc|08Jan2016:sessionId=F4DF
<<random log lines>>
xyz|09Jan2016:sessionId=F3D2
<<random log lines>>
ijk|06Jan2016:sessionId=CF38

The result I'm expecting

abc|07Jan2016:sessionId=F4DF
ijk|06Jan2016:sessionId=CF38

As you can see, I just want to get the log lines that have sessionIds from lines that have the string matches 'abc' and 'ijk'

The grep command that I tried

grep -m 1 'abc.*sessionId\|ijk.*sessionId' test.log

The result I'm getting

ijk|06Jan2016:sessionId=CF38

The grep is not looking for matches with the string 'abc', but it is looking for the 'ijk' match with the wildcard '.*sessionId' Can somebody please let me know what I'm missing here..?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

This awk may solve the problem:

awk 'BEGIN {FS="\\|"; pat["abc"]; pat["ijk"]}
$1 in pat && /:sessionId=/ {print; delete pat[$1]}' file.log

abc|07Jan2016:sessionId=F4DF
ijk|06Jan2016:sessionId=CF38

Code Demo

over 4 years ago · Santiago Trujillo Denunciar

0

With your shown samples, please try following awk code. This will exit from program once one of each string's very 1st occurrence is printed, so this will not read whole Input_file.

awk -F'|' '
($1=="abc" || $1=="xyz") && ++arr[$1]==1{
   count++
   print
}
count==2{ exit }
' Input_file

Explanation: Adding detailed explanation for above code.

awk -F'|' '                                ##Starting awk program from here and setting field separator as | here.
($1=="abc" || $1=="xyz") && ++arr[$1]==1{  ##Checking condition if 1st field is abc OR xyz AND their respective index in array arr is ONLY 1.
   count++                                 ##Increase count with 1 here.
   print                                   ##Printing current line here.
}
count==2{ exit }                           ##Checking condition if count is 2 then exit from program.
' Input_file                               ##Mentioning Input_file name here.
over 4 years ago · Santiago Trujillo Denunciar

0

Suggestion with grep:

grep -E "^(abc|ijk)\|" file.log

Suggesting with awk:

 awk '/^(abc|ijk)\|/1' file.log
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda