Chinaunix首页 | 论坛 | 博客
  • 博客访问: 264389
  • 博文数量: 45
  • 博客积分: 930
  • 博客等级: 准尉
  • 技术积分: 553
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-22 17:53
文章分类

全部博文(45)

文章存档

2013年(5)

2012年(40)

分类: LINUX

2012-08-03 11:04:10

需求: A文件是一个文件列表,B文件每一行包含了A文件某行,要从B中抽取出包含了A中的行

点击(此处)折叠或打开

  1. awk '{print $0}' A.txt | xargs -i grep {} B.txt > outFile
用一个shell脚本实现也是可以的,脚本如下:

点击(此处)折叠或打开

  1. #!/bin/sh

  2. if [ $# -ne 3 ];then
  3.     echo "parameter error"
  4.     echo "usage:`basename $0` regFile targetFile outFile"
  5.     exit 1
  6. fi

  7. regFile=$1
  8. targetFile=$2
  9. outFile=$3

  10. if [ ! -f $regFile -o ! -f $targetFile ];then
  11.     echo "$regFile or $targetFile doesn't exist,please check it"
  12.     exit 1
  13. fi

  14. if [ -f $outFile ];then
  15.     rm $outFile
  16. fi

  17. while read curLine
  18. do
  19.     grep $curLine $targetFile >> $outFile
  20. done < $regFile

  21. echo "parse lines from $targetFile with $regFile to $outFile"
  22. echo "==============\nfinished"


阅读(1322) | 评论(0) | 转发(0) |
0

上一篇:Vim经典配置

下一篇:shell排序C实现

给主人留下些什么吧!~~