Awk
如何在復雜的線條上非常精確地匹配
我們有以下文件:
more test.txt export KAFKA_HEAP_OPTS="-Xmx8g -Xms8g" export KAFKA_JVM_PERFORMANCE_OPTS="-XX:MetaspaceSize=96m -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:G1HeapRegionSize=16M -XX:MinMetaspaceFreeRatio=50 -XX :MaxMetaspaceFreeRatio=80"
如何驗證線路:
export KAFKA_HEAP_OPTS="-Xmx8g -Xms8g"
和
export KAFKA_JVM_PERFORMANCE_OPTS="-XX:MetaspaceSize=96m -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:G1HeapRegionSize=16M -XX:MinMetaspaceFreeRatio=50 -XX :MaxMetaspaceFreeRatio=80"
存在於文件中 - test.txt
當然,我們可以
grep -q 'export KAFKA_HEAP_OPTS="-Xmx8g -Xms8g"' test.txt [[ $? -eq 0 ]] && echo "line in file" grep -q 'export KAFKA_HEAP_OPTS="export KAFKA_JVM_PERFORMANCE_OPTS="-XX:MetaspaceSize=96m -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:G1HeapRegionSize=16M -XX:MinMetaspaceFreeRatio=50 -XX :MaxMetaspaceFreeRatio=80"' test.txt [[ $? -eq 0 ]] && echo "line in file"
但是這種方法很笨拙
#! /bin/bash env - bash -c '. test.txt; [ -n "$KAFKA_HEAP_OPTS" ] && [ -n "$KAFKA_JVM_PERFORMANCE_OPTS" ]' if [ $? -eq 0 ]; then echo 'OK' else echo 'ERROR' fi