John Smith
John Smith

Reputation: 1876

add text at the end of specific line

I have a yaml file like this:

spec:
  values:
    image: xxxx.dkr.ecr.eu-west-1.amazonaws.com/xxxx:mypr-ij4uhtuh3

I'm finding adding this text: # {"$imagepolicy": "xxx:xxx-test-pr333" } at the end of the line containing the word image surprisingly hard.

I've tried with sed, awk and ruby but I can't get it straight, sed is especially confusing with all those special characters and spaces.

the end result should be this:

spec:
  values:
    image: xxxx.dkr.ecr.eu-west-1.amazonaws.com/xxxx:mypr-ij4uhtuh3 # {"$imagepolicy": "xxx:xxx-test-pr333" }

thanks for any hint.

Upvotes: 1

Views: 366

Answers (5)

ufopilot
ufopilot

Reputation: 3975

$ str='# {"$imagepolicy": "xxx:xxx-test-pr333"}'
$ awk -F"\t" -v s="$str" '/^[ ]+image:/{$++NF=s}1' input_file 
spec:
  values:
    image: xxxx.dkr.ecr.eu-west-1.amazonaws.com/xxxx:mypr-ij4uhtuh3 # {"$imagepolicy": "xxx:xxx-test-pr333"}

Match image: then add new column $++NF=s.

The content of s is $str declared with -v s="$str"

Upvotes: 0

Daweo
Daweo

Reputation: 36360

I would harness GNU AWK for this task following way, let file.txt content be

spec:
  values:
    image: xxxx.dkr.ecr.eu-west-1.amazonaws.com/xxxx:mypr-ij4uhtuh3

then

awk '{print $0 (/image/?" # {\"$imagepolicy\": \"xxx:xxx-test-pr333\" }":"")}' file.txt

gives output

spec:
  values:
    image: xxxx.dkr.ecr.eu-west-1.amazonaws.com/xxxx:mypr-ij4uhtuh3 # {"$imagepolicy": "xxx:xxx-test-pr333" }

Explanation: I used so-called ternary operator (condition?valueiftrue:valueiffalse) so if current line do contain /image/ I print concatenation of current line ($0) and given string otherwise concatenation of current line with empty string ("") that is unchange line. Note that " (and only ") needs to be escaped (\) as they used for delimiting strings in GNU AWK.

(tested in gawk 4.2.1)

Upvotes: 2

RARE Kpop Manifesto
RARE Kpop Manifesto

Reputation: 2807

awk one-liner solution - tested and confirmed working on mawk 1.3.4, gawk 5.1.1, and macos nawk.

No matter how many times "image" shows up within one line, OFS (now set to the desired padding text) would only be appended once. And since FS is the full line ^$, lines could only have NF value of 0 (zero byte line) or 1 (everything else), so none of the existing spaces and tabs would get distorted.

  • One extra bonus is that the way it's written, it could even be used in gawk-unicode mode to safely append text to "lines" within purely binary files (add RT check for files not ending with \n), if there's ever such a need.

INPUT

spec:
  values:
    image: xxxx.dkr.ecr.eu-west-1.amazonaws.com/xxxx:mypr-ij4uhtuh3
 

CODE

{m,n,g}awk 'NF+=/image/' FS='^$' OFS=' # {"$imagepolicy": "xxx:xxx-test-pr333" }'

OUTPUT

spec:
  values:
    image: xxxx.dkr.ecr.eu-west-1.amazonaws.com/xxxx:mypr-ij4uhtuh3 # {"$imagepolicy": "xxx:xxx-test-pr333" }

Upvotes: 0

potong
potong

Reputation: 58351

This might work for you (GNU sed):

sed 's/^\s*image:.*/&  # {"$imagepolicy": "xxx:xxx-test-pr333" }/' file

Match a line that following some whitespace begins image: and append # {"$imagepolicy": "xxx:xxx-test-pr333" }/ to it.

Upvotes: 2

Barmar
Barmar

Reputation: 780688

This will do it:

sed '/image/s/$/ # {"$imagepolicy": "xxx:xxx-test-pr333" }/' file.yaml

The address /image/ matches a line containing the word image. The regexp $ matches the end of the line, and this is replaced with the string you want to add, in order to append to the line.

Upvotes: 2

Related Questions