Bash remove substring in file from string

bash remove substring from middle of string
bash remove pattern from string
sed replace line in file
how to delete a word from a file using shell scripting
remove portion of string bash
sed replace character
remove text with sed
remove from file

I've one string like this:

myString='value1|value57|value31|value21'

and I've a file, called values_to_remove.txt containing a list of values, one per line, in this way

values_to_remove.txt

value1
value31

In bash, how can I remove the values contained in "values_to_remove.txt" from the string, taking into account that the values are separated by pipe and of course if I remove a value I have to removee also the preceding and the following pipe if any.

I've achieved this in python and called the python script from bash, but I need to do this directly in bash with one line command, rather than small script, otherwise I can already use my little python script.

That's the python code

myString = 'value1|value2|value3|value4'
arrString = myString.split("|")

with open("myfile.txt", encoding="utf-8") as file:
   for l in file:
       if  l in arrString:
           arrString.remove(l)

myNewString = "|".join(arrString)

Note that: the values separeted by pipe can be anything string.

Thank you


Remove occurrences of string in text file, sed -i -e 's/goodbye//g' filename. To delete multiple words: sed -i -e 's/\(goodbye\|​hello\|test\|download\)//g' filename. To remove characters from the starting and end of string data is called trimming. There is a built-in function named trim() for trimming in many standard programming languages. Bash has no built-in function to trim string data.


Here is a bash solution (The if statement is a runtime optimization to skip the repacement in case of no match, thanks @Inian):

for val in value1 value31; do
    if [[ "$mystring" =~ \|$val|$val\| ]]; then
        mystring=${mystring/$BASH_REMATCH/}     
    fi
done

This looks in pure bash for the first regular expression that matches either |value or value| and removes it. Note you can match both at the same times because then you will delete too many separators. If there is a chance there are no separators you need to use ? after each pipe (maybe just the second one is enough).

You can also avoid regular expressions and just attempt to delete both a prior and a posterior pipe:

for val in value1 value31; do 
    mystring=${mystring/|$val/};
    mystring=${mystring/$val|/}; 
done

All of these can be written on one line if you really need to:

 for val in value1 value31; do [[ "$mystring" =~ \|$val|$val\| ]]; mystring=${mystring/$BASH_REMATCH/}; done

Remove substring from front and back of variable, I don't think that's possible (but would love to be proved wrong). However, you can use an intermediate variable. The bash -c run by -exec is just a bash instance,  I want to remove a substring from a string. If I use the "tr" command, it doesn't seem to to what I want it to: echo './somestring.dat' | The UNIX and Linux Forums Removing substring from a string


Remove exact matching word using sed, In the above string, you don't need start-of-word/end-of-word markers and you may use: echo 'foggy light' | sed 's/foggy//g'. For the additional  linux - running a process and tailing a file simultaneously bash,shell,tail I want a run a long task on a remote machine (with python fabric using ssh). It logs to a file on the remote machine. What I want to do is to run that script and tail (actively display) the log file content until the script execution ends. The problem Substring of a file


How to use sed to find and replace text in files in Linux / Unix shell , Find and replace text within a file using sed command Our syntax is correct but the / delimiter character is also part of word1 and In this example only find word 'love' and replace it with 'sick' if line content a specific string such as FOO: Display Text Between Two Strings or Words · sed Delete / Remove  Bash string manipulation guide. Use substring to manipulate strings with ease. Find the length of a string, use the index command to find a position of a character within a substring. Bash substring examples.


Manipulating Strings, Bash supports a surprising number of string manipulation operations. Extracts $length characters of substring from $string at $position. conversion to new filename. rm -f $file # Delete original files after converting. echo "$filename. Replace one substring with another, in a string; Delete substring from a string; Remove all occurences of a substring in a string; Remove substring only if its a prefix to string; Remove substring only if its a suffix to string; Check if substring exists in string; Convert string to uppercase; Convert string to lowercase; Convert the first character to upperase


[PDF] grep, awk and sed – three VERY useful command-line utilities Matt , string. This is useful if you are searching through multiple files for the same containing the dollar character '$' then you cannot do grep '$' a_file as the wanted to trim a file and only operate on every 3rd line for instance, you could do this:. sed -i 's/\.out//g' input_file And this will remove any occurrence of the substring .out from that file. How to check if a string contains a substring in Bash. 2113.