aws中的OFS问题 [英] issue with OFS in awk

查看:112
本文介绍了aws中的OFS问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个包含此字符串(字段分隔符是百分号),存储在名为data的变量中


I have a string containing this (field separator is the percentage sign), stored in a variable called data

201%jkhjfhn%kfhngjm%mkdfhgjdfg%mkdfhgjdfhg%mkdhfgjdhfg%kdfhgjgh%kdfgjhgfh%mkfgnhmkgfnh%k,gnhjkgfn%jkdfhngjdfng

我正在尝试打印出用字符串代替百分号的字符串,但它似乎比我想象的要硬:

I'm trying to print out that string replacing the percentage sign with a pipe but it seems harden than i thought:

echo ${data} | awk -F"%" 'BEGIN {OFS="|"} {print $0}'

我知道我离它很近,只是距离不够.

I know I'm very close to it just not close enough.

我看到的代码是:

I see that code as:

1 echo the variable value into a awk session
2 set field separator as "%"
3 set as output field separator "|"
4 print the line

推荐答案

尝试一下:

echo "$data" | awk -F"%" 'BEGIN {OFS="|"} {$1=$1; print $0}'

来自 awk手册

最后,有时候强迫awk重建整个系统很方便 使用字段和OFS的当前值进行记录.为此,使用看似 无害的分配:

Finally, there are times when it is convenient to force awk to rebuild the entire record, using the current value of the fields and OFS. To do this, use the seemingly innocuous assignment:

$1 = $1   # force record to be reconstituted
print $0  # or whatever else with $0


如果搜索awk的替代方法,则仅使用tr的另一种轻量级方法:


Another lightweight way using only tr if you search an alternative for awk :

tr '%' '|' <<< "$data"

这篇关于aws中的OFS问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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