Bash add increased number to string in a file
I have this properties file, which can have as many "spark-worker" strings in it:
[master]
spark-master ansible_ssh_host=ec2-3-236-23-XXX.compute-1.amazonaws.com
[worker]
spark-worker ansible_ssh_host=ec2-34-204-206-XXX.compute-1.amazonaws.com
spark-worker ansible_ssh_host=ec2-3-238-204-XXX.compute-1.amazonaws.com
.
.
spark-worker ansible_ssh_host=ec2-3-238-204-XXX.compute-1.amazonaws.com
[spark_cluster:children]
master
worker
And I wand to add a digit after every string "spark-worker" so the result would be:
[master]
spark-master ansible_ssh_host=ec2-3-236-23-XXX.compute-1.amazonaws.com
[worker]
spark-worker-1 ansible_ssh_host=ec2-34-204-206-XXX.compute-1.amazonaws.com
spark-worker-2 ansible_ssh_host=ec2-3-238-204-XXX.compute-1.amazonaws.com
.
.
spark-worker-n ansible_ssh_host=ec2-3-238-204-XXX.compute-1.amazonaws.com
[spark_cluster:children]
master
worker
What's the best to do it bash way?
An awk
one-liner:
awk '$1 == "spark-worker" { $1 = $1 "-" ++c } 1' file
Using sed
in a bash
script
#!/usr/bin/env bash
n=1
while read -r line; do
sed -i.bak "s/spark-worker/&-$n/" <<< "$line"
if [[ "$line" =~ "spark-worker" ]]; then
n=$(( n+1 ))
fi
done < input_file
Or as a one liner
$ n=1; while read -r line; do sed "s/spark-worker/&-$n/" <<< "$line"; if [[ "$line" =~ "spark-worker" ]]; then n=$(( n+1 )); fi; done < input_file
[master]
spark-master ansible_ssh_host=ec2-3-236-23-XXX.compute-1.amazonaws.com
[worker]
spark-worker-1 ansible_ssh_host=ec2-34-204-206-XXX.compute-1.amazonaws.com
spark-worker-2 ansible_ssh_host=ec2-3-238-204-XXX.compute-1.amazonaws.com
.
.
spark-worker-3 ansible_ssh_host=ec2-3-238-204-XXX.compute-1.amazonaws.com
[spark_cluster:children]
master
worker
Or using awk
awk -v work="spark-worker" '$0~work{n++; gsub(work,work"-"n)}1' input_file
[master]
spark-master ansible_ssh_host=ec2-3-236-23-XXX.compute-1.amazonaws.com
[worker]
spark-worker-1 ansible_ssh_host=ec2-34-204-206-XXX.compute-1.amazonaws.com
spark-worker-2 ansible_ssh_host=ec2-3-238-204-XXX.compute-1.amazonaws.com
.
.
spark-worker-3 ansible_ssh_host=ec2-3-238-204-XXX.compute-1.amazonaws.com
[spark_cluster:children]
master
worker