我怎么能提供的bash变量字段在AWK打印 [英] how can I supply bash variables as fields for print in awk

查看:154
本文介绍了我怎么能提供的bash变量字段在AWK打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前想使用awk来重新排列.csv文件类似于以下内容:

I currently am trying to use awk to rearrange a .csv file that is similar to the following:

stack,over,flow,dot,com

和输出将会是:

over,com,stack,flow,dot

(或任何其它顺序,只是使用这种作为例子)

(or any other order, just using this as an example)

和当谈到时间重新排列csv文件,我一直在尝试使用以下命令:

and when it comes time to rearrange the csv file, I have been trying to use the following:

first='$2'
second='$5'
third='$1'
fourth='$3'
fifth='$4'

awk -v a=$first -v b=$second -v c=$third -v d=$fourth -v e=$fifth -F '^|,|$' '{print $a,$b,$c,$d,$e}' somefile.csv

使用awk /打印间preting的$ A,$ B,$ C等领域为数字,意图所以它会出来如下:

with the intent of awk/print interpreting the $a,$b,$c,etc as field numbers, so it would come out to the following:

{print $2,$5,$1,$3,$4}

和打印出的顺序csv文件的领域,可惜我一直没能得到这个正常工作还没有。我试过几种不同的方法,这似乎像最有前途的,但遗憾的是一直没能得到任何解决方案尚未正常工作。话虽如此,我想知道如果任何人都可以尽可能的给任何建议或指出我的缺点,因为我在这个时间点难倒,任何帮助将是非常美联社preciated,谢谢!

and print out the fields of the csv file in that order, but unfortunately I have not been able to get this to work correctly yet. I've tried several different methods, this seeming like the most promising, but unfortunately have not been able to get any solution to work correctly yet. Having said that, I was wondering if anyone could possibly give any suggestions or point out my flaw as I am stumped at this point in time, any help would be much appreciated, thanks!

推荐答案

使用简单的数字:

first='2'
second='5'
third='1'
fourth='3'
fifth='4'

awk -v a=$first -v b=$second -v c=$third -v d=$fourth -v e=$fifth -F '^|,|$' \
     '{print $a, $b, $c, $d, $e}' somefile.csv

这篇关于我怎么能提供的bash变量字段在AWK打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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