professorswag123 professorswag123 - 8 months ago 103
Linux Question

linux replace string with another string

I want to replace a string in a file.
I have a textfile which looks like this: id#string.

123456#some new string
234567#some other new string
345678#still some other new string

and then I have alot of folders based on the id of the textfile.

so now I want to replace the string in a textfile

{"fields":[{"t":"B","v":"false or true","n":"SomeText"},{"t":"S","v":"","n":"Keywords"},{"t":"S","v":"","n":"Author"},{"t":"S","v":"","n":"SomeText"},{"t":"S","v":"THI$ is THE old §TRING I want to REPLACE","n":"SomeText"},{"t":"S","v":"","n":"SomeText"},{"t":"A","n":"SomeText"},{"t":"A","v":"SomeInt","n":"SomeText"}]}

but i dont know how to do this since the newstring and or oldstring may contain additional characters

FOLDER=`echo 123456#string | cut -d# -f1`
NEWSTRING=`echo 123456#string | cut -d# -f2`
sed -i ??? $FOLDER/filename.txt

thanks for your help


of the issue is not clear what should be engaged and what replaced, but maybe you need is:

# t1 file contains your strings

ROW='{"fields":[{"t":"B","v":"false or true","n":"SomeText"},{"t":"S","v":"","n":"Keywords"},{"t":"S","v":"","n":"Aut
for S in $(cat ./t1); do
    F=$(echo $S | awk -F# '{print $1}')
    N=$(echo $S | awk -F# '{print $2}')
    if [ -z "$N" ]; then continue; fi
    NROW=$(echo ${ROW/THIS is THE old STRING I want to REPLACE/$S})
    NROW=$(echo ${NROW/Keywords/$N})
    NROW=$(echo ${NROW/false or true/$F})
    #mkdir $F
    #echo $NROW >> file.json
    echo $NROW