分页: 3 / 3

Re: 如何利用shell从文件中读取文件路径

发表于 : 2010-08-15 15:54
gzbao9999
trigger 写了:
gzbao9999 写了:
nishizawa23 写了:那请问`echo $i|rev|cut -c 1`的正则表达式咋写的?
:em03
echo $i|grep -Po ^.
应该是
echo "$i"|grep -Po .$
:em06 我rev了

Re: 如何利用shell从文件中读取文件路径

发表于 : 2010-08-15 16:43
gzbao9999
提取.h的时候可以这样精确匹配
find . -name *.c -exec grep -Po '(?<=<)[^<>]*.h(?=>)' {} \;

Re: 如何利用shell从文件中读取文件路径

发表于 : 2010-08-16 10:36
nishizawa23
这样找到的头文件可能会出现
linux/types.h
asm/div64.h

已上面作为文件名用find定位路径时就会出现错误
find . -name linux/types.h
可是我就是想定位 linux文件夹中的types.h(实际路径为include/linux/types.h)

用find是指定filename的上一级目录,能实现吗?

Re: 如何利用shell从文件中读取文件路径

发表于 : 2010-08-16 14:48
gzbao9999
可以在这个基础上

代码: 全选

find . -name *.c -exec grep -Po '(?<=<)[^<>]*.h(?=>)' {} \;
继续匹配出head tail来,根据路径前缀head,grep到正确的路径

代码: 全选

#!/bin/bash
#str=types.h;
str="linux/types.h";
#str=include/linux/types.h;
if [ `echo $str|grep '/'` ] ; then
	head=`echo $str|grep -Po '^.*/(?=[^/]*.h)'`;
	tail=`echo $str|grep -Po '(?<=/)[^/]*$'`;
	find /usr/include/ -name $tail|grep $head;
else 
	find /usr/include/ -name $str;
fi;

Re: 如何利用shell从文件中读取文件路径

发表于 : 2010-08-16 15:02
gzbao9999
完整的方式如下

代码: 全选

#!/bin/bash
find . -name *.c -exec grep -Po '(?<=<)[^<>]*.h(?=>)' {} \;> mytemp
while read str
do
echo $str=============;
if [ `echo $str|grep '/'` ] ; then
	head=`echo $str|grep -Po '^.*/(?=[^/]*.h)'`;
#	echo $head;
	tail=`echo $str|grep -Po '(?<=/)[^/]*$'`;
#	echo $tail;
	find /usr/include/ -name $tail|grep $head;
else 
	find /usr/include/ -name $str;
fi;
done < mytemp
rm mytemp;

Re: 如何利用shell从文件中读取文件路径

发表于 : 2010-08-16 15:18
gzbao9999
我x还可以更简洁更准确点

代码: 全选

#!/bin/bash
find . -name *.c -exec grep -Po '(?<=<)[^<>]*.h(?=>)' {} \;> mytemp
while read str
do
echo $str=============;
if [ `echo $str|grep '/'` ] ; then
   tail=`echo $str|grep -Po '(?<=/)[^/]*$'`;
   find /usr/include/ -name $tail|grep $str;
else 
   find /usr/include/ -name $str;
fi;
done < mytemp
rm mytemp;