bash 使用 awk 和 for 循环逐行读取文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21401543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading file line by line using awk and for loop
提问by user1502952
File contains compiler generated warnings
文件包含编译器生成的警告
warnings.txt(with 8 lines)
warnings.txt(8 行)
Line1 "/Project/user/XYZ/gsoap/gsoap-2.8.9/gsoap/stdsoap2.cpp", line 1657: error #4296-D: arithmetic operation on boolean type
Line2{ return soap->count - soap->buflen + soap->bufidx - (soap->ahead != 0);
Line3
Line4
Line5 "/Project/user/XYZ/gsoap/gsoap-2.8.9/gsoap/stdsoap2.cpp", line 4136: warning #3348-D: declaration hides variable "err" (declared at line 3
Line6 700)
Line7 { int err = SSL_get_error(soap->ssl, r);
Line8 ^
I want to read this file line by line using awk as while loop takes much time to process
我想使用 awk 逐行读取此文件,因为 while 循环需要很多时间来处理
Two ways i tried:
我试过的两种方法:
for line in `awk '{print for line in `awk '{BEGIN{ORS="\n";}{print Line1 "/Project/user/XYZ/gsoap/gsoap-2.8.9/gsoap/stdsoap2.cpp", line 1657: error #4296-D: arithmetic operation on boolean type
Line2{ return soap->count - soap->buflen + soap->bufidx - (soap->ahead != 0);
Line3
Line4
Line5 "/Project/user/XYZ/gsoap/gsoap-2.8.9/gsoap/stdsoap2.cpp", line 4136: warning #3348-D: declaration hides variable "err" (declared at line 3
Line6 700)
Line7 { int err = SSL_get_error(soap->ssl, r);
Line8 ^
}' warnings.txt`
do
echo $line
done
}' warnings.txt`
do
echo $line
done
and,
和,
"/Project/user/XYZ/gsoap/gsoap-2.8.9/gsoap/stdsoap2.cpp",
line
1657:
error
#4296-D:
arithmetic
operation
on
boolean
type
{
return
soap->count
-
soap->buflen
+
soap->bufidx
-
(soap->ahead
!=
0);
"/Project/user/XYZ/gsoap/gsoap-2.8.9/gsoap/stdsoap2.cpp",
line
4136:
warning
#3348-D:
declaration
hides
variable
"err"
(declared
at
line
3
700)
{
int
err
=
SSL_get_error(soap->ssl,
r);
^
Expected Output:
预期输出:
Same as above(i.e. content of file line by line)
同上(即逐行文件内容)
while read -r line; do
echo "$line"
done < warning.txt
My output:
我的输出:
$ awk '1' warnings.txt
Line1 "/Project/user/XYZ/gsoap/gsoap-2.8.9/gsoap/stdsoap2.cpp", line 1657: error #4296-D: arithmetic operation on boolean type
Line2{ return soap->count - soap->buflen + soap->bufidx - (soap->ahead != 0);
Line3
Line4
Line5 "/Project/user/XYZ/gsoap/gsoap-2.8.9/gsoap/stdsoap2.cpp", line 4136: warning #3348-D: declaration hides variable "err" (declared at line 3
Line6 700)
Line7 { int err = SSL_get_error(soap->ssl, r);
Line8 ^
Thanks
谢谢
回答by helpermethod
Use the read
builtin instead
改用read
内置函数
ifsold="$IFS"
IFS="
"
for line in `cat warnings.txt` ; do
echo "line: $line"
done
IFS="$ifsold"
回答by Ed Morton
Here is how you use awk to read warnings.txt and re-produce the file as-is, per your posted question:
以下是您如何使用 awk 读取 warnings.txt 并根据您发布的问题按原样重新生成文件:
##代码##Obviously you could do that with cat
instead of awk. If you have anything else you want to do with the contents of that file, please do let us know (by updating your question with expected output and an explanation) so we can help you.
显然你可以用cat
awk 代替。如果您想对该文件的内容做任何其他事情,请告诉我们(通过使用预期输出和解释更新您的问题),以便我们为您提供帮助。
回答by Dominik Heidler
You could do that:
你可以这样做:
Code:
代码:
##代码##