Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.

AWK: Print the Line Numbers in a Record Separator Block

61 ملاحظات
پہلے نہ پڑھے ہوئے پیغام پر جائیں

mkr...@gmail.com

نہ پڑھا ہوا،
14 فروری، 2018، 12:27:44 PM14/2/18
بنام
I have been scratching my head on this. I have a yaml file that I am editing to comment out a section of the yaml and then add a new section to it later in the script via the linenumber in the file. Because the yaml file is a bit sparse when is comes to key words I am doing a multi line search using awk. However this return the number of the block found and not the line number for the match.

Short of using a dedicated yaml editor is there a way to return the number of line and not the block?

thanks

Code:

awk 'BEGIN{FS="\n"; RS=""} $2~/.*type:.*rollingFile/ && $1~/.*file:/ {print NR}' logging.yml

returns 12 but the line is 44

Sample yaml

# you can override this using by setting a system property, for example -Des.logger.level=DEBUG
es.logger.level: INFO
rootLogger: ${es.logger.level}, console, file
logger:
# log action execution errors for easier debugging
action: DEBUG

# deprecation logging, turn to DEBUG to see them
deprecation: INFO, deprecation_log_file

# reduce the logging for aws, too much is logged under the default INFO
com.amazonaws: WARN
# aws will try to do some sketchy JMX stuff, but its not needed.
com.amazonaws.jmx.SdkMBeanRegistrySupport: ERROR
com.amazonaws.metrics.AwsSdkMetrics: ERROR

org.apache.http: INFO

# gateway
#gateway: DEBUG
#index.gateway: DEBUG

# peer shard recovery
#indices.recovery: DEBUG

# discovery
#discovery: TRACE

index.search.slowlog: TRACE, index_search_slow_log_file
index.indexing.slowlog: TRACE, index_indexing_slow_log_file

additivity:
index.search.slowlog: false
index.indexing.slowlog: false
deprecation: false

appender:
console:
type: console
layout:
type: consolePattern
conversionPattern: "[%d{ISO8601}][%-5p][%-25c] %m%n"

#file:
#type: dailyRollingFile
#file: ${path.logs}/${cluster.name}.log
#datePattern: "'.'yyyy-MM-dd"
#layout:
#type: pattern
#conversionPattern: "[%d{ISO8601}][%-5p][%-25c] %.10000m%n"

file:
type: rollingFile
file: ${path.logs}/${cluster.name}.log
maxBackupIndex: 30
maxFileSize: 10MB
layout:
type: simple

# Use the following log4j-extras RollingFileAppender to enable gzip compression of log files.
# For more information see https://logging.apache.org/log4j/extras/apidocs/org/apache/log4j/rolling/RollingFileAppender.html
#file:
#type: extrasRollingFile
#file: ${path.logs}/${cluster.name}.log
#rollingPolicy: timeBased
#rollingPolicy.FileNamePattern: ${path.logs}/${cluster.name}.log.%d{yyyy-MM-dd}.gz
#layout:
#type: pattern
#conversionPattern: "[%d{ISO8601}][%-5p][%-25c] %m%n"

deprecation_log_file:
type: dailyRollingFile
file: ${path.logs}/${cluster.name}_deprecation.log
datePattern: "'.'yyyy-MM-dd"
layout:
type: pattern
conversionPattern: "[%d{ISO8601}][%-5p][%-25c] %m%n"

index_search_slow_log_file:
type: dailyRollingFile
file: ${path.logs}/${cluster.name}_index_search_slowlog.log
datePattern: "'.'yyyy-MM-dd"
layout:
type: pattern
conversionPattern: "[%d{ISO8601}][%-5p][%-25c] %m%n"

index_indexing_slow_log_file:
type: dailyRollingFile
file: ${path.logs}/${cluster.name}_index_indexing_slowlog.log
datePattern: "'.'yyyy-MM-dd"
layout:
type: pattern
conversionPattern: "[%d{ISO8601}][%-5p][%-25c] %m%n"

Ben Bacarisse

نہ پڑھا ہوا،
14 فروری، 2018، 3:32:27 PM14/2/18
بنام
mkr...@gmail.com writes:

> I have been scratching my head on this. I have a yaml file that I am
> editing to comment out a section of the yaml and then add a new
> section to it later in the script via the linenumber in the
> file. Because the yaml file is a bit sparse when is comes to key words
> I am doing a multi line search using awk. However this return the
> number of the block found and not the line number for the match.
>
> Short of using a dedicated yaml editor is there a way to return the
> number of line and not the block?

You could count the lines in each block. In this case that just means
adding NF+1 to a counter after reading each field.

> awk 'BEGIN{FS="\n"; RS=""}
> $2~/.*type:.*rollingFile/ && $1~/.*file:/
> {print NR}' logging.yml
>
> returns 12 but the line is 44

I make it 52. Have I missed something?

I used:

awk 'BEGIN{FS="\n"; RS=""; LN=1}
$2~/.*type:.*rollingFile/ && $1~/.*file:/ {print LN}
{LN += NF+1}'

> Sample yaml

<snipped>
--
Ben.

mkr...@gmail.com

نہ پڑھا ہوا،
15 فروری، 2018، 2:17:56 PM15/2/18
بنام
Thanks that will work. The reason for line #52 was that I posted the fixed version of the log file and I said rollingFile but it should have been dailyRollingFile.

So this is the code ended up with.

awk 'BEGIN{FS="\n"; RS=""; LN=1} $2~/.*[ #]type:.*dailyRollingFile/ && $1~/.*[ #]file:/ {print LN} {LN += NF+1}' /etc/elasticsearch/logging.yml
0 نئے پیغامات