这里我想删除每行第一个大写字母上的序列号。我该如何在这个文件中执行此操作?
1 #
2 # Automatically generated file; DO NOT EDIT.
3 # OpenWrt Configuration
4 #
5 CONFIG_MODULES=y
6 CONFIG_HAVE_DOT_CONFIG=y
7 CONFIG_TARGET_sunxi=y
8 # CONFIG_TARGET_sunxi_BPI-M1-Plus is not set
9 CONFIG_TARGET_sunxi_BPI-M1=y
10 # CONFIG_TARGET_sunxi_BPI-R1 is not set
11 CONFIG_TARGET_BOARD="sunxi"
12 CONFIG_TARGET_ARCH_PACKAGES="sunxi"
13 CONFIG_DEFAULT_TARGET_OPTIMIZATION="-Os -pipe -march=armv7-a -mtune=cortex-a8 -mfpu=vfpv3-d16"
14 CONFIG_CPU_TYPE="cortex-a8+vfpv3"
15 CONFIG_LINUX_3_18=y
16 CONFIG_DEFAULT_base-files=y
17 CONFIG_DEFAULT_busybox=y
18 CONFIG_DEFAULT_dnsmasq=y
19 CONFIG_DEFAULT_dropbear=y
20 CONFIG_DEFAULT_firewall=y
21 CONFIG_DEFAULT_fstools=y
22 CONFIG_DEFAULT_ip6tables=y
^^^^ ^^
抱歉,我应该在编号序列前面加上空格并标记它们。每行都有空格。实际上,我必须处理超过 4000 行。这里我只发布 1-22。命令 sed 应该有效。我忘了明确解释。正则表达式对我来说很新而且很复杂。希望你能给我一个建议。
答案1
不知道您是否之后C
或line numbers
我提供以下解决方案:
要摆脱
line numbers
使用:sed 's/^ *.[0-9] //g' line.txt # Result # # Automatically generated file; DO NOT EDIT. # OpenWrt Configuration # CONFIG_MODULES=y CONFIG_HAVE_DOT_CONFIG=y CONFIG_TARGET_sunxi=y # CONFIG_TARGET_sunxi_BPI-M1-Plus is not set CONFIG_TARGET_sunxi_BPI-M1=y # CONFIG_TARGET_sunxi_BPI-R1 is not set CONFIG_TARGET_BOARD="sunxi" CONFIG_TARGET_ARCH_PACKAGES="sunxi" CONFIG_DEFAULT_TARGET_OPTIMIZATION="-Os -pipe -march=armv7-a -mtune=cortex-a8 -mfpu=vfpv3-d16" CONFIG_CPU_TYPE="cortex-a8+vfpv3" CONFIG_LINUX_3_18=y CONFIG_DEFAULT_base-files=y CONFIG_DEFAULT_busybox=y CONFIG_DEFAULT_dnsmasq=y CONFIG_DEFAULT_dropbear=y CONFIG_DEFAULT_firewall=y CONFIG_DEFAULT_fstools=y CONFIG_DEFAULT_ip6tables=y
要摆脱
C
使用:sed '5,22s/ [C]//g' line.txt # Results 1 # 2 # Automatically generated file; DO NOT EDIT. 3 # OpenWrt Configuration 4 # 5 ONFIG_MODULES=y 6 ONFIG_HAVE_DOT_CONFIG=y 7 ONFIG_TARGET_sunxi=y 8 #ONFIG_TARGET_sunxi_BPI-M1-Plus is not set 9 ONFIG_TARGET_sunxi_BPI-M1=y 10 #ONFIG_TARGET_sunxi_BPI-R1 is not set 11 ONFIG_TARGET_BOARD="sunxi" 12 ONFIG_TARGET_ARCH_PACKAGES="sunxi" 13 ONFIG_DEFAULT_TARGET_OPTIMIZATION="-Os -pipe -march=armv7-a -mtune=cortex-a8 -mfpu=vfpv3-d16" 14 ONFIG_CPU_TYPE="cortex-a8+vfpv3" 15 ONFIG_LINUX_3_18=y 16 ONFIG_DEFAULT_base-files=y 17 ONFIG_DEFAULT_busybox=y 18 ONFIG_DEFAULT_dnsmasq=y 19 ONFIG_DEFAULT_dropbear=y 20 ONFIG_DEFAULT_firewall=y 21 ONFIG_DEFAULT_fstools=y 22 ONFIG_DEFAULT_ip6tables=y
写入原始文件并创建备份使用命令
--in-place
中的选项:sed
sed --in-place=.bak '5,22s/ [C]//g' line.txt sed --in-place=.bak 's/^ *.[0-9] //g' line.txt
要写入新文件:
sed 's/^ *.[0-9] //g' line.txt >> new_file.txt sed '5,22s/ [C]//g' line.txt >> new_file.txt
保留每行前的空格:
sed 's/^ *.[0-9] / /g' line.txt
答案2
awk '{print $2, $3, $4, $5, $6, $7, $8}' ur4000rowfile.txt
会给:
#
# Automatically generated file; DO NOT EDIT.
# OpenWrt Configuration
#
CONFIG_MODULES=y
CONFIG_HAVE_DOT_CONFIG=y
CONFIG_TARGET_sunxi=y
# CONFIG_TARGET_sunxi_BPI-M1-Plus is not set
CONFIG_TARGET_sunxi_BPI-M1=y
# CONFIG_TARGET_sunxi_BPI-R1 is not set
CONFIG_TARGET_BOARD="sunxi"
CONFIG_TARGET_ARCH_PACKAGES="sunxi"
CONFIG_DEFAULT_TARGET_OPTIMIZATION="-Os -pipe -march=armv7-a -mtune=cortex-a8 -mfpu=vfpv3-d16"
CONFIG_CPU_TYPE="cortex-a8+vfpv3"
CONFIG_LINUX_3_18=y
CONFIG_DEFAULT_base-files=y
CONFIG_DEFAULT_busybox=y
CONFIG_DEFAULT_dnsmasq=y
CONFIG_DEFAULT_dropbear=y
CONFIG_DEFAULT_firewall=y
CONFIG_DEFAULT_fstools=y
CONFIG_DEFAULT_ip6tables=y
或者如果你这样做:
awk '{$1=""; print $0}' ur4000rowfile.txt
你将获得如下输出:
#
# Automatically generated file; DO NOT EDIT.
# OpenWrt Configuration
#
CONFIG_MODULES=y
CONFIG_HAVE_DOT_CONFIG=y
CONFIG_TARGET_sunxi=y
# CONFIG_TARGET_sunxi_BPI-M1-Plus is not set
CONFIG_TARGET_sunxi_BPI-M1=y
# CONFIG_TARGET_sunxi_BPI-R1 is not set
CONFIG_TARGET_BOARD="sunxi"
CONFIG_TARGET_ARCH_PACKAGES="sunxi"
CONFIG_DEFAULT_TARGET_OPTIMIZATION="-Os -pipe -march=armv7-a -mtune=cortex-a8 -mfpu=vfpv3-d16"
CONFIG_CPU_TYPE="cortex-a8+vfpv3"
CONFIG_LINUX_3_18=y
CONFIG_DEFAULT_base-files=y
CONFIG_DEFAULT_busybox=y
CONFIG_DEFAULT_dnsmasq=y
CONFIG_DEFAULT_dropbear=y
CONFIG_DEFAULT_firewall=y
CONFIG_DEFAULT_fstools=y
CONFIG_DEFAULT_ip6tables=y
最后,如果您不想使用输出来创建新文件,只需将输出重定向到新文件,如下所示:
awk '{print $2, $3, $4, $5, $6, $7, $8}' ur400rowfile.txt > urnewcleanfile.txt
或这个:
awk '{$1=""; print $0}' ur400rowfile.txt > urnewcleanfile.txt