免费注册 查看新帖 |

Chinaunix

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

[文本处理] 如何在awk执行时,将一个shell命令的结果复制给一个awk变量? [复制链接]

论坛徽章:
0
11
发表于 2014-10-09 19:15
回复 4# yestreenstars


    需要访问列的内容,在-v var=$(...)命令里可以实现吗?例如将$2赋值为head -1 $1,其中的$1是指第一列的内容?

论坛徽章:
0
12 [报告]
发表于 2014-10-09 19:16 |只看该作者
回复 10# yestreenstars


    不行

论坛徽章:
2
摩羯座
日期:2014-11-03 15:28:56卯兔
日期:2015-01-04 17:20:51
13 [报告]
发表于 2014-10-09 19:19 |只看该作者
回复 11# funexploit


    为啥要这么处理?我觉得在3楼提供的方法已经可以解决LZ的问题了

   在-v里面设定变量,变量的值在awk处理之前就已经固定了(相当于在BEGIN区域里面定义变量,是在读取file之前操作的),如何能实现根据列的内容动态读取文件的效果?

论坛徽章:
0
14 [报告]
发表于 2014-10-09 19:19 |只看该作者
回复 5# 关阴月飞

不是,需求是取某一列的内容(这列的内容是一个文件名),然后取得这个文件里面的第一行作为一个新增列

   

论坛徽章:
0
15 [报告]
发表于 2014-10-09 19:24 |只看该作者
回复 13# bulletmarquis

我的问题实际上更通用,即如何在awk脚本中,调用一个外部命令,参数是$n(某一列的内容),然后将执行结果赋值给一个变量

   

论坛徽章:
2
摩羯座
日期:2014-11-03 15:28:56卯兔
日期:2015-01-04 17:20:51
16 [报告]
发表于 2014-10-09 19:29 |只看该作者
本帖最后由 bulletmarquis 于 2014-10-09 19:29 编辑

回复 15# funexploit


    我觉得你没看明白代码
  1. awk '{getline a<$0;print $0,a}' file
复制代码
getline就是用来执行外部命令,并获取结果,传给变量的
我按你的需求把代码写成下面这样,你可以看的更清楚
  1. awk '{fname=$0;_cmd_="head -1 "fname;_cmd_|getline rst;close(_cmd_);print fname, rst}' file
复制代码
这个写法效果和上面那个是一样的,就是标准的在awk里面读取文件内容,执行外部命令,获取外部命令的执行结果并打印的流程

论坛徽章:
3
丑牛
日期:2014-09-13 18:19:22摩羯座
日期:2014-10-10 17:43:02水瓶座
日期:2014-10-16 01:00:22
17 [报告]
发表于 2014-10-09 19:52 |只看该作者
学生党:飘过~无聊用python试试

  1. #!/usr/bin/env python
  2. # -*- coding: gbk -*-
  3. "just for fun ~ so so"

  4. from sys import argv
  5. import linecache as lc

  6. with open(argv[1]) as fd:
  7.         for x in fd:
  8.                 print  x.strip('\n'), lc.getline(x.strip('\n'),1),
复制代码
测试:
$ ./yhsafe.py file.txt
file1.txt file1_title
file2.txt file2_title
file3.txt file3_title

论坛徽章:
0
18 [报告]
发表于 2014-10-09 20:29 |只看该作者
bulletmarquis 发表于 2014-10-09 19:29
回复 15# funexploit


多谢,问题解决了。我之前不知道"cmd | getline"这种格式。

论坛徽章:
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
19 [报告]
发表于 2014-10-09 20:30 |只看该作者
回复 15# funexploit

>> ...然后将执行结果赋值给一个变量


http://www.gnu.org/software/gawk/manual/gawk.html#Getline

4.9 Explicit Input with getline...
...
• Plain Getline:                  Using getline with no arguments.
• Getline/Variable:                  Using getline into a variable.
• Getline/File:                  Using getline from a file.
• Getline/Variable/File:                  Using getline into a variable from a file.
• Getline/Pipe:                  Using getline from a pipe.
• Getline/Variable/Pipe:                  Using getline into a variable from a pipe.
• Getline/Coprocess:                  Using getline from a coprocess.
• Getline/Variable/Coprocess:                  Using getline into a variable from a coprocess.
• Getline Notes:                  Important things to know about getline.
• Getline Summary:                  Summary of getline Variants.

...

4.9.6 Using getline into a Variable from a Pipe

When you use ‘command | getline var’, the output of command is sent through a pipe to getline and into the variable var. For example, the following program reads the current date and time into the variable current_time, using the date utility, and then prints it:

BEGIN {
     "date" | getline current_time
     close("date")
     print "Report printed on " current_time
}

In this version of getline, none of the built-in variables are changed and the record is not split into fields.

论坛徽章:
0
20 [报告]
发表于 2014-10-09 20:45 |只看该作者
知道了,多谢。
回复 19# jason680


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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP