如何使用 sed 或 grep 从一个文件到另一个文件提取文本?

How to extract text from file to file using sed or grep?

我的示例字符串在 txt 文件中 /www/meteo/last.txt:

a:3:{i:0;s:4:"6.13";i:1;s:5:"19.94";i:2;s:5:"22.13";}

我想从那个文件中逐行获取第 3 行的数字到一个新文件中。 (这些值是温度,因此它们会随时间变化 - 每 10 分钟)

新文件 /www/meteo/new.txt:(逐行)

 6.13 
 19.94
 22.13

试试这个 awk 方法

awk -F'"' 'BEGIN{OFS="\n"} {print ,,}' last.txt  > new.txt

输出:

cat new.txt
6.13
19.94
22.13

或者,如果您想使用 sedgrep

sed -r 's/([^"]*)("[^"]*")([^"]*)/\n/g;s/"//g' /www/meteo/last.txt

grep -Eo '"[^"]*"' /www/meteo/last.txt | sed 's/"//g'

如果你想要一个特定的值,假设你可以使用引号中的第二个温度 sed

grep -Eo '"[^"]*"' /www/meteo/last.txt | sed -n '2p'