linux - Can I declare variables/arrays inside a sed script? -



linux - Can I declare variables/arrays inside a sed script? -

i wondering if can declare variables within sed script. i.e. interpreter sed have concept of variables c, awk, python ... i'm not talking abt passing shell variables sed script.

e.g. next awk script supposed mask names of real customers replacing them tom dick or harry in sequence.

awk -f: ' begin{ ar[1]='tom'; ar[2]='dick'; ar[3]='harry' } /name:/ { print $1 ":" ar[i] if (i == 3) = 1 else = + 1; } ' customers.txt

can declare variables i or ar in sed script?

like commenter said, there rudimentary build called hold space can pile on info , pull off, easier if utilize bash function case statement or if want intuitive can utilize shell expansion , if don't need iterate sequentially can utilize $random:

sed "s/name:.*/name: $([[ $(($random%3)) -eq 0 ]] && (echo -n "tom") || ([[ $(($random%3)) -eq 1 ]] && echo "dick") || echo "harry")/" customers.txt

linux variables awk sed scripting

Comments

Popular posts from this blog

php - How to pass multiple values from url -

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

database - php search bar when I press submit with nothing in the search bar it shows all the data -