Cd to another directory to view a log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cd to another directory to view a log file
# 1  
Old 10-18-2018
Cd to another directory to view a log file

Hi guys

Today I have been working on a script to execute to view entries within a log file.

I have successfully got the command I want to execute within the script itself. I want to view the last 5 entries within a log file and see just the last numbers. The file name would change depending on the date.

Code:
sort -rn $(ls -c localhost_access_log.*.txt | head -n 1) | sed -r '/HTTP\/1\.1" [0-9]+ [0-9]+ [0-9]$/d' | head -n 5 | grep POST | sed -e 's|^.*\s\(.*\)$|\1|'

Returns what I expect to see

Code:
2347
2433
2374
2328
2287

However I want to execute this within a shell script and the file is located within a different location to the log file.

I tried to setup an alias within the shell script but that hasn't worked so I am not 100% sure how to do this.

Code:
logfile=`cd '/opt/product/apachetomcat/8080/8.5.5_8080/apache-tomcat-8.5.5/logs'`

$logfile

last_install=`sort -rn $(ls -c localhost_access_log.*.txt | head -n 1) | sed -r '/HTTP\/1\.1" [0-9]+ [0-9]+ [0-9]$/d' | head -n 5 | grep POST | sed -e 's|^.*\s\(.*\)$|\1|'`

echo $last_install

exit 0

Any help would be much appreciated.

The log file location: /opt/product/apachetomcat/8080/8.5.5_8080/apache-tomcat-8.5.5/logs

Thanks in advance
# 2  
Old 10-18-2018
Hi try:
Code:
logdir=/opt/product/apachetomcat/8080/8.5.5_8080/apache-tomcat-8.5.5/logs
cd "$logdir"

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-18-2018
Fantastic!

What was I doing wrong?

Code:
logdir='/opt/product/apachetomcat/8080/8.5.5_8080/apache-tomcat-8.5.5/logs'
cd "$logdir"

last_install=`sort -rn $(ls -c localhost_access_log.*.txt | head -n 1) | sed -r '/HTTP\/1\.1" [0-9]+ [0-9]+ [0-9]$/d' | head -n 1 | grep POST | sed -e 's|^.*\s\(.*\)$|\1|'`

echo $last_install

2308
# 4  
Old 10-18-2018
Your line:

Code:
logfile=`cd '/opt/product/apachetomcat/8080/8.5.5_8080/apache-tomcat-8.5.5/logs'`

was nonsensical, as `` captures the output of a command and the cd command outputs nothing. So you were effectively doing logfile=''
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 10-18-2018
Quote:
Originally Posted by simpsa27
What was I doing wrong?
I am not sure but probably the various nested subshells are at fault. As Corona688 mentioned. I only saw your second version and did not realise in your first posting the "cd" was within the assignment. So let us go over your code:

Code:
logdir='/opt/product/apachetomcat/8080/8.5.5_8080/apache-tomcat-8.5.5/logs'
cd "$logdir"

last_install=`sort -rn $(ls -c localhost_access_log.*.txt | head -n 1) | sed -r '/HTTP\/1\.1" [0-9]+ [0-9]+ [0-9]$/d' | head -n 1 | grep POST | sed -e 's|^.*\s\(.*\)$|\1|'`

echo $last_install

First: you should NOT use the cd-command in a script. Never! Rather than

Code:
cd /some/where
command file

do it like this:

Code:
command /some/where/file

or, better yet (especially if you need the path several times, not once), like this:

Code:
mylocation="/some/where"
command1 "${mylocation}/file
command2 "${mylocation}/otherfile

Next: the various pipelines. Always be aware that calling a program takes time: to load the bnary, then to set up and start the sub-process, then to process the data and then remove the ended subprocess. Usually processing the data is the smallest part of this. Avoid these subproccesses.

Let us have a look:

Code:
last_install=`sort -rn $(ls -c localhost_access_log.*.txt | head -n 1) | sed -r '/HTTP\/1\.1" [0-9]+ [0-9]+ [0-9]$/d' | head -n 1 | grep POST | sed -e 's|^.*\s\(.*\)$|\1|'

this:

Code:
sed -r '/HTTP\/1\.1" [0-9]+ [0-9]+ [0-9]$/d' | head -n 1 | grep POST | sed -e 's|^.*\s\(.*\)$|\1|'

will do what? First, it deletes some lines with HTTP... in them, then takes the first remaining one, the filters out all lines with POST in them (there is only one line at that time) and then trims this line (if any). All of this can be done in a single invocation of sed:

Code:
sed -n '/HTTP\/1\.1" \([0-9][0-9]* \)\{2\}[0-9]$/ !{p;q}'

This is equivalent to your first sed-invocation plus the pipeline to head: it waits for the first line NOT containing "HTTP...", prints it and quits. All other lines are silently dropped because of the "-n". That means, only one line will be printed. I also removed the extended regexps. Only GNU-sed understands them since they are not POSIX and it is good practice to write scripts as portable as possible. Now the rest. I use another rule for lines containing "POST", which trims them and then prints them out. Regardless of something being printed or not, sed quits after this, so either a line with POST is printed or nothing:

Code:
sed -n '/HTTP\/1\.1" \([0-9][0-9]* \)\{2\}[0-9]$/ !{
                         /POST/ s/^.*\s//p
                         q;
       }'

Now for the rest of the command:

Code:
last_install=`sort -rn $(ls -c localhost_access_log.*.txt | head -n 1)....

