交換方法/別のファイルに一つのファイルからの行を追加します。

Sureshchandra Jarugula:

(私は、例えば以下のように一つのファイルがあると私はsystem_props始まるれる行をgrepするための要件を持っていますcat file1 | grep ^system_props)..

JAVA_HOME=`find "$AGENT_HOME/jre" -name release -type f 2>/dev/null | sed "s|/release||g"`

system_props="$system_props -sensu.controller.hostName=abc.nam.net"
system_props="$system_props -sensu.controller.port=8181"
system_props="$system_props -sensu.controller.node=Mcagent"

if [ -z "$JAVA_HOME" ]; then
   if [ -d "/opt/middleware" ]; then
      JAVA_HOME=`find /opt/middleware -type d -name jre 2>/dev/null | grep WebSphere | grep java | grep -v grep | sort | uniq`
   fi
fi

私は以下のようなダミーのコンテンツを持つと言うのFILE2と呼ばれる別のファイルを持っています。

JAVA_HOME=`find "$AGENT_HOME/jre" -name release -type f 2>/dev/null | sed "s|/release||g"`

system_props="$system_props -sensu.controller.hostName=testhost.net"
system_props="$system_props -sensu.controller.port=8080"

if [ -z "$JAVA_HOME" ]; then
   if [ -d "/opt/middleware" ]; then
      JAVA_HOME=`find /opt/middleware -type d -name jre 2>/dev/null | grep WebSphere | grep java | grep -v grep | sort | uniq`
   fi
fi

今、私の要件は、コンテンツ交換することであるcat file1 | grep ^system_propsとしcat file2 | grep ^system_props)

system_props線の期待される出力は、同じ配列の下FILE1にあるFILE2に添加されるべきです。

system_props="$system_props -sensu.controller.hostName=abc.nam.net"
system_props="$system_props -sensu.controller.port=8181"
system_props="$system_props -sensu.controller.node=Mcagent"
Hriavindershidargl3:

あなたは次のことを試してみてくださいでした。書かれたサンプルでテスト。

awk '
FNR==NR{
  if(match($0,/system_props="/)){
    val=(val?val ORS:"")$0
  }
  next
}
/^system_props="/{
  if(++count==1){
    print val
  }
next
}
1
'  Input_file1   Input_file2

説明:上記のコードの詳細な説明を追加します。

awk '                                ##Starting awk program from here.
FNR==NR{                             ##Checking condition FNR==NR which will be TRUE when Input_file1 is being read.
  if(match($0,/system_props="/)){    ##Checking condition if match for string system_props=" is found in current line then do following.
    val=(val?val ORS:"")$0           ##Creating variable val and keep appending current line value to its value here.
  }
  next                               ##next will skip all further statements from here.
}
/^system_props="/{                   ##Checking condition if line is starting from sting system_props=" then do following.
  if(++count==1){                    ##Checking condition if variable count is 1 then do following.
    print val                        ##Printing val variable here.
  }
  next                               ##next will skip all further statements from here.
}
1                                    ##1 will print edited/non-edited line here.
'  file1  file2                      ##Mentioning Input_file names here.

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=7375&siteId=1