用 awk 替换 UNIX 列 [英] replace columns UNIX with awk

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

问题描述

我想用用户提供的 id 替换特定列中的数据,并再次使用用户提供的值.所以用户给出了行的id,列的id,以及列中要替换的值.

I want to replace data in spcific column that the user gives with id, with a value that again is given by the user. So the user gives the id of the row, the column, and the value to replace in the column.

我有这个代码:

awk -v antik1=$1 -v antik2=$2 '{sub(/antik1/,"$antik2") ; print }'
persons.dat.txt

但是当我这样运行时

./tool.sh Yang POUTSES

什么都没给

persons.dat.txt:

persons.dat.txt :

933|Mahinda|Perera|male|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox
1129|Carmen|Lepland|female|1984-02-18|2010-02-28T04:39:58.781+0000|81.25.252.111|Internet Explorer
4194|Há»" Chí|Do|male|1988-10-14|2010-03-17T22:46:17.657+0000|103.10.89.118|Internet Explorer
8333|Chen|Wang|female|1980-02-02|2010-03-15T10:21:43.365+0000|1.4.16.148|Internet Explorer
8698|Chen|Liu|female|1982-05-29|2010-02-21T08:44:41.479+0000|14.103.81.196|Firefox

推荐答案

您的代码根本不符合您的要求:

You code does not match your requirement at all:

用户给出行、列的 id 和要替换的值

the user gives the id of the row, the column, and the value to replace

我会用它来解释

awk -v row=2 -v col=3 -v new_value="Hello World" '
    BEGIN { FS = OFS = "|" }
    NR == row {$col = new_value}
    {print}
' persons.dat.txt

注意这里变量的使用:

  • 一个未修饰的变量就像C一样,用变量的值替换(行"变量的用法
  • $ 符号就像一个运算符,它引用由值(此处为$col")给出的字段编号的值
  • an unadorned variable is like C, replace with the variable's value (usage of the "row" variable
  • the $ symbol is like an operator that references the value of the field number given by the value (here, "$col")

这篇关于用 awk 替换 UNIX 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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