Do you really want to access the file created last or could it be the file last modified? Maybe it won't matter because logfiles are usually created, written to, then the next one is created, etc. The sorting for modification time and the one for creation time would produce the same results in this case. In any way you should NOT use backticks any more. You used "$(...)" within, why not use them outside too?

Code:
last_install=$( sort -rn $(ls -c localhost_access_log.*.txt | head -n 1)....

And, finally, make your code better readable. Lines are free of charge, so you don't have to press everything into one line:

Code:
last_install=$( sort -rn $( ls -c "$logdir"/localhost_access_log.*.txt |\
                            head -n 1 \
                          ) |\
                sed -n '/HTTP\/1\.1" \([0-9][0-9]* \)\{2\}[0-9]$/ !{
                          /POST/ s/^.*\s//p
                          q;
                        }' \
              )

It might be only me, but i find this easier to comprehend what belongs to where. Perhaps even better:

Code:
lastlogfile=$( ls -c "$logdir"/localhost_access_log.*.txt | head -n 1 )
last_install=$( sort -rn "$lastlogfile" |\
                sed -n '/HTTP\/1\.1" \([0-9][0-9]* \)\{2\}[0-9]$/ !{
                          /POST/ s/^.*\s//p
                          q;
                       }' \
              )


I hope this helps.

bakunin

Last edited by bakunin; 10-18-2018 at 12:36 PM..
This User Gave Thanks to bakunin For This Post:
# 6  
Old 10-19-2018
Requirement no longer needed. Will close

Thanks

Last edited by simpsa27; 10-19-2018 at 06:18 AM.. Reason: Not needed anymore
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Using vim to view the contents of a directory

When I use this command: vi /home/bob/.vimI expect to see. " ============================================================================ " Netrw Directory Listing (netrw v149) " /home/bob/.vim " Sorted by name " Sort sequence:... (4 Replies)
Discussion started by: cokedude
4 Replies

2. Shell Programming and Scripting

How can view log messages between two time frame from /var/log/message or any type of log files

How can view log messages between two time frame from /var/log/message or any type of log files. when logfiles are very big and especially many messages with in few minutes, I would like to display log messages between 5 minute interval. Could you pls give me the command? (1 Reply)
Discussion started by: johnveslin
1 Replies

3. Shell Programming and Scripting

Last modified time of the folder is changing when I view the file inside the directory

Hi., Last modified time of the folder is changing when I view the file inside the directory. Here is the test on sample directory. I believe that ls -l commands gives the time detail w.r.t last modified time. Pl. suggest. bash-3.2$ mkdir test bash-3.2$ cd test bash-3.2$ touch myfile.txt... (2 Replies)
Discussion started by: IND123
2 Replies

4. Shell Programming and Scripting

Command to view files in Directory

Hi, Can u provide me the command to view files in a directory.Urgent Pls. -Vamsi (4 Replies)
Discussion started by: VamsiVasili
4 Replies

5. Shell Programming and Scripting

Command to view files in Directory

Hi, Can you help me in providing me a command to view all files present in a directory. Thanks a lot. -Vamsi (1 Reply)
Discussion started by: VamsiVasili
1 Replies

6. UNIX for Dummies Questions & Answers

Unable to view files in a particular directory under /opt

Hi Everybody, I am Unable to view files in a particular directory under /opt. But, when I reboot the server, I am able to view the files.. Its happening daily. Do u 've n e answers/suggestions. Kindly help.. :eek: (1 Reply)
Discussion started by: its.simron
1 Replies

7. Linux

Unable to view files in a particular directory under /opt

Hi Everybody, I am Unable to view files in a particular directory under /opt. But, when I reboot the server, I am able to view the files.. Its happening daily. Do u 've n e answers/suggestions. Kindly help.. :confused: (1 Reply)
Discussion started by: its.simron
1 Replies

8. UNIX for Advanced & Expert Users

Can't view directory content - SUNOS

Hi, The app I support has a directory used for debugg files. It has just come to light there are no files in there. Or is there :confused: Changing into the directory and completing an ls -lta you get no files. This is truly not right for the application. If I go up one directory the... (2 Replies)
Discussion started by: nhatch
2 Replies

9. UNIX for Dummies Questions & Answers

view user permissions on directory

Hello, and thanks in advance- I just installled red hat and pinged my machine and got a reply. When i go to http://myIPhere from my other machine it asks for a password and username. It doesnt accepts the username and passwords I use to login to my linux box. I therefore think its a premissions... (1 Reply)
Discussion started by: 99miles
1 Replies

10. UNIX for Dummies Questions & Answers

Unable to view contents of a directory

Hi, first post here be gentle. Very new to Unix. Using HP-UX 10.20 I CD into a remote directory on one machine $ cd /net/remote hostname yet when I do an ll in this directory none of the contents appear. It just is empty. when I do the same command from another machine, $ cd... (13 Replies)
Discussion started by: maddave
13 Replies
Login or Register to Ask a Question