gpt4 book ai didi

unix - 剪切命令——AWK 中等效的补码标志

转载 作者:行者123 更新时间:2023-12-05 08:49:44 25 4
gpt4 key购买 nike

我刚开始写shell脚本

我正在尝试编写一个完全执行以下操作的 AWK 命令

cut --complement -c $IGNORE_RANGE file.txt > tmp

$IGNORE_RANGE 可以是任何值,例如 1-5 或 5-10 等

我不能使用 cut,因为我在 AIX 中并且 AIX 不支持 --complement,有什么方法可以使用 AWK 命令实现这一点

例子:

文件.txt

abcdef
123456

输出

cut --complement -c 1-2 file.txt > tmp

cdef
3456


cut --complement -c 4-5 file.txt > tmp
abcf
1236

cut --complement -c 1-5 file.txt > tmp
f
6

最佳答案

能否请您尝试使用显示的示例进行跟踪、编写和测试。我们有 awkrange 变量,它应该在 start_of_position-end_of_position 中,我们可以根据需要传递它。

awk -v range="4-5" '
BEGIN{
split(range,array,"-")
}
{
print substr($0,1,array[1]-1) substr($0,array[2]+1)
}
' Input_file

或者为了更清楚地理解明智的做法,请尝试以下操作:

awk -v range="4-5" '
BEGIN{
split(range,array,"-")
start=array[1]
end=array[2]
}
{
print substr($0,1,start-1) substr($0,end+1)
}
' Input_file

说明: 为以上添加详细说明。

awk -v range="4-5" '                                ##Starting awk program from here creating range variable which has range value of positions which we do not want to print in lines.
BEGIN{ ##Starting BEGIN section of this program from here.
split(range,array,"-") ##Splitting range variable into array with delimiter of - here.
start=array[1] ##Assigning 1st element of array to start variable here.
end=array[2] ##Assigning 2nd element of array to end variable here.
}
{
print substr($0,1,start-1) substr($0,end+1) ##Printing sub-string of current line from 1 to till value of start-1 and then printing from end+1 which basically means will skip that range of characters which OP does not want to print.
}
' Input_file ##Mentioning Input_file name here.

关于unix - 剪切命令——AWK 中等效的补码标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63356703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com