Forum Programmation.shell combine grep and sed in one command

Posté par  . Licence CC By‑SA.
Étiquettes :
-5
7
déc.
2015

I need to get all log done in my project. I'm using this command to do that:

grep -rnw $1 -e "Logger.[view]*;$" >> log.txt

this line return all lines containing Logger.[one of the these caracters] contained in the project directory "$1" except that there are some lines written on 2 or 3 lines (IDE formating). In this case I get only the first line only. I want to get the complete text of that log knowing that a log line will always end with " );" example of such line :

    Logger.v(xxxxxxxxxxxxx
    xxxxxxxxxxxxxxxx);

Here is my script:

        #!/bin/bash    
        echo "Hello Logger!
        # get project path 
        echo "project directory is $1"
        # get all project logs and store them into temporary file tmp.txt for processing
        grep -rnw $1 -e "Logger.[view]" >> tmp.txt
        echo "tmp.txt created successfully"
        # remove package name from previous result and store result into log.txt
        sed -r 's/.{52}//' tmp.txt >> log.txt
        echo "log.txt created successfully"

grep command return **file_path/file_name : line_number : line**. I found this command that returns only the line even if it is written in 2 or 3 lines but without the **file_path** **file_name** and the **line_number**

    sed -n '/Logger.[viewd]/{:start /;/!{N;b start};/Logger.[viewd]/p}' Main.java

Is there a way to have those two results combined. example :

    /home/xxx/xxx/xxx/Main.java:97:Logger.i(xxxxxxxxxxxxx);    
    /home/xxx/xxx/xxx/Main.java:106:Logger.d(yyyyyyyyyyyy
        yyyyyyyyyyyyyyyyyyyy);
  • # hi

    Posté par  . Évalué à 2.

    I would try someting like this

    
    awk '/startPattern/,/endPattern/ { exec }'
    
    # in your case 
    awk '/Logger.[view]*/,/);/ {printf FILENAME ":" NR ":"; print}' file
    
    
    

    or you can try pcregrep (perl regular expression grep)

    Il ne faut pas décorner les boeufs avant d'avoir semé le vent

    • [^] # Re: hi

      Posté par  . Évalué à 2.

      sorry I made a little mistake the filename and linenumber are alwas repeated, this one should do the trick

      awk 'BEGIN {state = false }; /Logger.[view]*/ { printf FILENAME ":" NR " "; print ; state = !( $0 ~ /);/ ) }; state {print ; state = !($0 ~ /);/) } ' file1 file2...

      Il ne faut pas décorner les boeufs avant d'avoir semé le vent

Suivre le flux des commentaires

Note : les commentaires appartiennent à celles et ceux qui les ont postés. Nous n’en sommes pas responsables.