shell按行讀取檔案的3種方法

2022-09-26 17:06:11 字數 764 閱讀 5944

方法有很多,下面寫出三種方法:

寫法一:

複製** **如下:

#!/bin/bash

while read line

doecho $line

done < filename(待讀取的檔案)

寫法二:

複製** **如下:

#!/bin/bash

cat filename(待讀取的檔案) | while read line

doecho $line

done

寫法三:

複製** **如下:

for line in `cat filename(待讀取的檔案)`

dkfktwgbbo

echo $line

done

說明:fwww.cppcns.comor逐行讀和while逐行讀是有區別的,如:

複製** **如下:

$ cat file

1111

2222

3333 4444 555www.cppcns.com

$ cat file | while rea line; do echo $line; done

1111

2222

3333 4444 555

$ for line in $(1111

2222

3333

4444

555

本文標題: shell按行讀取檔案的3種方法

本文位址:

shell指令碼 按行讀取檔案

按行讀取檔案 bin bash echo 方法 1 while read line1 do echo line1 done 1 echo 方法 2 cat 1 while read line2 do echo line2 done echo 方法 3 for line3 in 1 do echo l...

使用shell按行讀取檔案

在shell裡面,按行讀取檔案得注意設定環境變數ifs,否則無法正確讀入。具體例子如下 oldifs ifs ifs n file home xx txt in file home in for pattern in cat do grep v e pattern done ifs oldifs要按...

按行讀取檔案

const string strurlfilename testurl.txt ifstream fin strurlfilename.c str fstream binary if fin fin.eof string serverurl getline fin,serverurl info lo...