LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-18-2018, 07:33 AM   #16
slayer_1994
Member
 
Registered: Feb 2017
Posts: 38

Original Poster
Rep: Reputation: Disabled

Am I doing something wrong here?

Code:
$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 1 | grep POST | sed -e 's|^.*\s\(.*\)$|\1|'`

echo $last_install

exit 0
The logfile is located in a different directory so I set an alias
Code:
logfile=`cd /opt/product/apachetomcat/8080/8.5.5_8080/apache-tomcat-8.5.5/logs`
Code:
ls: cannot access localhost_access_log.*.txt: No such file or directory
^C
 
Old 10-19-2018, 05:42 AM   #17
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Rep: Reputation: 61
What you want there is:
Code:
logfile=/opt/product/apachetomcat/8080/8.5.5_8080/apache-tomcat-8.5.5/logs
cd ${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 1 | grep POST | sed -e 's|^.*\s\(.*\)$|\1|'`
echo $last_install
exit 0
 
Old 10-21-2018, 06:30 AM   #18
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by l0f4r0 View Post
Maybe you could just take the "last" one (see warning below) as input with something like:
Code:
sort -rn $(ls -c localhost_access_log.*.txt | head -n 1) | head -n 5 | sed -e 's|^.*\s\(.*\)$|\1|' | less -S
EDIT: option -c with ls instead of -t. /!\ Since -c option sorts by ctime (there doesn't seem to be an easy and universal way to sort by creation time), it works as long as information about the log files are not modified otherwise your sort output could be different from what you would have expected...
Update:
Code:
ls -1 localhost_access_log.*.txt | sort -rt'.' -k2.1,2.4 -k2.6,2.7 -k2.9,2.10 | head -1
is even better as it takes your last log file for sure!
Explanation: as the filenames display the date, this command is sorting filenames by year, then by month and finally by day. -r option reverses the order (most recent first).
 
  


Reply

Tags
echo, grep, logfile, shell script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
trying to echo a line that contains echo in a file aristosv Linux - Software 7 01-05-2016 12:39 PM
[SOLVED] long command line is wrapped at the beginning of the same line lwang3rock Linux - Newbie 10 12-28-2012 12:27 PM
[SOLVED] how to echo an '-n' without a new line? paliga Linux - Newbie 8 10-20-2010 07:44 AM
Echo every other line yitzle Linux - Software 19 08-03-2010 10:36 AM
echo on a new line does not work B-Boy Linux - Newbie 2 06-02-2008 09:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration