[英]Reading from file in Linux
I have to read from file and assign values to variables. 我必须从文件读取并将值分配给变量。 Then I have to save them in another file.
然后,我必须将它们保存在另一个文件中。 I know how to do the second and third part but I don't know how to read from file.
我知道如何做第二部分和第三部分,但是我不知道如何从文件中读取。
The sample file looks like this. 示例文件如下所示。 how do I eat up unwanted things like Points, newline and store values somewhat like a = 21, b = 8 and so on.
我该如何吃掉点,换行符之类的不需要的东西,并存储一些类似于a = 21,b = 8等的值。
(Please note there is no empty line after A 21. Assume it as A 21\\nB 8 and so on) (请注意,A 21之后没有空行。假定它为A 21 \\ nB 8,依此类推)
Points:
A 21
B 8
C 2
D 13
E 24
You can check if the variables are empty. 您可以检查变量是否为空。 Something like this:
像这样:
while read -r var val; do
[[ ! -z $var && ! -z $val ]] && echo "$var=$val"
done < file
$ cat file
Points:
A 21
B 8
C 2
D 13
E 24
$ while read -r var val; do [[ ! -z $var && ! -z $val ]] && echo "$var=$val"; done < file
A=21
B=8
C=2
D=13
E=24
To answer your question in comments… 要在评论中回答您的问题…
$ . <(while read -r var val; do [[ ! -z $var && ! -z $val ]] && echo "$var=$val"; done < file)
$ echo $A
21
$ echo $B
8
也许我在想这个,但这样做不行吗?
sed 's/ / = /' file > newfile
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.