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
449 views
in Technique[技术] by (71.8m points)

linux - How to use "grep -v" or something similar in the entire text except for the first column?

I am trying to manipulate a file lets say :

76ers23 Philadelphia 76ers announced today that
76ers24 Lakers announced today 
76ers25 blazers  plays today 
76ers26 celics announced today that
76ers27 Bonston has Day off
76ers28 Philadelphia 76ers announced today that
76ers29 the blazzers announced today that
76ers30 76ers Training day
76ers31 Philadelphia 76ers has a day off  today 
76ers32 Philadelphia 76ers  humiliate Lakers 
76ers33 celics announced today that

I want to remove all the entries containing the term 76ers from the second column so as to obtain:

 76ers24    Lakers announced today 
 76ers25    blazers  plays today 
 76ers26    celics announced today that
 76ers27    Bonston has Day off
 76ers29    the blazzers announced today that
 76ers33    celics announced today that

my issue here is that if I will use the grep -v "76ers" it returns null

I am looking to use the grep (or another command) in the second line only.

I found this complicate way but which is pretty much what I want, but I got an_at the beginning of the second column.

cat file|awk '{print $1}' >file1
cat file|awk '{$1="";print $0}'|tr -s ' ' | tr ' ' '_' >file2
paste file1 file2 |grep -v "_76ers"

I'm not a bash expert so I guess there will be an easier way for that. Thank you in advance!


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

1 Reply

0 votes
by (71.8m points)

Use a regular expression that skips over the first column.

grep -v '^[^ ]* .*76ers' file

[^ ]* matches everything up to the first space.


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

...