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

sh/bash/dash/ksh/zsh等Shell脚本
gzbao9999
帖子: 627
注册时间: 2008-11-08 18:34

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

#31

帖子 gzbao9999 » 2010-08-15 15:54

trigger 写了:
gzbao9999 写了:
nishizawa23 写了:那请问`echo $i|rev|cut -c 1`的正则表达式咋写的?
:em03
echo $i|grep -Po ^.
应该是
echo "$i"|grep -Po .$
:em06 我rev了
气血鼓荡,身体发胀,偶飘上头,三时舒畅
gzbao9999
帖子: 627
注册时间: 2008-11-08 18:34

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

#32

帖子 gzbao9999 » 2010-08-15 16:43

提取.h的时候可以这样精确匹配
find . -name *.c -exec grep -Po '(?<=<)[^<>]*.h(?=>)' {} \;
气血鼓荡,身体发胀,偶飘上头,三时舒畅
nishizawa23
帖子: 27
注册时间: 2010-07-12 15:32

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

#33

帖子 nishizawa23 » 2010-08-16 10:36

这样找到的头文件可能会出现
linux/types.h
asm/div64.h

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

用find是指定filename的上一级目录,能实现吗?
gzbao9999
帖子: 627
注册时间: 2008-11-08 18:34

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

#34

帖子 gzbao9999 » 2010-08-16 14:48

可以在这个基础上

代码: 全选

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;
气血鼓荡,身体发胀,偶飘上头,三时舒畅
gzbao9999
帖子: 627
注册时间: 2008-11-08 18:34

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

#35

帖子 gzbao9999 » 2010-08-16 15:02

完整的方式如下

代码: 全选

#!/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;
气血鼓荡,身体发胀,偶飘上头,三时舒畅
gzbao9999
帖子: 627
注册时间: 2008-11-08 18:34

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

#36

帖子 gzbao9999 » 2010-08-16 15:18

我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;
气血鼓荡,身体发胀,偶飘上头,三时舒畅
回复