AWK或Sed提取单词

时间:2013-02-19 16:14:46

标签: text sed awk

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

输入文件:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

awk 'BEGIN { FS = ";" } ; { print $1 }' file