how to count lines of code in a single file - php

I'd like to count the number of lines of code in individual files.
sloccount sums up the entire directory.
Is there a program that will return the LOC for a single PHP file?

According to the documentation, just use the --details option.

Try this:
find /path/of/your/project | xargs wc -l "{}" \;

Related

Shell script to grep in a range of files

I'm setting up a script which takes some user data with the read command. Using this data I need to search the file range and then do some filtering.
Here's how it is,
Enter fromtime
read fromtime
Enter totime
read totime
Enter the ID
read id
Initially I SSH into a server and then there I have a directory, Records with path cd home/report/records here, I have:
REC_201901020345.gz (yyyymmddhhmm)
REC_201901120405.gz
REC_201903142543.gz
and so on.
These files have data along with the $id.
When the user inputs $fromtime and $totime it will be of format yyyymmddhh . Here, I need to go to that range of files and then grep for the $id and display. For example:
If $fromtime is 2019010103 and $totime is 2019031425. I need to go to only those specific range of files that is REC_201901020345.gz, REC_201901120405.gz, REC_201903142543.gz and perform the grep to find the id entered by the user.
I have tried this using an if condition but it doesn't seem to work. I am new to writing scripts like these. There might be mistakes when I have described everything here. Sorry for the same.
source config.sh
Enter fromtime
read fromtime
Enter totime
read totime
Enter the ID
read id
ssh $user#$ip
cd /home/report/records
# <-- need to know what to add here as described here, to navigate to the
# <-- specific range $fromtime-$totime. Then the command to find id will be
zfgrep $id *.gz
The result should be only the the data with the id's in the specified range of .gz files.
Try below command.
echo -e "$(ls -1 REC_????????????.gz 2>/dev/null)\nREC_${fromtime}##\nREC_${totime}##" | sort | sed -n "/##/,/##/p" | sed '1d;$d' | xargs zfgrep -a "$id"
Explanation:
'fromdate' and 'todate' along with a ## (say marker) is appended to the output of ls.
Sorted the input, resulting in desired file names enclosed with marker.
Both sed, prints only lines between marker.
Last one is the command, supposed to be executed for each file name.
You can omit pipes and all next commands, starting from end, and see how output is building.
To get the list of files within the given range (fromtime, totime), the following shell script may be used:
declare -i ta
for file in REC*.gz
do
ta=$(echo "${file}" | grep -oP 'REC_\K(.*)(?=[[:digit:]]{2}.gz)')
if [ "${ta}" ] ; then
if [ ${ta} -le ${totime} -a ${ta} -ge ${fromtime} ] ; then
echo -e "${file}"
fi
fi
done

Have over 360 .php files, forgot to include footer.php in all of them. What's the fastest way to do this?

I am working on a project. I'm almost close to finished. I am using CodeIgniter as a framework and I have over 360 main files in my views folder. However, I only have included <?php include "includes/footer.php" ?> in about 3 functions in my controller. I have over 360 functions and 200 controllers.
What's the fastest way to include a footer file in every one of the pages?
I'd use something such as Notepad++. With regular expression matching, you can easily add that to the bottom of files:
Here are the settings I used to accomplish this.
The fastest (and most easily configurable) way to insert '' into alot of files (on linux command line) is sed:
find views/ -name '*.php' -exec sed -i.bak '$ i\<?php include "includes/footer.php" ?>' {} +
Explanation:
-i.bak means "in-place" edit and create backup copy with '.bak'
suffix. Any string append to -i causes backup file creation, and the string is appended to the backup filename. A bare -i causes only "in-place" editing and no backup file creation. Remove the -i disables in-place editing and causes output to std out.
$ means "match last line". You could replace $ with /PATTERN/, where PATTERN is a regex.
i\ means insert
The effect of this command is the literal string after the backslash, up to the last single quote, will be inserted before the last line of each file found by find.
It's a bit of a problem but you can maybe search and replace multiple files... e.g. if you are loading views and have your footer after the body then replace your previous view i.e. $this->load->view('home_view', $data); with the same string but then add your footer as well?
Notepad++ as suggested would be a good idea or:
Find and replace in multiple files
You could easily write a simple PHP script that: opens each file in the directory, appends the needed code to that file, and closes the file. You would then rinse and repeat for every file.
EDIT: Needed to provide more details for answer.
Some pseudocode below. Note its pythonic in style (since thats what I am familiar with but the general gist is the same).
def appendCode(file):
code = "<?php require(\"footer.php\"); ?>"
with open(file, 'a') as f:
#append code here to end of file.
f.append(f)
def readDir(dir):
if isDir(dir):
d = openDir(dir)
return d.allFiles()
def main():
dirs = ["dir1", "dir2"]
for dir in dirs:
files = readDir(dir)
for f in files:
appendCode(f)
Alternatively, you could use a one liner command prompt that have been posted here or linked to! I personally enjoy writing simple programs like this just for the fun of it.

