How to read a file line by line?
2014-04-18 17:56
351 查看
1.Example 1.Tips
2.Bonus
2.Initiate a Loop
3.See also: While read LINE
This article introduces the concept of playing a file line by line in Linux with the help of examples and tips along with a guided tour of initiating a loop. The article discusses the errors committed while reading a file line by line on the Linux platform.
With samples and illustrations, it shows how the 'for loop' and 'while loop' differ in their respective outputs. It also provides tips on how to use the while loop and depicts its syntax. It concludes with the process behind initiating a loop along with the
side effects the while loops can exhibit.
One of the most common errors when using scripts bash on GNU / Linux is to read a file line by line by using a for loop (for line in $ (cat file.txt) do. ..), which in this example leads to an assessment for each line and not every word of the file.
It is possible to change the value of the variable $ IFS (Internal Field Separator, internal field separator) with a for loop before starting the loop.
Sample output with a for loop:
for line in $ (cat file.txt) do echo "$ line" done
This
is
row
No
1
This
is
row
No
2
This
[...]
The solution is to use a while loop coupled with the internal read.
It is possible to get the result with a for loop provided to change the value of the variable $ IFS (Internal Field Separator, internal field separator) before starting the loop.
While loop
The while loop remains the most appropriate and easiest way to read a file line by line.
Syntax
while read line
do
command
done <file
Example
The starting file:
This is line 1
•This is line 2
•This is line 3
•This is line 4
•This is line 5 •
The instructions in the command line:
while read line; do echo -e "$line\n"; done < file.txt
or in a "bash" script:
#! / bin / bash
while read line
do
echo-e "$ line \ n"
done <file.txt
The output on the screen (stdout):
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
Tips
It is entirely possible from a structured file (like an address book or /etc/passwd, for example), to retrieve the values of each field and assign them to several variables with the command 'read'. Be careful to properly assign the IFS variable with good
field separators (space by default).
Example:
#! /bin/bash
while IFS=: read user pass uid gid full home shell
do
echo -e "$full :\n\
Pseudo : $user\n\
UID :\t $uid\n\
GID :\t $gid\n\
Home :\t $home\n\
Shell :\t $shell\n\n"
done < /etc/passwd
Bonus
while read i; do echo -e "Parameter : $i"; done < <(echo -e "a\nab\nc")
Initiate a Loop
Although the while loop is the easiest method, it has its side effects. It obliterates the formatting of lines including spaces and tabs. •Moreover, the for loop coupled with a change of IFS helps keep the structure of the document output.
Syntax
old_IFS=$IFS # save the field separator
IFS=$'\n' # new field separator, the end of line
for line in $(cat fichier)
do
command
done
IFS=$old_IFS # restore default field separator
2.Bonus
2.Initiate a Loop
3.See also: While read LINE
This article introduces the concept of playing a file line by line in Linux with the help of examples and tips along with a guided tour of initiating a loop. The article discusses the errors committed while reading a file line by line on the Linux platform.
With samples and illustrations, it shows how the 'for loop' and 'while loop' differ in their respective outputs. It also provides tips on how to use the while loop and depicts its syntax. It concludes with the process behind initiating a loop along with the
side effects the while loops can exhibit.
One of the most common errors when using scripts bash on GNU / Linux is to read a file line by line by using a for loop (for line in $ (cat file.txt) do. ..), which in this example leads to an assessment for each line and not every word of the file.
It is possible to change the value of the variable $ IFS (Internal Field Separator, internal field separator) with a for loop before starting the loop.
Sample output with a for loop:
for line in $ (cat file.txt) do echo "$ line" done
This
is
row
No
1
This
is
row
No
2
This
[...]
The solution is to use a while loop coupled with the internal read.
It is possible to get the result with a for loop provided to change the value of the variable $ IFS (Internal Field Separator, internal field separator) before starting the loop.
While loop
The while loop remains the most appropriate and easiest way to read a file line by line.
Syntax
while read line
do
command
done <file
Example
The starting file:
This is line 1
•This is line 2
•This is line 3
•This is line 4
•This is line 5 •
The instructions in the command line:
while read line; do echo -e "$line\n"; done < file.txt
or in a "bash" script:
#! / bin / bash
while read line
do
echo-e "$ line \ n"
done <file.txt
The output on the screen (stdout):
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
Tips
It is entirely possible from a structured file (like an address book or /etc/passwd, for example), to retrieve the values of each field and assign them to several variables with the command 'read'. Be careful to properly assign the IFS variable with good
field separators (space by default).
Example:
#! /bin/bash
while IFS=: read user pass uid gid full home shell
do
echo -e "$full :\n\
Pseudo : $user\n\
UID :\t $uid\n\
GID :\t $gid\n\
Home :\t $home\n\
Shell :\t $shell\n\n"
done < /etc/passwd
Bonus
while read i; do echo -e "Parameter : $i"; done < <(echo -e "a\nab\nc")
Initiate a Loop
Although the while loop is the easiest method, it has its side effects. It obliterates the formatting of lines including spaces and tabs. •Moreover, the for loop coupled with a change of IFS helps keep the structure of the document output.
Syntax
old_IFS=$IFS # save the field separator
IFS=$'\n' # new field separator, the end of line
for line in $(cat fichier)
do
command
done
IFS=$old_IFS # restore default field separator
相关文章推荐
- How to read file line by line?
- Shell Script Utility To Read a File Line By Line
- how to read file by ZipFile
- Expect script to read file line by line
- How To Read A Research Paper?---by Mubarak Shah
- How to Parse or Read XML File in Java >> XML Tutorial Example
- How to open/read/write a local file from an applet
- How to read Mac .DMG file in Linux or PC(II)
- How To Read XML File In Java – (SAX Parser)
- [Linux cmd line] How to convert a binary file into output in C include file style.
- How to create and execute JAR file in Java – Command line Eclipse Netbeans
- How to Read and Understand a Scientific Paper: A Step-by-Step Guide for Non-Scientists
- My Wiki: How to read a properties file in a web application
- How to read whole file? (C++)
- How to install oracle by Command Line Interface(CLI) in silent mode on Linux
- How to read file in Java – BufferedInputStream Example
- How to redirect the output of DBMS_OUTPUT.PUT_LINE to a file
- HOWTO: 使用 InternetReadFile 获取文件
- How To Trace The Remote File Server (RFS) Process In Physical Standby Database (Doc ID 1481125.1)
- How to remove a line from a file in java?