加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在bash中使用多个参数并将它们传递给awk?

发布时间:2020-12-15 21:55:24 所属栏目:安全 来源:网络整理
导读:我正在编写一个函数,我将替换前导/尾随空格 从列中,如果列中没有值,则将其替换为null. 函数适用于一列,但我如何修改多列. Function : #cat trimfunction#!/bin/bashfunction trim{vCol=$1 ###input column namevFile=$2 ###input file namevar3=/home/vipin/
我正在编写一个函数,我将替换前导/尾随空格
从列中,如果列中没有值,则将其替换为null.
函数适用于一列,但我如何修改多列.

Function :

#cat trimfunction
#!/bin/bash
function trim
{
vCol=$1                                                    ###input column name
vFile=$2                                                   ###input file name
var3=/home/vipin/temp                                      ###temp file
awk -v col="${vCol}" -f /home/vipin/colf.awk ${vFile} > $var3 ###operation
mv -f $var3 $vFile                                         ###Forcefully mv
}

AWK script :

#cat colf.awk
#!/bin/awk -f
BEGIN{FS=OFS="|"}{
  gsub(/^[ t]+|[ t]+$/,"",$col)                        ###replace space from 2nd column
}
{if ($col=="") {print $1,"NULL",$3} else print $0}         ###replace whitespace with NULL

Input file : leading/trailing/white space in 2nd column

#cat filename.txt
1| 2016-01|00000321|12
2|2016-02 |000000432|13
3|2017-03 |000004312|54
4|  |000005|32
5|2017-05|00000543|12

Script :

#cat script.sh

    . /home/vipin/trimfunction
    trim 2 filename.txt

Output file : leading/trailing/white space removed in 2nd column

#./script.sh
#cat filename.txt
1|2016-01|00000321|12
2|2016-02|000000432|13
3|2017-03|000004312|54
4|NULL|000005
5|2017-05|00000543|12

If input file is like below – ( white/leading/trailing space in 2nd
and 5th column of file)

1|2016-01|00000321|12|2016-01 |00000
2|2016-02 |000000432|13| 2016-01|00000
3| 2017-03|000004312|54|  |00000
4|  |000005|2016-02|0000
5|2017-05 |00000543|12|2016-02 |0000

How to achive below output – (All leading/trailing space trimmed and
white space replaced with NULL in 2nd and 5th col) something like trim
2 5 filename.txt trim 2 5 filename.txt ###passing two column name as
input

1|2016-01|00000321|12|2016-01|00000
2|2016-02|000000432|13|2016-01|00000
3|2017-03|000004312|54|NULL|00000
4|NULL|000005|2016-02|0000
5|2017-05|00000543|12|2016-02|0000

解决方法

这将按照你所说的想做:

$cat tst.sh
file="${!#}"
cols=( "$@" )
unset cols[$(( $# - 1 ))]

awk -v cols="${cols[*]}" '
BEGIN {
    split(cols,c)
    FS=OFS="|"
}
{
    for (i in c) {
        gsub(/^[[:space:]]+|[[:space:]]+$/,$(c[i]))
        sub(/^$/,$(c[i]))
    }
    print
}' "$file"

$./tst.sh 2 5 file
1|2016-01|00000321|12|2016-01|00000
2|2016-02|000000432|13|2016-01|00000
3|2017-03|000004312|54|NULL|00000
4|NULL|000005|2016-02|0000
5|2017-05|00000543|12|2016-02|0000

但如果你真正想要的是在所有领域而不是特定领域进行操作,那么当然有一个更简单的解决方案.

永远不要做cmd文件> TMP;顺便说一下mv tmp文件,总是做cmd文件> tmp&&改为使用mv tmp文件(注意&&),这样只有在命令成功时才覆盖原始文件.另外 – 总是引用你的shell变量,除非你有一个非常具体的目的,不要这样做并完全理解所有含义,所以使用“$file”,而不是$file.谷歌一下.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读