how to search for a line match from all files recursively found in a directory

Using linux commands, is there any way to know which file has the given line of code from a given project directory?
All directories are traced recursively for all files in subdirectories as well.
say your project is in php and is in directory /var/www/projectDir/
and the line to be searched is public function getProjectData( to know where (in which file lying in which subdirectory ) getProjectData php function is defined?
You can use the shell :
grep "text string to search" directory-path -R
EDIT: adding -R for recursive search
or even better, use PHP :
$reflFunc = new ReflectionFunction('function_name');
print 'File :' $reflFunc->getFileName() . ' at line : ' . $reflFunc->getStartLine();`
use the command
find /var/www/projectDir/ -name '*.php'|xargs grep 'public function getProjectData('
the first portion
find /var/www/projectDir/ -name '*.php'
will search all the files which are having extension php and within directory /var/www/projectDir
the second portion
xargs grep 'public function getProjectData('
will search all the files found in first portion for public function getProjectData(.
xargs is used to consider the output of first portion as a standard input for second portion.
The symbol | named pipe will pipe the output to second portion
output
/var/www/projectDir/sub/directory/somephpfile.php: public function getProjectData($param1, $param2) {
now you can open the file and search for which line the content is defined using
(ctrl + F for gedit) or ( ctrl + W for nano)
or use any of your favorite editor.
I go with the below line. It helps me always,
find dirname -type f -print | xargs grep -i "pattern" > /tmp/samplefile
I know that is as an old question and currently most used IDEs provide such functionality, but when I started to learn programing, one of the first thing that i learned is to use:
grep -iRn "wanted string"
i : Ignore case distinctions in patterns and input data
R : For each directory operand, read and process all files in that directory, recursively, following all symbolic links.
n : Line-number
More info - see this

Get directories names and split them in PHP

I have two problems.
Problem number one: Linux: I want to get only last folder names using ls:
Wrong - /home/test/ok/ok1/
Right - ok1
Currently I'm using command:
ls -d /home/test/*/*/
Problem number two: PHP: I want all directories names (from command's output - point 1) save to separate variables, dividing them into numbers and letters.
For example we have 3 folders from the previous command:
ts2
OK4
oks67
I want each of them to write to the variable $foldername, and then receive two additional subvariables $foldername_1 (letter) and $foldername_2 (number)
Thanks in advance for any help! :)
To address your first problem, use basename
$ basename /home/test/ok/ok1/
ok1

Bash script - Recursive Find of New lines and Carriage returns at end of files

I have a website that is giving me all sorts of errors, I've ran a recursive script to check for BOM headers.. but how would I do the same thing to find carriage returns and line feeds at the end of a file?
I want to check over my codebase to make sure there aren't any files with extra lines hanging out
i guess you want to check (without remove/replace) the empty lines at the end of a file.
you can try :
awk '{a=$0;}END{if(!a)print FILENAME}' file
this will print the file name out if there is at least one empty line at the end of the file.
for Recursion, you could use find ... |xargs awk '...'
updated
ok, I made an example, so that you could test:
find . -iname "*.php"|xargs -n1 awk '{a=$0;}END{if(!a)print FILENAME}'
the above line will check all php files recursively based on your current directory, if there is at least one empty line at the end of a php file, print the filename.

Categories