Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

awk - How to delete multiple columns using multiple patterns?

I have a file like this (delimited by ):

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.

question from:https://stackoverflow.com/questions/65890994/how-to-delete-multiple-columns-using-multiple-patterns

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...