I am trying to count the occurence of a particular line in a file using shell script. The content of the file is:
Mon Dec 23 06:21:00 2019 [pid 3294] [ftpuser] FTP response: Client "127.0.0.1", "230 Login successfull."
Mon Dec 23 06:21:00 2019 [pid 3294] [ftpuser] FTP response: Client "127.0.0.1", "230 Login successfull."
Mon Dec 23 06:21:00 2019 [pid 3294] [ftpuser] FTP response: Client "127.0.0.1", "230 Login successfull."
Mon Dec 23 06:21:00 2019 [pid 3294] [ftpuser] FTP response: Client "127.0.0.1", "230 Login successfull."
The shell script code is:
file="vsftpd1.log"
count=0
while read str; do
echo $str
if [[ $str == *"230 Login successfull."* ]];
then
count=$((count+1))
fi
done < $file
echo $count
The output of the last line should be four. I have noticed that if condition is executing only one time. But it should run four times. Please help me to find mistake in the code, if any.
Thankxx in advance!
You could simplify that with grep (match occurences) and its -c (count occurences).
#!/bin/sh
file=vsftpd1.log
grep -c '230 Login successfull.' "$file"
If you need to capture the number into a variable:
count=$(grep -c '230 Login successfull.' "$file")
echo $count
That entire (rather inefficient) snippet of code can be replaced with the much simpler:
count=$(grep -c '230 Login successfull.' vsftpd1.log)
echo ${count}
As more explanation, grep -c returns the line count of the lines that match your search pattern. There is no need for a bash-based loop which reads one line at a time and checks the pattern - this is likely to be much faster in the grep executable.
The following transcript shows this in action:
pax:~$ grep '230 Login successfull.' qq.in
Mon Dec 23 06:21:00 2019 [pid 3294] [ftpuser] FTP response: Client "127.0.0.1", "230 Login successfull."
Mon Dec 23 06:21:00 2019 [pid 3294] [ftpuser] FTP response: Client "127.0.0.1", "230 Login successfull."
Mon Dec 23 06:21:00 2019 [pid 3294] [ftpuser] FTP response: Client "127.0.0.1", "230 Login successfull."
Mon Dec 23 06:21:00 2019 [pid 3294] [ftpuser] FTP response: Client "127.0.0.1", "230 Login successfull."
pax:~$ count=$(grep -c '230 Login successfull.' qq.in) ; echo ${count}
4
when I run script in mac os x like this:
*/1 * * * * /Users/dolphin/Library/"Mobile Documents"/com~apple~CloudDocs/Document/source/dolphin/dolphin-scripts/bash/cron/latex_compile_alive_monitor.sh >> /Users/dolphin/shell.log
the output is:
From dolphin#dolphins-MacBook-Pro.local Tue Jan 21 20:47:01 2020
Return-Path: <dolphin#dolphins-MacBook-Pro.local>
X-Original-To: dolphin
Delivered-To: dolphin#dolphins-MacBook-Pro.local
Received: by dolphins-MacBook-Pro.local (Postfix, from userid 501)
id 7430417C4C19; Tue, 21 Jan 2020 20:47:00 +0800 (CST)
From: dolphin#dolphins-MacBook-Pro.local (Cron Daemon)
To: dolphin#dolphins-MacBook-Pro.local
Subject: Cron <dolphin#dolphins-MacBook-Pro> /Users/dolphin/Library/"Mobile Documents"/com~apple~CloudDocs/Document/source/dolphin/dolphin-scripts/bash/cron/latex_compile_alive_monitor.sh >> /Users/dolphin/shell.log
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=dolphin>
X-Cron-Env: <USER=dolphin>
X-Cron-Env: <HOME=/Users/dolphin>
Message-Id: <20200121124701.7430417C4C19#dolphins-MacBook-Pro.local>
Date: Tue, 21 Jan 2020 20:47:00 +0800 (CST)
+ BOOK_PATH='/Users/dolphin/Library/Mobile Documents/com~apple~CloudDocs/Document/source/dolphin/summary/'
+ COMMAND='/Library/TeX/texbin/latexmk -pdfxe -pvc -xelatex -interaction=nonstopmode '
+ PROCESS_NAME_KEYWORDS_MAP=(["dolphin-book-2020.tex"]="${COMMAND} ./dolphin-book-2020/dolphin-book-2020.tex" ["the-book-of-mine.tex"]="${COMMAND} ./the-books-of-mine/the-book-of-mine.tex" ["kubelet-learn.tex"]="${COMMAND} ./kubelet-learn/kubelet-learn.tex")
/Users/dolphin/Library/Mobile Documents/com~apple~CloudDocs/Document/source/dolphin/dolphin-scripts/bash/cron/latex_compile_alive_monitor.sh: line 15: dolphin: unbound variable
which variable was unboud? COMMAND? I am already defined in script.this is my script:
#!/usr/bin/env bash
# 当使用未初始化的变量时,程序自动退出
set -u
# 当任何一行命令执行失败时,自动退出脚本
set -e
# 在运行结果之前,先输出执行的那一行命令
set -x
BOOK_PATH="/Users/dolphin/Library/Mobile Documents/com~apple~CloudDocs/Document/source/dolphin/summary/"
COMMAND="/Library/TeX/texbin/latexmk -pdfxe -pvc -xelatex -interaction=nonstopmode "
declare -A PROCESS_NAME_KEYWORDS_MAP=(
["dolphin-book-2020.tex"]="${COMMAND} ./dolphin-book-2020/dolphin-book-2020.tex"
["the-book-of-mine.tex"]="${COMMAND} ./the-books-of-mine/the-book-of-mine.tex"
["kubelet-learn.tex"]="${COMMAND} ./kubelet-learn/kubelet-learn.tex"
)
cd "${BOOK_PATH}"
for key in ${!PROCESS_NAME_KEYWORDS_MAP[#]}
do
PID_COUNT=`ps -ef | grep "${key}" | grep -v "grep" | wc -l`
if [[ ${PID_COUNT} -lt 1 ]]; then
nohup `${PROCESS_NAME_KEYWORDS_MAP[${key}]}` &
else
echo "process already exists..."
fi
done
maybe remove the space in the end of COMMAND
By default when Laravel creates a post it stores the date and time stamp as:
2019-01-31 23:32:06
How can I show it in the view as - Jan 31st, 2018 11:32pm?
U can use Php Carbon in modifying date. eg.
$dt = Carbon::createFromFormat('Y-m-d H:i:s.u', '2019-02-01 03:45:27.612584');
// $dt->toAtomString() is the same as $dt->format(DateTime::ATOM);
echo $dt->toAtomString(); // 2019-02-01T03:45:27+00:00
echo $dt->toCookieString(); // Friday, 01-Feb-2019 03:45:27 UTC
echo $dt->toIso8601String(); // 2019-02-01T03:45:27+00:00
// Be aware we chose to use the full-extended format of the ISO 8601 norm
// Natively, DateTime::ISO8601 format is not compatible with ISO-8601 as it
// is explained here in the PHP documentation:
// https://php.net/manual/class.datetime.php#datetime.constants.iso8601
// We consider it as a PHP mistake and chose not to provide method for this
// format, but you still can use it this way:
echo $dt->format(DateTime::ISO8601); // 2019-02-01T03:45:27+0000
echo $dt->toISOString(); // 2019-02-01T03:45:27.612584Z
echo $dt->toJSON(); // 2019-02-01T03:45:27.612584Z
echo $dt->toIso8601ZuluString(); // 2019-02-01T03:45:27Z
echo $dt->toDateTimeLocalString(); // 2019-02-01T03:45:27
echo $dt->toRfc822String(); // Fri, 01 Feb 19 03:45:27 +0000
echo $dt->toRfc850String(); // Friday, 01-Feb-19 03:45:27 UTC
echo $dt->toRfc1036String(); // Fri, 01 Feb 19 03:45:27 +0000
echo $dt->toRfc1123String(); // Fri, 01 Feb 2019 03:45:27 +0000
echo $dt->toRfc2822String(); // Fri, 01 Feb 2019 03:45:27 +0000
echo $dt->toRfc3339String(); // 2019-02-01T03:45:27+00:00
echo $dt->toRfc7231String(); // Fri, 01 Feb 2019 03:45:27 GMT
echo $dt->toRssString(); // Fri, 01 Feb 2019 03:45:27 +0000
echo $dt->toW3cString(); // 2019-02-01T03:45:27+00:00
Kindly read more about Php Carbon
You can use Carbon in laravel.
First, use carbon:
use Carbon\Carbon;
Then use the format you want, in your case, it would be like this:
public function test()
{
$dt = User::find(1);
return $dt->created_at->format('M jS\\, Y h:i:s A'); //Dec 1st, 1975 02:15:16 PM
}
You can also use it in your view, just use carbon at the top of your file:
<?php use Carbon\Carbon;?>
then change the format in your display like {{$dt->created_at->format('M jS\\, Y h:i:s A')}}
Your input is 2018-01-31 23:32:06.
Your expected output is Jan 31st, 2018 11:32pm.
I have created a function:
function convertToPreferedDateFormat($dateString='')
{
$toTimeString = strtotime($dateString);
$yourExpectedFormat = 'M dS, Y h:ia';
// $prettyBeautifullWay = 'l jS \of F Y h:i:s A';
return date($yourExpectedFormat, $toTimeString);
}
Then now testing
$timeString = '2018-01-31 23:32:06';
$expectedOutput = 'Jan 31st, 2018 11:32pm';
echo convertToPreferedDateFormat($timeString);
echo "<br>";
echo $expectedOutput;
I am attempting to use perl to rename files based on the folder they are in and the time created. Files GOPR1521.MP4 and GOPR7754.MP4 were created on two different cameras at the same time and date, and I want to be able their names to indicate that. For example .../GoProTravisL/GOPR1521.mp4 created at 12:32:38 should become 123238L_GOPR1520.mp4, and GOPR7754.MP4 becomes 123239R_GOPR7754.MP4. Right now the only problem is the time stamps. I would think its a problem with being wrong timezone or hour offset, but the minutes are off too. Is there something in perl I am missing when getting time stamps? Below is the perl code, what it outputs for times for each file, and what Finder on OS X says the creation times are.
Code:
#!/usr/bin/perl
use Time::Piece;
use File::stat;
use File::Find;
use File::Basename;
use File::Spec;
#files = <$ARGV[0]/>;
find({ wanted => \&process_file, no_chdir => 1 }, #files);
sub process_file {
my($filename, $dirs, $suffix) = fileparse($_,qr/\.[^.]*/);
if ((-f $_) && ($filename ne "" )) {
#print "\n\nThis is a file: $_";
#print "\nFile: $filename";
#print "\nDIR: $dirs";
my(#parsedirs) = File::Spec->splitdir($dirs);
my #strippeddirs;
foreach my $element ( #parsedirs ) {
push #strippeddirs, $element if defined $element and $element ne '';
}
$pardir = pop(#strippeddirs);
#print "\nParse DIR: ", $pardir;
#print "\nFile creation time: ";
$timestamp = localtime(stat($_)->ctime)->strftime("%H%M%S"); #gives time stamp
print $timestamp;
$newname = $timestamp . substr($pardir,-1) ."_". $filename . $suffix;
print "\nRename: $dirs$filename$suffix to $dirs$newname\n";
#rename ($dirs . $filename . $suffix,$dirs . $newname) || die ( "Error in renaming: " . $! );
} else {
print "\n\nThis is not file: $_\n";
}
}
Output of time stamps for each file:
/Volumes/Scratch/Raw/2016-03-21/GoProTravisL/
File: GOPR1520
File creation time: 05-55-21
File: GOPR1521
File creation time: 05-56-18
File: GOPR1522
File creation time: 05-57-44
File: GOPR1523
File creation time: 05-58-49
File: GP011520
File creation time: 05-59-53
/Volumes/Scratch/Raw/2016-03-21/GoProTravisR
File: GOPR7754
File creation time: 06-02-48
File: GOPR7755
File creation time: 06-04-19
File: GOPR7756
File creation time: 06-06-27
File: GOPR7757
File creation time: 00-06-16
File: GP017754
File creation time: 00-19-30
File: GP027754
File creation time: 00-22-20
Actual file times using ls:
MacTravis:2016-03-21 travis$ ls -lR /Volumes/Scratch/Raw/2016-03-21
total 0
drwxr-xr-x 8 travis admin 272 Apr 9 21:25 GoProTravisL
drwxr-xr-x 9 travis admin 306 Apr 9 21:25 GoProTravisR
/Volumes/Scratch/Raw/2016-03-21/GoProTravisL:
total 21347376
-rw------- 1 travis admin 4001240088 Mar 21 12:04 GOPR1520.MP4
-rw------- 1 travis admin 1447364149 Mar 21 12:31 GOPR1521.MP4
-rw------- 1 travis admin 2140532053 Mar 21 12:45 GOPR1522.MP4
-rw------- 1 travis admin 1649133454 Mar 21 13:00 GOPR1523.MP4
-rw------- 1 travis admin 1691562945 Mar 21 12:21 GP011520.MP4
/Volumes/Scratch/Raw/2016-03-21/GoProTravisR:
total 31941008
-rw------- 1 travis admin 4001129586 Mar 21 12:04 GOPR7754.MP4
-rw------- 1 travis admin 2166255754 Mar 21 12:31 GOPR7755.MP4
-rw------- 1 travis admin 3202301883 Mar 21 12:45 GOPR7756.MP4
-rw------- 1 travis admin 2466803806 Mar 21 12:08 GOPR7757.MP4
-rw------- 1 travis admin 4001257192 Mar 21 11:27 GP017754.MP4
-rw------- 1 travis admin 516025454 Mar 21 11:29 GP027754.MP4
ctime is the "time of last status change", which I believe is the time the inode was last modified. It is NOT the file's creation time[1]. ls lists the file modification time, so simply change from using ctime to using mtime.
Historically, the time at which a file was created wasn't tracked by file systems used on unix file systems. Some newer file systems track it, but I am unsure how to access it (nor is it needed here).
We are trying to convert the output received from the below code
The current output is in this form
testingwindows,1446727960,1446728560,kkulka11,testingwin
testingwindows1,1446727160,141228560,kkulka11,testingwin
testingwindows2,1446727120,1446728560,kkulka11,testingwin
testingwindows3,1446727960,1446728560,kkulka11,testingwin
The output required is something like
testingwindows from Fri Oct 3 13:51:05 2015 GMT to Mon Nov 9 13:51:05 2015 GMT by kkulka11 for testingwin.
testingwindows1 from Fri Oct 2 13:51:05 2015 GMT to Mon Nov 9 13:51:05 2015 GMT by kkulka11 for testingwin.
testingwindows2 from Fri Oct 2 13:51:05 2015 GMT to Mon Nov 9 13:51:05 2015 GMT by kkulka11 for testingwin.
testingwindows3 from Fri Oct 12 13:51:05 2015 GMT to Mon Nov 9 13:51:05 2015 GMT by kkulka11 for testingwin.
This is my current code
if ( $COMMAND eq 'queryone' ) {
my $msend_query = "$MCELL_HOME\\bin\\mquery";
my #args_query = (
$msend_query,
"-q",
"-c", "$MCELL_HOME\\etc\\mclient.conf",
"-n", "$CS_BLACKOUT_CELL",
"-d",
"-f", "csv",
"-a", "CS_EMB_GBF_BLACKOUTS" ,
"-s", "blackout_host,start_timestamp,stop_timestamp,userid,reason",
"-w", "blackout_host: == '${BLACKOUTHOST}'"
);
system(#args_query);
We tried using perl -pe 's/(\d{10})/gmtime($1)/e'; but not able to convert and it gives this error
'o~}go⌂⌂t⌂x⌂w' is not recognized as an internal or external command,
operable program or batch file.
when we used the code as
if ( $COMMAND eq 'queryone' ) {
my $msend_query = "$MCELL_HOME\\bin\\mquery";
my $mqt = "$MCELL_HOME\\mqt.pl";
my #args_query = (
$msend_query,
"-q",
"-c", "$MCELL_HOME\\etc\\mclient.conf",
"-n", "$CS_BLACKOUT_CELL",
"-d",
"-f", "csv",
"-a", "CS_EMB_GBF_BLACKOUTS",
"-s", "blackout_host,start_timestamp,stop_timestamp,userid,reason",
"-w", "blackout_host: == '${BLACKOUTHOST}'"
) | $mqt;
system(#args_query);
Needed experts quick help and guidance to achieve the output in human-readable format.
Edit:
Updated the code as per Jacob comments but still not received the output as desired. Please suggest
if ( $COMMAND eq 'queryone' ) {
my $msend_query = "$MCELL_HOME\\bin\\mquery";
my #args_query = (
$msend_query,
"-q",
"-c", "$MCELL_HOME\\etc\\mclient.conf",
"-n", "$CS_BLACKOUT_CELL",
"-d",
"-f", "csv",
"-a", "CS_EMB_GBF_BLACKOUTS" ,
"-s", "blackout_host,start_timestamp,stop_timestamp,userid,reason",
"-w", "blackout_host: == '${BLACKOUTHOST}'"
);
chomp;
my #parts = split(/,/, system(#args_query));
$parts[1] = localtime($parts[1]);
$parts[2] = localtime($parts[2]);
printf("%s from %s to %s by %s for %s\n", #parts);
}
Output:
M:\AbhayBackup\PerlKK>test.pl -q -h testingwin
testingwin
sub: testingwin
testingwin,1446727960,1446728560,kkulka11,testingwin
0 from Thu Jan 1 05:30:00 1970 to Thu Jan 1 05:30:00 1970 by for
while (<DATA>) {
chomp;
my #parts = split(/,/, $_);
$parts[1] = localtime($parts[1]);
$parts[2] = localtime($parts[2]);
printf("%s from %s to %s by %s for %s\n", #parts);
}
__DATA__
testingwindows,1446727960,1446728560,kkulka11,testingwin
testingwindows1,1446727160,141228560,kkulka11,testingwin
testingwindows2,1446727120,1446728560,kkulka11,testingwin
testingwindows3,1446727960,1446728560,kkulka11,testingwin
Output:
testingwindows from Thu Nov 5 05:52:40 2015 to Thu Nov 5 06:02:40 2015 by kkulka11 for testingwin
testingwindows1 from Thu Nov 5 05:39:20 2015 to Sun Jun 23 07:09:20 1974 by kkulka11 for testingwin
testingwindows2 from Thu Nov 5 05:38:40 2015 to Thu Nov 5 06:02:40 2015 by kkulka11 for testingwin
testingwindows3 from Thu Nov 5 05:52:40 2015 to Thu Nov 5 06:02:40 2015 by kkulka11 for testingwin
Edit: based on your most recent update to the question, I now believe that you're trying to capture the output of the command and process it. Since you haven't provided a minimal, complete, and verifiable example, and because I have no idea what mquery is and you haven't provided an explanation for that, either, I present to you this guess:
if ($COMMAND eq 'queryone') {
my #lines = `$MCELL_HOME\\bin\\mquery -q -c $MCELL_HOME\\etc\\mclient.conf -n $CS_BLACKOUT_CELL -d -f csv -a CS_EMB_GBF_BLACKOUTS -s blackout_host,start_timestamp,stop_timestamp,userid,reason -w blackout_host: == '${BLACKOUTHOST}'`;
for (#lines) {
chomp;
my #parts = split(/,/, $_);
$parts[1] = localtime($parts[1]);
$parts[2] = localtime($parts[2]);
printf("%s from %s to %s by %s for %s\n", #parts);
}
}