简体   繁体   中英

How to delete multiple columns using multiple patterns?

I have a file like this (delimited by \t):

gene1 previous name1
gene2 previous name2
gene3 previous name3
gene4 previous name4

I want to delete the columns that contain gene2 and gene4 in the first column. I know that I can search multiple patterns using sed or awk and a |but in reality my file have thousand of lines and I want to delete hundred of columns (I have a variable with the patterns I want to search for). How can I do this without having to write manually all the patterns?

Pattern variable:

gene2
gene4

Expected output:

gene1 previous name1
gene3 previous name3

I only want to grep the first column because the word gene2 (or *gene4) could be in the third column.

Use grep :

cat > in_file <<EOF
gene1 previous name1
gene2 previous name2
gene3 previous name3
gene4 previous name4
EOF

cat > pat_file <<EOF
gene2
gene4
EOF

grep -v -f pat_file in_file

Output:

gene1 previous name1
gene3 previous name3

Here, grep uses the following options:
-v : Print lines that do not match.
-f file : Obtain patterns from file , one per line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM