2

シェルスクリプトが欲しい

  1. 現在の画面解像度を取得します(解決済み)
  2. 解像度設定を含む行をファイルで検索し、
  3. ファイル内の古い解像度設定を現在の解像度設定に置き換えます

これまでのところ (ありがとう!) 現在の画面解像度データを取得するためのコマンドがあります。

system_profiler SPDisplaysDataType | awk '/Resolution/ {
    print "screenwidth \""$2"\"";
    print "screenheight \""$4"\"";
}'

それぞれに書き込む必要がある行は、次で始まります。

screenwidth "VALUE1"
screenheight "VALUE2"

結果をファイルの "VALUE" 位置に書き込むにはどうすればよいですか?

(私はシェルスクリプトの世界ではかなりの初心者です)

4

2 に答える 2

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
于 2013-02-07T18:14:09.087 に答える
2

への 1 回の呼び出しawkで十分grepです (不要です)。

system_profiler SPDisplaysDataType | awk '/Resolution/ {
    print "screenwidth \""$2"\"";
    print "screenheight \""$4"\"";
}'
于 2013-02-07T18:07:37.447 に答える