linux下,提取无重复字符串

本文介绍了一种使用Linux命令从特定格式的文本文件中提取文件名的方法,并通过实例演示了整个过程。具体步骤包括使用grep筛选包含.txt的部分,sed去除注释符号,cut进行字段切割,最后通过sort和uniq命令来排序并去除重复项。

目标=提取文件内容中的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

 

天娇

天娇
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值