开发者

Modifying data using awk

In a long file i'm searching for something like this:

c 0.5p_f

10 px 2

I need to modify a 3rd column of a line afte开发者_运维技巧r 'c 0.5p_f' marker.

It's part of a bash script that would do this and i would like to avoid using, like, awk scripts, only bash commands.


Why not use awk? It's perfect.

do_modify{$3="modify";do_modify=0}/c 0\.5p_f/{do_modify=1}1

If you can use sed scripts,

/c 0\.5p_f/{n;s/\([^[:space:]]*[[:space:]]\+[^[:space:]]*[[:space:]]\+\)\S*/\1modify/}

would do. Not that pure Bash is hard either, though.

do_modify=
while read -r line; do
    if [[ -n ${do_modify} ]]; then
        columns=(${line})
        columns[2]=modified
        line=${columns[*]}
        do_modify=
    fi
    printf '%s\n' "${line}"
    if [[ ${line} = *'c 0.5p_f'* ]]; then
        do_modify=1
    fi
done
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