Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Bash How to read a file line by line     - Oracle LONG RAW BLOB CLOB type of presentation (Database)

- Ubuntu install Vendetta Online 14.04 (Linux)

- Oracle study notes view (Database)

- C ++ multithreading and critical resource instance (Programming)

- Check the Linux server performance with ten or so commands in a minute (Server)

- The file name is garbled or deleted files with special characters under Linux (Linux)

- How to compile and install wxWidgets in Ubuntu / Debian / Linux Mint (Linux)

- Docker container plaintext password problem-solving way (Server)

- APF firewall installation and configuration under Linux (Linux)

- php for Linux the MySQL extension module installation and configuration (Database)

- Use the DBMS_SCHEDULER package to manage scheduled tasks (Database)

- Install Visual Studio Code in Ubuntu (Linux)

- Python regular expressions: how to use regular expressions (Programming)

- Tree Traversals Again (Programming)

- The traffic monitoring system: cacti (Linux)

- Getting Started with Linux: Nginx Web Server How to Block Specific User Agents (UA) (Server)

- Linux user status query, and to unlock locked user (Linux)

- Understanding the Linux load average on other UNIX-Like systems (Linux)

- MySQL concat function is SQL injection (Linux)

- Enterprise Hadoop cluster architecture - NFS installation (Server)

 
         
  Bash How to read a file line by line
     
  Add Date : 2017-08-31      
         
         
         
  How KSH or BASH shell reads a file line by line in a Linux or UNIX-like systems?

Under Linux, OSX, * BSD or Unix-like systems you can use while..do..done the bash loop to read a file line by line.

In Bash Unix or Linux shell reads a file line by line syntax

For bash, ksh, zsh and other shells syntax is as follows

while read -r line; do COMMAND; done Passed by the -r option to read stop command to prevent explain the backslash escapes.

Before read command to add IFS = option to prevent the leading and trailing whitespace characters are removed.

while IFS = read -r line; do COMMAND_on $ line; done < input.file
It is more suitable for human readable syntax:

#! / Bin / bash
input = "/ path / to / txt / file"
while IFS = read -r var
do
echo "$ var"
done < "$ input"
Example

Here are some examples:

#! / Bin / ksh
file = "/ home / vivek / data.txt"
while IFS = read line
do
# Display $ line or do somthing with $ line
echo "$ line"
done < "$ file"
In the same example, the bash shell:

#! / Bin / bash
file = "/ home / vivek / data.txt"
while IFS = read -r line
do
# Display $ line or do somthing with $ line
printf '% s \ n' "$ line"
done < "$ file"
You can also look at this the better:

#! / Bin / bash
file = "/ etc / passwd"
while IFS =: read -r f1 f2 f3 f4 f5 f6 f7
do
# Display fields using f1, f2, .., f7
printf 'Username:% s, Shell:% s, Home Dir:% s \ n' "$ f1" "$ f7" "$ f6"
done < "$ file"

Bash script: read the file and progressive output file

 

Bash script: Progressive reading text files and create a pdf file

My input file is as follows (faq.txt):

4 | http: //www.cyberciti.biz/faq/mysql-user-creation/ | Mysql User Creation: Setting Up a New MySQL User Account
4096 | http: //www.cyberciti.biz/faq/ksh-korn-shell/ | What is UNIX / Linux Korn Shell?
4101 | http: //www.cyberciti.biz/faq/what-is-posix-shell/ | What Is POSIX Shell?
17267 | http: //www.cyberciti.biz/faq/linux-check-battery-status/ | Linux: Check Battery Status Command
17245 | http: //www.cyberciti.biz/faq/restarting-ntp-service-on-linux/ | Linux Restart NTPD Service Command
17183 | http: //www.cyberciti.biz/faq/Ubuntu-linux-determine-your-ip-address/ | Ubuntu Linux: Determine Your IP Address
17172 | http: //www.cyberciti.biz/faq/determine-ip-address-of-linux-server/ | HowTo: Determine an IP Address My Linux Server
16510 | http: //www.cyberciti.biz/faq/unix-linux-restart-php-service-command/ | Linux / Unix: Restart PHP Service Command
8292 | http: //www.cyberciti.biz/faq/mounting-harddisks-in-freebsd-with-mount-command/ | FreeBSD: Mount Hard Drive / Disk Command
8190 | http: //www.cyberciti.biz/faq/rebooting-solaris-unix-server/ | Reboot a Solaris UNIX System
My bash script:

