Skip to content
wordpress meta

title: bash-scan-text-block-reverse
date: '2020-04-12T08:49:27-05:00'
status: publish
permalink: /bash-scan-text-block-reverse
author: admin
excerpt: ''
type: post
id: 1578
    - Bash
tag: []
post_format: []

example finding a block.

start string and reverse search up to next string. then replacing a line inside the block

bash example

```bash $ cat



file="listener_test.yml" dir="./unregister" variable="bar"

codeuri_num=$(grep -n "CodeUri: $dir" $file | awk '{print $1}' FS=":") function_num=$(grep -n "Type: 'AWS::Serverless::Function'" $file | awk '{print $1}' FS=":") block=$(sed "${function_num},${codeuri_num}!d" $file)

echo if [[ "$block" == "AutoPublishCodeSha256" ]];then echo "found AutoPublishCodeSha256 so update" line=$(echo "${block}" | grep -n "AutoPublishCodeSha256") line_num=$(awk "/Auto/ && NR >= ${function_num} && NR <= ${codeuri_num} {print NR}" $file) newline="AutoPublishCodeSha256: $var" sed -i "${line_num}s/.*/ \ \ \ \ \ AutoPublishCodeSha256: $variable/" $file else echo "AutoPublishCodeSha256 not found so insert" #codeuri_num=$((codeuri_num+1)) sed -i "${codeuri_num} i \ \ \ \ \ \ AutoPublishCodeSha256: $variable" $file fi ````