LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 878|回复: 6

awk脚本中如何将变量的值作为正规式

[复制链接]
发表于 2006-6-7 23:20:23 | 显示全部楼层 |阅读模式
我在脚本中为一个变量赋值,例如:
               date = "2006/Jun/7"
想在后面的语句中引用该值作为一个正规式,即/.../中的内容是"2006/Jun/7"
不知道能否实现?谢谢!
发表于 2006-6-8 11:02:43 | 显示全部楼层

菜鸟回复,错了请指出

Post by wmaple
我在脚本中为一个变量赋值,例如:
               date = "2006/Jun/7"
想在后面的语句中引用该值作为一个正规式,即/.../中的内容是"2006/Jun/7"
不知道能否实现?谢谢!


#!/bin/sh

str="Yesterday is 2006/Jun/7"
var="2006\/Jun\/7"

echo $str |awk '/'$var'/{print $1,$3}'

[root@freedom ~]# ./temp
Yesterday 2006/Jun/7


不知是不是楼主想要的答复阿?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-6-9 01:07:06 | 显示全部楼层
谢谢yfreedom的回复,在命令行上的方法我学会了,我的本意是如何在awk脚本中实现。例如:

BEGIN{
  mydate = "2006/Jun/06";
  print mydate;
}

/mydate/{ 〈===mydate的值作为正规式
  print
}

现在问题已经解决了:

$0 ~ mydate{ 〈===用比较表达式
  print
}

不知道还有没有其他的方法?学习中...
回复 支持 反对

使用道具 举报

发表于 2006-6-9 13:50:06 | 显示全部楼层
Post by wmaple
谢谢yfreedom的回复,在命令行上的方法我学会了,我的本意是如何在awk脚本中实现。例如:

BEGIN{
  mydate = "2006/Jun/06";
  print mydate;
}

/mydate/{ 〈===mydate的值作为正规式
  print
}

现在问题已经解决了:

$0 ~ mydate{ 〈===用比较表达式
  print
}

不知道还有没有其他的方法?学习中...

mydate is awk internal variable, I don't think you can reference it in the address section.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-6-10 02:43:11 | 显示全部楼层
Post by yongjian
mydate is awk internal variable, I don't think you can reference it in the address section.


But it really works.
回复 支持 反对

使用道具 举报

发表于 2006-6-10 06:15:51 | 显示全部楼层
works when you are not using it in the address section. (inside / /).
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-6-10 15:45:11 | 显示全部楼层
of couse
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表