日期:2014-05-16  浏览次数:20663 次

linux下,提取无重复字符串

目标=提取文件内容中的fileName,无重复。

?

------------------待处理文件

the follow code has no meaning, just for testing.

# exec copy $svr/c4.txt to $user/b1-dir

exec copy $svr/a4.txt to $user/b1-dir

?

#

try to extract information from a file using linux commands.

exec copy $svr/a3.txt to $user/b2-dir

exec copy $svr/a2.txt to $user/b3-dir

?

line test again;

exec copy $svr/a1.txt to $user/b4-dir

exec copy $svr/a9.txt to $user/b5-dir

exec copy $svr/a8.txt to $user/b6-dir

exec copy $svr/a7.txt to $user/b7-dir

# exec copy $svr/c5.txt to $user/b1-dir

exec copy $svr/a6.txt to $user/b8-dir

exec copy $svr/a5.txt to $user/b9-dir

?

paste file to another file test:

exec copy $svr/a4.txt to $user/b1-dir

exec copy $svr/a3.txt to $user/b2-dir

exec copy $svr/a2.txt to $user/b3-dir

exec copy $svr/a1.txt to $user/b4-dir

------------------待处理文件end

?

解答=

grep ".txt" test.txt | sed 's/# //' | cut -d ' ' -f3 | cut -d '/' -f2 | sort | uniq

?

解释=

grep ".txt" test.txt ? ? ? ?提取含有".txt"的整行字符串。

sed 's/# //' ? ? ? ? ? ? ? ?移除行首的注释符“# ”。

cut -d ' ' -f3 ? ? ? ? ? ? ?按' '分割每行,取出第3个子串。

cut -d '/' -f2 ? ? ? ? ? ? ?按'/'分割每行,取出第2个子串。

sort ? ? ? ? ? ? ? ? ? ? ? ?对所有行排序。

uniq ? ? ? ? ? ? ? ? ? ? ? ?剔除重复的行。

?

结果=

a1.txt

a2.txt

a3.txt

a4.txt

a5.txt

a6.txt

a7.txt

a8.txt

a9.txt

c4.txt

c5.txt

?

天娇

天娇