1

我不知道怎么做,所以我需要一些帮助,我正在处理一个文件,只需要提取这个文件的一些子字符串。每条线用分号分隔;我只需要第一个字段和另一个字段。类似的东西如下:

输入文件:

nakamed;   ecd.bat  ;\\DomainD\userhome_n\nakamed;"\\DomainD\userhome_n\nakamed";"\\serverfile66\userhome\nakamed";"Migrated from standalone DFS"
tanreka;   ecd.bat serverfile60_D  ;;
tayljka;   ecd.bat  ;\\DomainD\userhome_t\tayljka;"\\DomainD\userhome_t\tayljka";"\\serverfile47\userhome_d\tayljka";"Migrated from standalone DFS"
taylpoo;   ecd.bat  ;\\DomainD\userhome_t\taylpoo;"\\DomainD\userhome_t\taylpoo";"\\serverfile32\userhome\taylpoo";"Migrated from standalone DFS"
thomcet;   ecd.bat  ;\\DomainD\userhome_t\thomcet;"\\DomainD\userhome_t\thomcet";"\\serverfile32\userhome\thomcet";"Migrated from standalone DFS"
thomdop;   ecd.bat  ;\\DomainD\userhome_t\thomdop;"\\DomainD\userhome_t\thomdop";"\\serverfile66\userhome\thomdop";"Migrated from standalone DFS"

所需的输出文件:

nakamed;   serverfile66
tanreka;   serverfile60
tayljka;   serverfile47
taylpoo;   serverfile32
thomcet;   serverfile32
thomdop;   serverfile66
4

2 回答 2

1

尝试使用sed

sed -r 's/^([a-zA-Z0-9]+);.*(serverfile[0-9]+).*/\1;   \2/' file.txt

或与awk

awk '{print $1, " ", gensub(/.*(serverfile[0-9]+).*/, "\\1", $0)}' file.txt

输出

nakamed;   serverfile66
tanreka;   serverfile60
tayljka;   serverfile47
taylpoo;   serverfile32
thomcet;   serverfile32
thomdop;   serverfile66
于 2013-02-19T16:19:28.213 回答
1

您可以通过将分号设置为分隔符来打印出第一个字段:

awk 'BEGIN { FS = ";" } ; { print $1 }' file
于 2013-02-19T16:19:36.860 回答