awk脚本:如何prevent ARGV被视为输入文件名 [英] Awk script: How to prevent ARGV from being treated as an input file name

查看:98
本文介绍了awk脚本:如何prevent ARGV被视为输入文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来,awk脚本认为 ARGV [1] ARGV [ARGC] 作为输入文件。

有没有什么办法让awk的考虑 ARGV 简单的参数,而不是输入文件

例如:

test.awk

 #!的/ usr /斌/的awk -f
BEGIN {标题= ARGV [2]}
{如果($ 1 ==AA){打印标题}}

dat文件

  AB
BA
AA
CC$ test.awk逸我的有趣的标题


  

    

      

我的有趣标题
      AWK:test.awk:3:致命的:无法打开文件'我的有趣的标题用于读取(没有这样的文件或目录)


    
  

解决方案

这样呢?

  $ AWK -v标题='我的有趣标题''$ 0〜/ AA / {打印标题} 1'输入
AB
BA
我的题目有趣
AA
CC

It seems that awk script considers ARGV[1] to ARGV[ARGC] as input files.

Is there any way to make awk considering ARGV as simple arguments instead of an input file

Example:

test.awk

#!/usr/bin/awk -f
BEGIN {title=ARGV[2]}
{if ($1=="AA") {print title}}

dat file

AB
BA
AA
CC

$ test.awk dat 'My Interesting Title'

My Interesting Title awk: test.awk:3: fatal: cannot open file `My Interesting Title' for reading (No such file or directory)

解决方案

Something like this?

$ awk -v title='My Interesting Title' '$0 ~ /AA/ {print title}1' input
AB
BA
My Interesting Title
AA
CC

这篇关于awk脚本:如何prevent ARGV被视为输入文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