cut命令-在AWK中等效的补码标志 [英] cut command --complement flag equivalent in AWK

查看:91
本文介绍了cut命令-在AWK中等效的补码标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是刚开始编写shell脚本

I am new to writing shell scripts

我正在尝试编写一个AWK命令,该命令恰好可以完成以下操作

I am trying to write an AWK command which does exactly the below

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

$ IGNORE_RANGE的值可以是1-5或5-10等

$IGNORE_RANGE can be of any value say, 1-5 or 5-10 etc

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

i cannot use cut since i am in AIX and AIX does not support --complement, is there any way to achieve this using AWK command

示例:

file.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

推荐答案

能否请您按照显示的示例尝试进行以下操作,编写并进行测试.我们有rangerange变量,该变量应该在start_of_position-end_of_position中,并且可以根据需要传递它.

Could you please try following, written and tested with shown samples. We have range variable of awk which should be in start_of_position-end_of_position and we could pass it as per need.

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

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

OR to make it more clear in understanding wise try following:

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

说明: 添加以上详细说明.

Explanation: Adding detailed explanation for above.

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.

这篇关于cut命令-在AWK中等效的补码标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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