标签在AWK分隔值 [英] Tab separated values in awk

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

问题描述

我如何选择从制表符分隔字符串中的第一列?

 #回声LOAD_SETTLED LOAD_INIT 2011-01-13 3点50分01秒| awk的-F'\\ t''{打印$ 1}

以上将返回整条生产线,而不是如预期只是LOAD_SETTLED。

更新:

我需要更改制表符分隔值的第三列。
以下是行不通的。

 回声$线| awk的'BEGIN {VAR -v =$ mycol_newFS =[\\ t] +}; {打印$ 1 $ 2变量$ $ 4 $ 5 $ 6 7 $ $ 8 9}'>> /pdump/temp.txt

然而,这工作,如果分离是逗号而不是标签符合市场预期。

 回声$线| AWK -v VAR =$ mycol_new-F'\\ t''{打印$ 1,$ 2,无功,$ 4,$ 5,$ 6,$ 7,$ 8, $ 9}'>> /pdump/temp.txt


解决方案

您需要设置 OFS 变量(输出字段分隔符)是一个标签:

 回声$行|
AWK -v VAR =$ mycol_new-F $'\\ t''BEGIN {OFS = FS} {$ 3 = VAR;打印}'

(确保你引用的echo语句的 $行变量)

How do I select the first column from the TAB separated string?

# echo "LOAD_SETTLED    LOAD_INIT       2011-01-13 03:50:01" | awk -F'\t' '{print $1}'

The above will return the entire line and not just "LOAD_SETTLED" as expected.

Update:

I need to change the third column in the tab separated values. The following does not work.

echo $line | awk 'BEGIN { -v var="$mycol_new" FS = "[ \t]+" } ; { print $1 $2 var $4 $5 $6 $7 $8 $9 }' >> /pdump/temp.txt

This however works as expected if the separator is comma instead of tab.

echo $line | awk -v var="$mycol_new" -F'\t' '{print $1 "," $2 "," var "," $4 "," $5 "," $6 "," $7 "," $8 "," $9 "}' >> /pdump/temp.txt

解决方案

You need to set the OFS variable (output field separator) to be a tab:

echo "$line" | 
awk -v var="$mycol_new" -F $'\t' 'BEGIN {OFS = FS} {$3 = var; print}'

(make sure you quote the $line variable in the echo statement)

这篇关于标签在AWK分隔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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