使用shell按行讀取檔案

2021-09-02 09:52:20 字數 469 閱讀 1745

在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

要按行讀取csv檔案時候,**如下

ifs=","

sed "1,3d" $ | while read col1, col2, col3, col4

do echo "$col1, $col2, $col3"

done

ifs=$oldifs

其中 sed "1,3d" 是刪除頭三行的意思, 和本文無直接關係。

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...

按行讀取檔案

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

shell指令碼按行讀取檔案並解析

shell指令碼讀取乙個配置檔案,配置檔案的格式如下 name abc pwd 123456 permission mop 檔名稱為 config.cfg 要在shell指令碼裡讀取這個配置檔案,並且把值一一賦給相應的變數,實現如下 while read line do name echo line...