accessing a path from an external text file
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?
shell-script
|
show 2 more comments
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?
shell-script
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 andgrep "max_val" debug_TFS16.log > log1.txt
then go to path on line 2 andgrep "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
|
show 2 more comments
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?
shell-script
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?
shell-script
shell-script
edited Dec 20 '18 at 21:08
roaima
42.8k551116
42.8k551116
asked Dec 20 '18 at 1:29
Vineeth Thomas
213
213
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 andgrep "max_val" debug_TFS16.log > log1.txt
then go to path on line 2 andgrep "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
|
show 2 more comments
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 andgrep "max_val" debug_TFS16.log > log1.txt
then go to path on line 2 andgrep "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
3
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
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
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
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
@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
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
|
show 2 more comments
4 Answers
4
active
oldest
votes
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.
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490048%2faccessing-a-path-from-an-external-text-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 20 '18 at 21:50
MrDaniiell
16
16
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Dec 20 '18 at 4:15
Romeo Ninov
5,21231827
5,21231827
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 20 '18 at 19:17
Jeff Schaller
38.8k1053125
38.8k1053125
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 20 '18 at 21:41
user208145
1,26121215
1,26121215
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490048%2faccessing-a-path-from-an-external-text-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 andgrep "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