4

I am new to Linux and have a very large text log file from which to extract. I thought to use bash?

For example, the file contains:

Node:xyz
Time:01/07/13 14:26:17
INFO: Trusted certif ok

Node:abc
Time:01/07/13 14:26:18
INFO: Trusted certif ok

Node:def
Time:01/07/13 14:26:18
INFO: Trusted certif not ok

I need to extract the text after Node: and add it to the text after Info: to display on one line, output to be redirected to a new file. I am trying awk and sed, but not figured it out yet. Help much appreciated.

Example output would look like:

xyz Trusted certif ok
abc Trusted certif ok
dbf Trusted certif not ok

4 Answers 4

13

Try doing this :

in

awk -F: '/^Node/{v=$2}/^INFO/{print v $2}' file.txt

in :

while IFS=: read -r c1 c2; do
    [[ $c1 == Node ]] && var=$c1
    [[ $c1 == INFO ]] && echo "$var$c2"
done < file.txt

in :

perl -F: -lane '
    $v = $F[1] if $F[0] eq "Node";
    print $v, $F[1] if $F[0] eq "INFO"
' file.txt

in (in a file, Usage : ./script.py file.txt ):

import sys
file = open(sys.argv[1])
while 1:
    line = file.readline()
    tpl = line.split(":")
    if tpl[0] == "Node":
        var = tpl[0]
    if tpl[0] == "INFO":
        print var, tpl[1]
    if not line:
        break
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you all very much. awk is awesome, as is your help.
@EdMorton: what is the problem with the pure bash solution?
@TrueY shell is an environment from which to call tools. It has programming language constructs (loops, etc.) to help you sequence the order in which you call tools. It is not a tool for parsing text files and so it's capabilities for doing that are extremely limited and it's side-effects non-obvious. For example, courtesy of the missing -r argument for read the script you posted will incorrectly interpret backslashes, and the use of echo will only work on some systems with some inputs. There may be additional edge cases it fails for and it's over twice the length of the robust awk script.
@EdMorton: Thx 4 the answer! Generaly you are right. Every shell, interpreter and compiler has its own strength and weakness. The specific case will decide which tool to use. IMHO if some tool os not needed, it is better not to use. So in this case I prefer the pure bash version.
@canfiese: try gawk
|
2

Using sed:

sed -n '/^Node/N;/Time/N;s/^Node:\([^\n]*\)\n[^\n]*\n[^ ]* /\1 /p' input

Comments

0
perl -F: -lane '$x=$F[1] if(/^Node:/);if(/^INFO:/){print "$x".$F[1];}' your_file

tested below:

> cat temp
Node:xyz
Time:01/07/13 14:26:17
INFO: Trusted certif ok

Node:abc
Time:01/07/13 14:26:18
INFO: Trusted certif ok

Node:def
Time:01/07/13 14:26:18
INFO: Trusted certif not ok

> perl -F: -lane '$x=$F[1] if(/^Node:/);if(/^INFO:/){print "$x".$F[1];}' temp
xyz  Trusted certif ok
abc  Trusted certif ok
def  Trusted certif not ok

Comments

0
sed -n 'N;N;s/\n.*\n/ /;s/\S*://g;p;n' file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.