1

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

marked as duplicate by don_crissti, X Tian, G-Man, Gilles bash Jul 7 '15 at 11:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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?, 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 7 '15 at 3:56
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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.