我想要一个
的shell脚本到目前为止(谢谢!)我已获得获取当前屏幕分辨率数据的命令:
system_profiler SPDisplaysDataType | awk '/Resolution/ {
print "screenwidth \""$2"\"";
print "screenheight \""$4"\"";
}'
应该写入的行分别以:
开头screenwidth "VALUE1"
screenheight "VALUE2"
如何将结果写入“VALUE”位置的文件?
(我是shell脚本世界的初学者)
答案 0 :(得分:2)
拨打awk
就足够了(并且grep
是不必要的):
system_profiler SPDisplaysDataType | awk '/Resolution/ {
print "screenwidth \""$2"\"";
print "screenheight \""$4"\"";
}'
答案 1 :(得分:2)
如果我理解你的话,
sys..|grep..|awk ..$2 is new widht
sys..|grep..|awk ..$4 is new height
您希望将旧文件中的value1 / 2替换为上面一行中的新值
-- old file --
screenwidth "VALUE1"
screenheight "VALUE2"
然后你可以一次性完成:
sys..|grep..|awk 'NR==FNR{w=$2;h=$4;next}/screenwidth/{$0="screenwidth \""w"\"";} /screenheight/{$0="screenheight \""h"\""}1' - oldfile
请参阅此测试示例:
#I simulate your sys..|grep.. with echo
kent$ cat old.txt
foo
screenwidth "VALUE1"
screenheight "VALUE2"
bar
kent$ echo "foo 200 bar 400"|awk 'NR==FNR{w=$2;h=$4;next}/screenwidth/{$0="screenwidth \""w"\"";} /screenheight/{$0="screenheight \""h"\""}1' - old.txt
foo
screenwidth "200"
screenheight "400"
bar