免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: tasteoftime_90
打印 上一主题 下一主题

[文本处理] 再请教一个关于自动代入变量的问题 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2014-08-07 23:21 |只看该作者
下午已经写过这样的了 不行啊
我的日志目录如下:
a1-1.log.1
a1-1.log.2
a1-1.log.3
a1-2.log.1
a1-2.log.2
a1-2.log.3
b1-1.log.1
b1-1.log.2
b1-1.log.3
b1-2.log.1
b1-2.log.2
b1-2.log.3

shell内容如下:
a1-1_log=$(ls -lt /try/a1-1.log.* |head -1 |awk '{print $9}')
a1-2_log=$(ls -lt /try/a1-2.log.* |head -1 |awk '{print $9}')
b1-1_log=$(ls -lt /try/b1-1.log.* |head -1 |awk '{print $9}')
b1-2_log=$(ls -lt /try/b1-2.log.* |head -1 |awk '{print $9}')
for file in a1-1.log a1-2_log b1-1_log b1-2_log; do
    test=$(grep -Po '(?<=xxoo )\d+' $file |tail -5 |awk '{print ;}')
    echo "$test" >> /try/try.txt
done

结果是:
test.sh: line 6: a1-1_log=: command not found
test.sh: line 7: a1-2_log=: command not found
test.sh: line 8: b1-1_log=: command not found
test.sh: line 9: b1-2_log=: command not found
grep: a1-1.log: 没有那个文件或目录
grep: a1-2_log: 没有那个文件或目录
grep: b1-1_log: 没有那个文件或目录
grep: b1-2_log: 没有那个文件或目录

另外,单独执行:
a1-1_log=$(ls -lt /try/a1-1.log.* |head -1 |awk '{print $9}')
grep -Po '(?<=xxoo )\d+' $file |tail -5 |awk '{print ;}'
就没问题,应该是for in循环的问题?
回复 10# Shell_HAT


   

论坛徽章:
9
2015亚冠之大阪钢巴
日期:2015-06-04 11:47:30丑牛
日期:2015-01-22 15:49:26巳蛇
日期:2015-01-22 10:11:18巨蟹座
日期:2014-11-20 10:55:03天蝎座
日期:2014-11-16 22:10:26处女座
日期:2014-11-16 11:01:10申猴
日期:2014-09-19 11:12:37双鱼座
日期:2014-07-25 10:09:54程序设计版块每日发帖之星
日期:2015-08-24 06:20:00
12 [报告]
发表于 2014-08-08 10:16 |只看该作者
test=$(grep -Po '(?<=xxoo )\d+' <<<$file |tail -5 |awk '{print ;}')

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
13 [报告]
发表于 2014-08-08 10:35 |只看该作者
回复 11# tasteoftime_90
  1. a1_1_log=$(ls -1t /try/a1-1.log.* | head -1)
  2. a1_2_log=$(ls -1t /try/a1-2.log.* | head -1)
  3. b1_1_log=$(ls -1t /try/b1-1.log.* | head -1)
  4. b1_2_log=$(ls -1t /try/b1-2.log.* | head -1)
  5. for file in ${a1_1_log} ${a1_2_log} ${b1_1_log} ${b1_2_log}; do
  6.     test=$(grep -Po '(?<=xxoo )\d+' $file | tail -5)
  7.     echo "$test" >> /try/try.txt
  8. done
复制代码

论坛徽章:
0
14 [报告]
发表于 2014-08-08 11:06 |只看该作者
我早上也刚刚发现问题 哈哈哈
想请教下为什么定义a1-1_log的时候不能使用“-”这一个符号
回复 13# Shell_HAT


   

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
15 [报告]
发表于 2014-08-08 11:21 |只看该作者
回复 14# tasteoftime_90

说说你要什么功能...(看起来一个awk就能完成)

别再一直问 X-Y problem
just talk us what you want to do
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4118053
   

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
16 [报告]
发表于 2014-08-08 11:27 |只看该作者
回复 14# tasteoftime_90


name
A word consisting only of alphanumeric characters and underscores, and beginning with an alphabetic character or an underscore.
Also referred to as an identifier.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP