Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

count lines in files

find . -name '*.php' | xargs wc -l
Comment

read a file and count how many lines


# Let's use a text file called file.txt
# the file contains 5 lines of some programming languages in use today:

$ cat file.txt
#Output:
#	JavaScript
#	Java
#	C
#	Python
#	C#


# Method 1 'wc'
# 'wc' command can be used to find the number of lines, characters,
# words, and bytes of a file.

$ wc -l file.txt

# Or

$ wc -l < file.txt

# Or

$ wc -l file.txt

# Output:
#	10 file.txt

# OR

# Method 2 - 'sed'
# 'sed' is a stream editor that can be used to perform basic text transformation 
# of an input file.
# This command is mostly used for find-and-replace functionality and 
# can also be used to find the number of lines of a specified file.

$ sed -n '=' file.txt
# Output:
#	1
#	2
#	3
#	4
#	5

# Or 

# sed -n '$='  which gives the overall number of lines
$ sed -n '$=' file.txt
# Output:
#	5

Comment

python number of lines in file

num_lines = sum(1 for line in open('myfile.txt'))

# Notice: Problem with this solution is that it won't
# 		  count empty line if it is at end of file.
Comment

PREVIOUS NEXT
Code Example
Shell :: github activity graph 
Shell :: wsl --install 
Shell :: how to config username and email in git 
Shell :: how to run exe file with shell 
Shell :: git get project name 
Shell :: start fast api server 
Shell :: brew portkill 
Shell :: composer install debian 
Shell :: *15856 connect() to unix:/var/run/php/php8.0-fpm.sock failed (11: Resource temporarily unavailable) 
Shell :: bash get file full path 
Shell :: comprimir carpeta linux comando 
Shell :: edit file as root ubuntu 
Shell :: bash show contents of file 
Shell :: nano for windows 
Shell :: Use pip to install the EB CLI. 
Shell :: git unsafe repository 
Shell :: check vm ram details in linux 
Shell :: change owner for folder and subfolders linux 
Shell :: react icons installation through npm 
Shell :: why jupyter notebook suggestions not showing after upgrade 
Shell :: print colored text bash 
Shell :: docker install ubuntu 22.04 
Shell :: how to install spark on macos 
Shell :: install docker in kali linux 
Shell :: linux update command 
Shell :: bash switch case 
Shell :: Required Windows feature(s) not enabled : Hyper-V and Containers 
Shell :: awk columns 
Shell :: sudo shutdown 
Shell :: installing mongodb on m1 mac 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =