Issue
I have 4 different named log
files, all with txt
extensions. I need to write a bash script file that extracts JavaScript file names from any of these log
files regardless of their names. The output of the script should not include the path, have to be unique, and sorted
After some research I came up with this:
cat logfile1.txt | grep '[^.(]*\.js' | awk -F " " '{print $7}' | sort | uniq -c| sort -nr
This code does only haft the job;
- PRO: It does extract any JS,
sorts
it, and givesunique
results.
- CON: I need this in a file.sh not a command line as, it is now. Also, I’m getting the entire path to the JS file. I only need the file name
jquery.js
I tried adding grep -v "*/name-of-path-before-JS"
to block the result from giving me the full path but that isn’t working.
I found someone who made something kind of similar using python;
source
filenames = set()
with open(r"/home/filelog.txt") as f:
for line in f:
end = line.rfind(".js") + 3 # 3 = len(".js")
start = line.rfind("/", 0, end) + 1 # 1 = len("/")
filename = line[start:end]
if filename.endswith(".js"):
filenames.add(filename)
for filename in sorted(filenames, key=str.lower):
print(filename)
Although is missing the sort
and uniq
options when giving the output
it does give the results by only putting out filename.js
and not the whole path as the command line I made. Also, I to add the path to the log.txt
file while running the script and not just appended it as in the python script below.
Example;
$./LogReaderScript.sh File-log.txt
Solution
Would you please try the shell script LogReaderScript.sh
:
#!/bin/bash
if [[ $# -eq 0 ]]; then # if no filenames are given
echo "usage: $0 logfile .." # then show the usage and abort
exit 1
fi
grep -hoE "[^/]+\.js" "[email protected]" | sort | uniq -c | sort -nr
By setting the file as executable with chmod +x LogReaderScript.sh
,
you can invoke:
./LogReaderScript.sh File-log.txt
If you want to process multiple files at a time, you can also say something
like:
./LogReaderScript.sh *.txt
-o
option togrep
tells grep to print the matched substrings only,
instead of printing the matched line.-E
option specifiesextended regex
as a pettern.-h
option suppresses the prefixed filenames on the output if multiple
files are given.- The pattern (regex)
[^/]+\.js
matches a sequence of any characters
other than a slash, and followed by a extention.js
. It will match
the target filenames. "[email protected]"
is expanded to the filename(s) passed as arguments to the script.
Answered By – tshiono
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0