Daichi42
Daichi42

Reputation: 5

Bash - Replace random text between commas

I'm struggling to find a way to replace a random text between the second and third occurence commas on Linux bash. The original text looks like this:

RANDOMTEXT,RANDOMTEXT, >RANDOMTEXT< ,RANDOMTEXT,RANDOMTEXT

The bold string is what I wanted to replace. I've tried various things with sed and awk but nothing worked.

Upvotes: 0

Views: 221

Answers (3)

MadSquirrel
MadSquirrel

Reputation: 41

You do something like that it not the best solution but you can inspired it

#!/bin/bash
IFS=','
j=0
for i in $(echo "RANDOMTEXT,RANDOMTEXT,RANDOMTEXT,RANDOMTEXT,RANDOMTEXT"); do
  j=$(($j+1));
  if [ $j -eq 3 ]; then
    printf "nop,";
  fi
  printf "$i,"
done

Upvotes: 0

ntj
ntj

Reputation: 171

sed 's/RANDOMTEXT/SOME_NEW_TEXT/3'

You can specify which occurence you want to replace with a number at the end of the substitute expression.

Upvotes: 0

Phylogenesis
Phylogenesis

Reputation: 7880

Something like:

awk -F, '{ OFS=","; $3 = "Text to replace"; print }'

Upvotes: 1

Related Questions