简体   繁体   中英

How to replace a line contain something with that same thing ( LINUX )?

How to replace a line contain something with that same thing

for example i have line with random thing and contain ( email ) in that line and i want to replace all lines with the email that has it in that line for example - all emails will start with ( abuse@.* )

aaaaaaaaaaaaaa abuse@xx.com
-random lines with no email- 
bbbbbbbbbbbbbb abuse@sdds.net
-random lines with no email- 
cccccccccccccc abuse@sss.org
-random lines with no email-

my output must be

email: abuse@xx.com
-random txt with no email- 
email: abuse@sdds.net
-random txt with no email- 
email: abuse@sss.org
-random txt with no email-

so it search for line has ( abuse@.* ) for ex. abuse@xx.com get the email then delete that line and put email: abuse@xx.com instead of that line

in shell code

Would gsed -i (or just sed -i on linux) be of use?

$ cat substitute.txt 
aaaaaaaaaaaaaa abuse@xx.com
-random lines with no email- 
bbbbbbbbbbbbbb abuse@sdds.net
-random lines with no email- 
cccccccccccccc abuse@sss.org
-random lines with no email-


$ cat substitute.txt | gsed -s "s/.*\(abuse\)/email: \1/" 
email: abuse@xx.com
-random lines with no email- 
email: abuse@sdds.net
-random lines with no email- 
email: abuse@sss.org
-random lines with no email-

$  gsed -i.bak -s "s/.*\(abuse\)/email: \1/" substitute.txt     
$ cat substitute.txt
email: abuse@xx.com
-random lines with no email- 
email: abuse@sdds.net
-random lines with no email- 
email: abuse@sss.org
-random lines with no email-

Yes, you can do the same, mostly, just hold (h), hold with append (H), and then grab (g):

ubuntu@ip-10-0-11-161:~/dev$ cat substitute.txt  | sed -s "/\(.*\)\(abuse\)/{ h;s/.*\(abuse\)/email: \1/;H;g;}"
aaaaaaaaaaaaaa abuse@xx.com
email: abuse@xx.com
-random lines with no email- 
bbbbbbbbbbbbbb abuse@sdds.net
email: abuse@sdds.net
-random lines with no email- 
cccccccccccccc abuse@sss.org
email: abuse@sss.org
-random lines with no email-

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