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

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

问题描述

我想在该用户ID为给出,与再次由用户给出的值spcific列替换的数据。所以用户给出的行,列,和的值的id列来替换

我有这个code:

 的awk -v antik1 = $ 1 -v antik2 = $ 2'{子(/ antik1 /$ antik2);打印}'
persons.dat.txt

但是当我运行它像这样

  ./ tool.sh杨POUTSES

它给了什么

persons.dat.txt:

  933 |马欣达|佩雷拉|男| 1989年12月3日| 2010-03-17T13:32:10.447 + 0000 | 192.248.2.123 |火狐
1129 |卡门| Lepland |女| 1984年2月18日| 2010-02-28T04:39:58.781 + 0000 | 81.25.252.111 | Internet Explorer中
4194 |厦»茶|景点|男| 1988年10月14日| 2010-03-17T22:46:17.657 + 0000 | 103.10.89.118 | Internet Explorer中
8333 |陈|王|女| 1980年2月2日| 2010-03-15T10:21:43.365 + 0000 | 1.4.16.148 | Internet Explorer中
8698 |陈|刘|女| 1982年5月29日| 2010-02-21T08:44:41.479 + 0000 | 14.103.81.196 |火狐


解决方案

您code不符合您的要求在所有的:


  

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


我会间preT以

 的awk -v行= 2 -v COL = 3 -v NEW_VALUE =Hello World的
    BEGIN {FS = OFS =| }
    NR ==排{$山坳= NEW_VALUE}
    {打印}
persons.dat.txt

请注意使用此变量:


  • 一个朴素的变量是像C,用变量的值的行变量
  • 的(使用替代
  • $ 符号是一样引用由值给定的场号的值的操作(这里,$栏)

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.

i have this code :

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

but when i run it like this

./tool.sh Yang POUTSES

it gives nothing

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:

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

which I would interpret with

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

Note the use of variables here:

  • 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")

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

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