#! / Bin / bash
# Usage: Create pdf files from input (wrapper script)
# Author: Vivek Gite under GPL v2.x +
# ------------------------------------------------- --------
#Input File
_db = "/ tmp / wordpress / faq.txt"
#Output Location
o = "/ var / www / prviate / pdf / faq"
_writer = "~ / bin / py / pdfwriter.py"
# If file exists
if [[- f "$ _db"]]
then
# Read it
while IFS = '|' read -r pdfid pdfurl pdftitle
do
local pdf = "$ o / $ pdfid.pdf"
echo "Creating $ pdf file ..."
#Genrate Pdf file
$ _writer --quiet --footer-Spacing 2 \
--footer-left "nixCraft is GIT UL ++++ W +++ C ++++ M + e +++ d-" \
--footer-right "Page [page] of [toPage]" - footer-line \
--footer-font-size 7 - print-media-type "$ pdfurl" "$ pdf"
done < "$ _ db"
fi
 

Tip: read from bash variable

Let's see how all the following installed the php package in Debian or Ubuntu Linux, enter:

# I will assign the output to a variable called $ list in #
list = $ (dpkg --list php \ * | awk '/ ii / {print $ 2}')
printf '% s \ n' "$ list"
Sample output:

php-pear
php5-cli
php5-common
php5-fpm
php5-gd
php5-json
php5-memcache
php5-mysql
php5-readline
php5-suhosin-extension
Now you can see them from the $ list and install these packages:

#! / Bin / bash
# BASH can iterate over $ list variable using a "here string" #
while IFS = read -r pkg
do
printf 'Installing php package% s ... \ n' "$ pkg"
/ Usr / bin / apt-get-qq install $ pkg
done <<< "$ list"
printf '*** Do not forget to run php5enmod and restart the server (httpd or php5-fpm) *** \ n'
Sample output:

Installing php package php-pear ...
Installing php package php5-cli ...
Installing php package php5-common ...
Installing php package php5-fpm ...
Installing php package php5-gd ...
Installing php package php5-json ...
Installing php package php5-memcache ...
Installing php package php5-mysql ...
Installing php package php5-readline ...
Installing php package php5-suhosin-extension ...
*** Donot forget to run php5enmod and restart the server (httpd or php5-fpm) ***
     
         
         
         
  More:      
 
- Gitlab installation under CentOS 7 (Linux)
- Build your own Python coding environment (Linux)
- Iptables command in detail (Linux)
- Linux remote wake the computer original code [C] (Linux)
- Bash difference in single quotes and double quotes (Programming)
- Java garbage collection and heap memory layout (Programming)
- Struts2 Result Types (Programming)
- Some Linux networking tools you might not know (Linux)
- Solve the compatibility problem between Linux and Java at the source in bold font (Linux)
- RedHat Linux 7 build yum source server (Server)
- VNC configuration detailed analysis under Linux (Linux)
- Linux systems use logwatch log file monitoring (Linux)
- Linux system components Detailed log (Linux)
- CoreOS Linux introduces Kubernetes kubelet (Server)
- Ubuntu derivative version of the user and how to install SmartGit / HG 6.0.0 (Linux)
- Linux User Rights Study Notes (Linux)
- MongoDB 3.0 New Features (Database)
- Python 3 for instructions encoded string conversion (Programming)
- Elixir: the future of programming languages (Programming)
- IPTABLES configuration steps under Linux (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.