-1

Get 2 texts from this line:

[23/08/2019 09:30:00 351] [DEBUG] [Hilo:Thread-17905] [Clase:co.com.colombiamovil.activator.db.PoolFacade: getSiebelDirectConnection (81)] [Conexi???n establecida  7682 siebelDB: TMAC70580369126846, duraci???n: 90]

Output:

23/08/2019 09:30:00 | duraci???n: 90 

or

23/08/2019 09:30:00 | 90

With this I take the duration:

grep "duraci???n: " ussdGw.log | cut -d" " -f16 | tr -d "]"

but I can't get the date out

Help me

terdon
  • 242,166

5 Answers5

1

You could just use awk for this:

$ awk 'gsub(/[][]/,""){print $1,$2,"|",$NF}' file
23/08/2019 09:30:00 | 90

The gsub(/[][]/,"") will replace all occurrences of either [ or ] (that's a character class: [ ] consisting of two characters ] and [, so becomes [ ][ ]) on the current line and then, if that was successfull, you print the 1st field (the date), the second field (the time) and the last (NF) field, the duration.

terdon
  • 242,166
0

I guess you want to parse a logfile with lots of lines? Making use of substring-manipulation:

$ cat get2texts.sh:

#!/bin/bash
logfile="$1"
IFS=']'
while read -r one two three four five junk; do
  one="${one#[}"
  one="${one% *}"
  five="${five/*,/ | }"
  echo "$one $five"
done < "$logfile" \
| tr -s ' ' ' '

Result:

$ get2texts.sh testfile
23/08/2019 09:30:00 | duraci???n: 90
markgraf
  • 2,860
0

If you set FS sensibly, you might get away with e.g.:

awk '{ print $2, $3, "| " $(NF-1) }' FS='[] []+' infile

Output:

23/08/2019 09:30:00 | 90
Thor
  • 17,182
0

To get the date and duration in lines containing duraci???n: with sed:

$ sed -n 's/^\[\([^ ]* [^ ]*\).*\(duraci???n:.*\)\]$/\1 | \2/p' logfile
23/08/2019 09:30:00 | duraci???n: 90

or

$ sed -n 's/^\[\([^ ]* [^ ]*\).*duraci???n: \(.*\)\]$/\1 | \2/p' logfile
23/08/2019 09:30:00 | 90


Explanation of the first command:

  • sed -n be silent (only matched parts are printed with the p flag)
  • 's/ substitute
  • ^\[ match [ at the start of the line
  • \( start first group
  • [^ ]* [^ ]* match date and time (any no space characters followed by space followed by any no space characters)
  • \) end first group
  • .* match any characters
  • \( start second group
  • duraci???n:.* match duraci???n:.* followed by any characters
  • \) end second group
  • \]$ match ] at the end of the line
  • /\1 | \2/ replace with first group, | and the second group
  • p' print matches
Freddy
  • 25,565
-1
awk '{print $1,$2,$NF}' filename| sed "s/\[//g"|sed "s/\]//g"| awk '{$NF=" | "$NF;print $0}'

output

23/08/2019 09:30:00  | 90