0

I am trying to do a list of name:

list = a, b, c, d, e
sed "/$list/d" output_file.txt

so that lines containing those variables a,b,c,d,e would be excluded from the output_file

Anyone please help

2 Answers 2

4

With GNU grep:

list=(a b c d e)
(IFS="|"; grep -vE "(${list[*]})" file)

or with GNU sed:

list=(a b c d e)
(IFS="|"; sed -E "/(${list[*]})/d" file)
Sign up to request clarification or add additional context in comments.

2 Comments

It looks like the output shown on the terminal when I run the script is correct but then when I open the file that I generated with my code alone with this one it still does not exclude the list
Try this: list=(a b c d e); (IFS="|"; grep -vE "(${list[*]})" input_file > output_file)
2

You can use a BASH array to generate a regex with pipe (alternation):

list=(a b c d e) # original array

printf -v re "%s|" "${list[@]}" # genera a regex delimited by |

sed -E "/${re%|}/d" file # use sed on this regex

1 Comment

To save changes inline for sed use: sed -E -i.bak "/${re%|}/d" file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.