Prayer

在一般中寻求卓越
posts - 1256, comments - 190, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

shell截取字符串

Posted on 2009-08-28 13:45 Prayer 阅读(1295) 评论(0)  编辑 收藏 引用 所属分类: Shell
在文件table.list中有如下内容,
cat table.list
./tempdata/s_job_chain.txt
./tempdata/s_entity_info.txt
./tempdata/s_db_info.txt
./tempdata/s_job_cost.txt
./tempdata/s_datamng_log.txt
./tempdata/s_audit_rule.txt
./tempdata/s_cnode_stat.txt
./tempdata/s_instance_stat.txt
./tempdata/h_instance_info.txt
./tempdata/s_ds_jobparam.txt
./tempdata/s_job_attr.txt
./tempdata/h_job_info.txt
./tempdata/s_refer_log.txt
./tempdata/s_receive_info.txt
./tempdata/s_event_obj.txt
./tempdata/s_cnode_info.txt
./tempdata/s_project_stat.txt
./tempdata/s_seq_job.txt


请问一下,有没有一种好的办法截取我需要的字符串,比如第一条记录,记录的长度未知,但我只要字符串“s_job_chain",这是一个表名,是我所需要的。
我有一种方法,代码如下,但我需要一种更好的。简单易懂的。谢谢
for one in `ls ./tempdata -name *.txt`
do
    onetab=`echo ${one##*/} | awk -F "." '{print $1}'`
    echo $onetab
done



cat ur.txt | xargs -i basename {}
如果不明白就去置顶看shell基础十二篇之find+xargs



awk -F'[/.]' '{print $(NF-1)}' table.list

解释,如果用/和.来进行分隔,那个倒数第二个字段就是你需要的表名

如果是文件名

试试

ls ./tempdata/ | awk -F'.' '/\.txt$/{print $1}'

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理