photon
9/25/2018 - 5:56 AM

Tag line editing. Exclude lines that have existing "tag:".

Tag line editing. Exclude lines that have existing "tag:".

#!/bin/sh

# Purpose:
#     Exclude lines that have existing "tag:"
#     Find lines that contains target string "voc"
#     Insert new tags "tag::" to the matched lines
#     Use non-extended regex to improve compatibility

# Methods:
#     Use the backslash syntax \1 \2 \1d \2d ..
#     Basicallt \1d is the same as \1
#     \1 \2 are defined by small brackets \( \)
#     Up to nine capture groups can be used. \9.
#     https://stackoverflow.com/questions/4609949/what-does-1-in-sed-do

str="
voc
bbb voc
voc bbb
ccc
tag: voc
"

fpath="./f1.txt"
printf "$str" > $fpath

cat $fpath

echo

sed -e '/^tag/! s/^\(.*voc[[:space:]]\|.*voc$\)/tag:: \1/' -i $fpath
cat $fpath

rm $fpath