请问shell脚本如何取出文本文件中的某个字符

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
rickychenh
帖子: 20
注册时间: 2008-07-04 20:02
来自: 四川 成都 UESTC

请问shell脚本如何取出文本文件中的某个字符

#1

帖子 rickychenh » 2009-04-04 11:00

比如a.txt这个文本我想取出它的最后一行的第一个数字放在num这个变量里应该如何写,谢谢啦
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

Re: 请问shell脚本如何取出文本文件中的某个字符

#2

帖子 HuntXu » 2009-04-04 11:35

export num=`tail -n 1 FILENAME|egrep -o "[0-9]+"|head -n 1`
HUNT Unfortunately No Talent...
头像
rickychenh
帖子: 20
注册时间: 2008-07-04 20:02
来自: 四川 成都 UESTC

Re: 请问shell脚本如何取出文本文件中的某个字符

#3

帖子 rickychenh » 2009-04-04 11:44

HuntXu 写了:export num=`tail -n 1 FILENAME|egrep -o "[0-9]+"|head -n 1`
谢谢,我想做的不是这样,是文件中最后一行的内容是
1.23434 23.123321

这两个数字之间是空格隔开的,我想把这两个数字分别读出来赋给变量num1和变量num2

麻烦您再给看一下,十分感谢
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

Re: 请问shell脚本如何取出文本文件中的某个字符

#4

帖子 HuntXu » 2009-04-04 11:44

小数点...
HUNT Unfortunately No Talent...
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

Re: 请问shell脚本如何取出文本文件中的某个字符

#5

帖子 HuntXu » 2009-04-04 11:46

代码: 全选

export num1=`tail -n 1 FILENAME|awk {'print $1'}`
export num2=`tail -n 1 FILENAME|awk {'print $2'}`
早说明都是数字嘛...我以为混排的...
HUNT Unfortunately No Talent...
头像
rickychenh
帖子: 20
注册时间: 2008-07-04 20:02
来自: 四川 成都 UESTC

Re: 请问shell脚本如何取出文本文件中的某个字符

#6

帖子 rickychenh » 2009-04-04 11:46

HuntXu 写了:

代码: 全选

export num1=`tail -n 1 FILENAME|awk {'print $1'}`
export num2=`tail -n 1 FILENAME|awk {'print $2'}`
早说明都是数字嘛...我以为混排的...
呵呵,开始没说明白,谢谢啦
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 请问shell脚本如何取出文本文件中的某个字符

#7

帖子 aerofox » 2009-04-06 12:43

代码: 全选

num=( `tail -n1 a.txt` )
echo ${num[0]}
echo ${num[1]}
回复