首页 > 系统相关 >linux 中删除文件的倒数第二列

linux 中删除文件的倒数第二列

时间:2023-09-12 18:14:23浏览次数:185  
标签:30 倒数 22 pc1 第二列 linux test03 txt root

 

001、方法1

[root@pc1 test03]# ls
a.txt
[root@pc1 test03]# cat a.txt           ## 测试数据
01      02      03      04      05      06      07      08      09      10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
[root@pc1 test03]# awk '{$(NF - 1) = ""; print $0}' a.txt
01 02 03 04 05 06 07 08  10
11 12 13 14 15 16 17 18  20
21 22 23 24 25 26 27 28  30
[root@pc1 test03]# awk '{$(NF - 1) = ""; print $0}' a.txt | sed 's/[\t ]\+/\t/g'
01      02      03      04      05      06      07      08      10
11      12      13      14      15      16      17      18      20
21      22      23      24      25      26      27      28      30

 

002、方法2

[root@pc1 test03]# ls
a.txt
[root@pc1 test03]# cat a.txt
01      02      03      04      05      06      07      08      09      10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
[root@pc1 test03]# awk '{for(i = 1; i <= NF; i++) {if( i != (NF - 1)) {printf(" %s", $i)}} printf("\n")}' a.txt
 01 02 03 04 05 06 07 08 10
 11 12 13 14 15 16 17 18 20
 21 22 23 24 25 26 27 28 30
[root@pc1 test03]# awk '{for(i = 1; i <= NF; i++) {if( i != (NF - 1)) {printf(" %s", $i)}} printf("\n")}' a.txt  | sed 's/^ //'
01 02 03 04 05 06 07 08 10
11 12 13 14 15 16 17 18 20
21 22 23 24 25 26 27 28 30

 

003、方法3

[root@pc1 test03]# ls
a.txt
[root@pc1 test03]# cat a.txt         ## 测试数据
01      02      03      04      05      06      07      08      09      10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
[root@pc1 test03]# num=$(head -n 1 a.txt | awk '{print NF - 1}')     ## 求出行号
[root@pc1 test03]# echo $num
9
[root@pc1 test03]# cut -f $num --complement a.txt     ## 利用cut 互补实现
01      02      03      04      05      06      07      08      10
11      12      13      14      15      16      17      18      20
21      22      23      24      25      26      27      28      30

 

004、rev  + cut实现

[root@pc1 test03]# ls
a.txt
[root@pc1 test03]# cat a.txt
01      02      03      04      05      06      07      08      09      10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
[root@pc1 test03]# rev a.txt | cut -f 2 --complement | rev
01      02      03      04      05      06      07      08      10
11      12      13      14      15      16      17      18      20
21      22      23      24      25      26      27      28      30

 

005、sed实现

[root@pc1 test03]# ls
a.txt
[root@pc1 test03]# cat a.txt
01      02      03      04      05      06      07      08      09      10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
[root@pc1 test03]# sed 's/\(.*\)\(\s\+\S\+\s\+\)\(\S\+$\)/\1\t\3/' a.txt
01      02      03      04      05      06      07      08      10
11      12      13      14      15      16      17      18      20
21      22      23      24      25      26      27      28      30

 

[root@pc1 test03]# ls
a.txt
[root@pc1 test03]# cat a.txt
01      02      03      04      05      06      07      08      09      10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
[root@pc1 test03]# sed -r 's/(.*)(\s+\S+\s+)(\S+$)/\1\t\3/' a.txt        ## sed实现
01      02      03      04      05      06      07      08      10
11      12      13      14      15      16      17      18      20
21      22      23      24      25      26      27      28      30

 。

 

标签:30,倒数,22,pc1,第二列,linux,test03,txt,root
From: https://www.cnblogs.com/liujiaxin2018/p/17697419.html

相关文章

  • 5、linux命令之`tail`
    目录linux命令之tail1、语法2、参数3、实例linux命令之tail用来打印日志,可以指定后面多少行输出。1、语法tail[参数][文件]2、参数选项说明-f循环读取-q不显示处理信息-v显示详细的处理信息-c<数目>显示的字节数-n<行数>显示文件的尾部n行......
  • CentOS Linux 7安全基线检查
    检查密码重用是否受限制 |  身份鉴别描述强制用户不重用最近使用的密码,降低密码猜测攻击风险检查提示--加固建议在/etc/pam.d/password-auth和/etc/pam.d/system-auth中passwordsufficientpam_unix.so这行的末尾配置remember参数为5-24之间,原来的内容不用更改,只在末尾加了r......
  • bilibili B站:makefile 编译Linux C/C++项目快速入门
    视频摘自:https://www.bilibili.com/video/BV1vg41177zT    ......
  • Linux安全加固
    1、设置复杂密码  :服务器设置大写、小写、特殊字符、数字组成的12-16位的复杂密码2、设置密码策略   :修改文件/etc/login.defs     PASS_MAX_DAYS99999   密码的有效期:99999基本上表示永久有效    PASS_MIN_DAYS0     表示自上次修......
  • Linux禁用root用户
    1、背景描述出于安全考虑,需要禁止使用root用户通过ssh远程登录Linux禁用root用户远程登录后,需要提供一个权限用户用于ssh远程登录2、创建拥有sudo权限的用户2.1、创建一个普通用户rainuseradd命令用于创建一个用户,选项-m表示创建用户的主目录,-c表示添加注释us......
  • linux 中 删除文件的最后一列以及若干列
     001、[root@pc1test03]#lsa.txt[root@pc1test03]#cata.txt##测试数据01020304050607080910111213141516171819202122232......
  • Jmeter ServerAgent 修改端口,linux
     编辑serverAgent.sh,在后面加上  --udp-port端口--tcp-port端口比如  参考:https://blog.csdn.net/lemon_linaa/article/details/89963904......
  • 在Linux服务器上装jenkins(方式二:rpm包)
    官网下载jenkinshttps://www.jenkins.io/zh/download/上面比较慢,我们从清华大学开源镜像站下载安装包:https://mirrors.tuna.tsinghua.edu.cn/搜索jenkins后,选择redhat(也可以选择redhat-stable,这个是稳定版本,版本相对redhat目录下的要低一些)......
  • linux NFS报错 无法重启rpcbind
    Failedtoregister(statd,1,tcp6/udp):svc_reg()err:RPC:Remotesystemerror-Noroutetohost 解决方案:net.ipv6.conf.all.disable_ipv6=0net.ipv4.conf.all.accept_redirects=0vi/etc/sysctl.conf==>net.ipv6.conf.all.disable_ipv6=0==>net.ipv4.c......
  • linux用户和用户组权限设置
    查看当前用户whoami #要查看当前登录用户的用户名whoami #表示打开当前伪终端的用户的用户名who命令其它常用参数:-a打印能打印的全部-d打印死掉的进程-m同ami,momlikes-q打印当前登录用户数及用户名-u打印当前登录用户登录信息-r打印运行等级创建新用户addusernewn......