Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 更改用户UNIX给定的特定行和列的值_Shell_Unix_Awk_Replace - Fatal编程技术网

Shell 更改用户UNIX给定的特定行和列的值

Shell 更改用户UNIX给定的特定行和列的值,shell,unix,awk,replace,Shell,Unix,Awk,Replace,我有这个档案: 16492674422392|Alberto|Parra|female|1985-09-22|2012-09-01T01:30:59.228+0000|190.96.12.239|Chrome > 16492674424948|Peng|Chen|female|1984-07-26|2012-09-23T00:51:52.900+0000|1.4.10.198|Internet > Explorer > 16492674425075|Changpeng|Xu|

我有这个档案:

 16492674422392|Alberto|Parra|female|1985-09-22|2012-09-01T01:30:59.228+0000|190.96.12.239|Chrome
> 16492674424948|Peng|Chen|female|1984-07-26|2012-09-23T00:51:52.900+0000|1.4.10.198|Internet
> Explorer
> 16492674425075|Changpeng|Xu|female|1984-03-27|2012-10-02T03:55:00.946+0000|1.50.15.119|Firefox
> 16492674425398|Prince|Kobayashi|male|1989-08-07|2012-09-30T03:30:41.772+0000|14.101.89.18|Chrome
> 16492674426410|Yang|Wei|male|1980-07-01|2012-10-01T13:11:48.528+0000|27.144.204.193|Firefox
我希望用户:

  • 选择一个id(id是第一列)
  • 选择一列并
  • 将该值更改为用户选择的一个值
  • 我使用:

    ./tool.sh 16492674426410 3 replacement
    
    作为输入,我运行的代码是:

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

    此脚本不允许用户选择列和id。如何修改它以使其按我所需工作?

    请尝试此测试版本:

    #!/bin/bash --
    
    awk -v anid="${1}" -v antik1="${2}" -v antik2="${3}" '    
    BEGIN {
      FS="|";
      OFS="|";
    }
    {
      if ($1 == anid) {
        $antik1=antik2;
      }
      print;
    }' persons.dat.txt
    
    测试:

    $ ./tool.sh 16492674426410 3 replacement
    16492674422392|Alberto|Parra|female|1985-09-22|2012-09-01T01:30:59.228+0000|190.96.12.239|Chrome
    16492674424948|Peng|Chen|female|1984-07-26|2012-09-23T00:51:52.900+0000|1.4.10.198|Internet Explorer
    16492674425075|Changpeng|Xu|female|1984-03-27|2012-10-02T03:55:00.946+0000|1.50.15.119|Firefox
    16492674425398|Prince|Kobayashi|male|1989-08-07|2012-09-30T03:30:41.772+0000|14.101.89.18|Chrome
    16492674426410|Yang|replacement|male|1980-07-01|2012-10-01T13:11:48.528+0000|27.144.204.193|Firefox
    

    符号是文件的一部分吗?您的列是否由
    |
    字符定义/分隔?文件中是否真的有一行只有
    >Explorer