2

I have a log.txt file with various lines of strings and information.

I want to write a script to read the log.txt file line by line and any line that contains the word debug I want to put that line into a new file called debug.txt and all other lines should go to info.txt.

What is the best way to go about doing this? I tried to do a while loop but couldn't figure it out. Thanks

azo_blnt
  • 23
  • 1
  • 1
  • 3
  • 1
    You _can_ process text files line by line in bash, but it's really not recommended. And it's slow, since the bash `read` command reads its input data byte by byte. For further info, please see [Why is using a shell loop to process text considered bad practice?](http://unix.stackexchange.com/q/169716/88378), and the associated links. I _was_ going to post an answer containing a short script using grep, and also mentioning that you can easily do it using awk, but Archemar has beaten me to it. :) – PM 2Ring Jul 07 '15 at 03:56

2 Answers2

4

there are zillion way to do it, first two I came with are awk and grep (in that order)

awk

awk '/debug/ { print > "debug.txt" ; next ;} { print } ' logfile > info.txt

where

  • /debug/ select line with word debug
  • { print > "debug.txt" ; print to debug.txt
  • next ;} read next line in logfile
  • { print } if no debug, print to stdout
  • > info.txt redirect stdout to info.txt

a more formal command

awk '/debug/ { print > "debug.txt" ; } 
  $0 !~ /debug/ { print > "info.txt " ; } ' logfile

where

  • $0 !~ /debug/ means, if debug does not appear on the line.

grep

 grep    debug logfile > debug.txt
 grep -v debug logfile > info.txt

where

  • grep debug select line with debug
  • grep -v debug select line without debug
  • logfile is read twice

please note also that previous content of debug.txt and info.txt will be deleted by using > to keep it, use >> in shell or in awk.

Archemar
  • 31,183
  • 18
  • 69
  • 104
2

Here is a bash script to do the job :

while IFS= read -r line; do
    if [[ $line =~ debug ]]; then
        echo "$line" >>debug.txt
    else
        echo "$line" >>info.txt
    fi
done <log.txt
heemayl
  • 54,820
  • 8
  • 124
  • 141