0

I have a text file with the paths of certain log file which are named the same.

/inc/sync/result/debug_TFS16.log
/inc/async/result/debug_TFS16.log
/dec/sync/result/debug_TFS16.log
/dec/async/result/debug_TFS16.log

How can I pick the path one line at a time and use it to run another script that further manipulates the log file?

  • 3
    Please be more specific about what you are trying to accomplish. How are you trying to manipulate the log files? Do you even have write access to them? What does the other script do and can you provide an example of the code? – Nasir Riley Dec 20 '18 at 1:38
  • The logfile contains lots of useless info and the max value. I will be using grep to get the max value and pipe it out to another text file. my issue is that, since the different log files are in different locations, i want to know how i can write a script to get the path, one line at a time and execute the grep to get max value. – Vineeth Thomas Dec 20 '18 at 13:59
  • Just to clarify, you're trying to read this log file and manipulate it at the same time? What sequence of events do you envision here? – Jeff Schaller Dec 20 '18 at 14:14
  • @JeffSchaller<br> ok this is exactly what i want done... i run the script which goes to the text file which has the path to the log file: /inc/sync/result/debug_TFS16.log<br/> /inc/async/result/debug_TFS16.log<br/> /dec/sync/result/debug_TFS16.log<br/> /dec/async/result/debug_TFS16.log<br/> then go to that path on line 1 and grep "max_val" debug_TFS16.log > log1.txt then go to path on line 2 and grep "max_val" debug_TFS16.log > log2.txt – Vineeth Thomas Dec 20 '18 at 19:04
  • I'm sorry for my post above, i'm very new to this and still learning how to format this. – Vineeth Thomas Dec 20 '18 at 19:09
0

Maybe using a loop:

#! /bin/bash

COUNT=0

while read line 
do 

COUNT=`expr ${COUNT} + 1`

PATHIN=`dirname $line`
FILENAME=`cat $line | awk -F"/" '{print $NF}'`

cd ${PATHIN}
grep "max_val" ${FILENAME} > Log_${COUNT}.log

done < input_file

You only must change input_file for name of file that have all data.

0

The command you search for is dirname for example for file:

/dec/async/result/debug_TFS16.log

if you execute

dirname /dec/async/result/debug_TFS16.log

you will get

/dec/async/result
0

Using bash features to read the filenames into an array,

readarray -t list < input
for((i=0; i<${#list[@]}; i++))
do 
  grep max_val "${list[i]}" > log"$((i+1))".txt
done

This reads the filenames into the array variable list, then loops over the indexes of that array (in i) and runs the grep command on the corresponding filename and redirects the output to the desired log filename. Bash arrays are zero-indexed, so I add one to the array index to create the filename.

0

You can step over each line in a file with a while read loop. Here's a sample file, called "samplefile.txt".

root@server :~$ cat samplefile.txt
line 1
line #2
line3
 line 4
#line5 shouldn't print
  # line 6 shouldn't print either
line 7 should print

If you want to add the ability to ignore lines starting with '#', you can add that into the command.

root@server :~$ while read line ; do echo -e "$line" | egrep -v '\s*^#'; done < samplefile.txt
line 1
line #2
line3
line 4
line 7 should print

Note that some lines started with a space, but the latter output didn't show that. That may or may not affect your scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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