简体   繁体   English

提取域然后使用sed / awk / grep / perl粘贴到同一行

[英]Extract domain then paste into the same line using sed/awk/grep/perl

I've started my tech adventure not so long ago - as you will feel from question - but now I'm stucked because after almost a whole day thinking and searching I don't know the proper solution for my problem. 不久前我开始了我的技术冒险 - 你会从问题中感受到 - 但现在我被困了,因为经过近一整天的思考和搜索,我不知道我的问题的正确解决方案。

Briefly, I got a file with thousand lines which contains email and firstname. 简而言之,我得到了一个包含千行的文件,其中包含电子邮件和名字。 The thing is I really need another column just with the domain name itself for example next to the email address. 问题是我真的需要另一个列,只有域名本身,例如电子邮件地址旁边。 Please take a look at the examples below. 请看下面的例子。

This is how it looks now: 这就是它现在的样子:

something@nothing.tld|:|george|-|
    anything@another.tld|:|thomas|-|
    third@address.tld|:|kelly|-|

How I wanted to look like: 我想怎么样:

something@nothing.tld|:|nothing.tld|--|george|-|
    anything@another.tld|:|another.tld|--|thomas|-|
    third@address.tld|:|address.tld|--|kelly|-|

My best guess was using sed to start the process and extract the domain but how can I paste that extracted domain within the same line that's where I stucked. 我最好的猜测是使用sed启动进程并提取域,但是如何将提取的域粘贴到我所在的同一行中。

sed -e 's/.*@\(.*\)|:|*/\1/'

If you could also give a short explanation along with a solution that would be really helpful. 如果您还可以提供简短的解释以及真正有用的解决方案。

Any help is appreciated. 任何帮助表示赞赏。

If you have the following data in a file named, file1 , 如果在名为file1的文件中包含以下数据,

 something@nothing.tld|:|george|-|
 anything@another.tld|:|thomas|-|
 third@address.tld|:|kelly|-|

you can use : and @ as delimiters and add data after it using awk, then save it to a new file, 您可以使用:@作为分隔符并在使用awk后添加数据,然后将其保存到新文件中,

awk -F '[@:]' '{ print $1"@"$2 ":|" $2"--" $3 }' file1 > file2

Above command saves following data in a file called file2 , 上面的命令将后续数据保存在名为file2的文件中,

something@nothing.tld|:|nothing.tld|--|george|-|
anything@another.tld|:|another.tld|--|thomas|-|
third@address.tld|:|address.tld|--|kelly|-|

With GNU awk for gensub(): 使用GNU awk for gensub():

$ awk 'BEGIN{FS=OFS="|"} {print $1, $2, gensub(/.*@/,"",1,$1), "--", $3, $4, $5}' file
something@nothing.tld|:|nothing.tld|--|george|-|
anything@another.tld|:|another.tld|--|thomas|-|
third@address.tld|:|address.tld|--|kelly|-|

With any awk: 有任何awk:

$ awk 'BEGIN{FS=OFS="|"} {d=$1; sub(/.*@/,"",d); print $1, $2, d, "--", $3, $4, $5}' file
something@nothing.tld|:|nothing.tld|--|george|-|
anything@another.tld|:|another.tld|--|thomas|-|
third@address.tld|:|address.tld|--|kelly|-|

You can do it like this with sed : 你可以用sed这样做:

sed -E 's/@([^|]+)\|:\|/&\1|--|/' infile

Note the use of a negated-group ( [^|] ), ie match anything except this character group. 注意使用negated-group( [^|] ),即匹配除此字符组之外的任何内容。

Output: 输出:

something@nothing.tld|:|nothing.tld|--|george|-|
anything@another.tld|:|another.tld|--|thomas|-|
third@address.tld|:|address.tld|--|kelly|-|

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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