Linux- sed xargs expr exec

使用sed、xargs和expr进行文本处理与计算
本文介绍了sed命令的用法,包括替换文本、全局替换和删除特定行。同时,讲解了xargs如何处理多个文件并解决文件名包含空格的问题。还提到了expr命令用于简单的数学运算。最后,讨论了exec命令如何替换当前shell进程来执行命令。

sed 常用于用正则表达式 来替换一些文本:
$ sed 's/exp/text/'
把每行的 冒号 替换成 %,且 标准输出显示:
$ sed 's/:/%/' /etc/passwd
加 g (global) 表示替换掉 所有的 冒号:
$ sed 's/:/%/' /etc/passwd
删除 3到6行:
$ sed 3,6d /etc/passwd
3,6 是地址,d是删除命令。s 和 d是sed最常见的应用。
可以把正则表达式 作为地址:
$ sed '/exp/d'
上面这些例子,sed 都是往 标准输出流 写;如果不写文件,sed 从标准输入流读。

xargs

当你对 多个文件 运行一个命令时,这个命令可能不支持多个文件。xargs 可以 绕过这个问题。
找出gif结尾 的文件,并验证他们 是否属于 gif 文件:
$ find . -name '*.gif' -print | xargs file
如果文件名 有空格 或者 新行,可能会有问题。
xargs 会启动很多进程,效率不高。
file 的 -- 表示 后面的参数 全是文件名,而不是 file 的参数
可以用 exec 的 -exec 参数 来替代 xargs
$ find . -name '*.gif' -exec file {} \;

expr 

expr 坐数学运算。比如 expr 1 + 2。参考 expr --help 查看全部操作。
Python 可以提供 更快捷,方便的运算 服务。

exec

exec 用新的进程 替换当前的 shell 进程。exec 会调用 系统调用 exec()。
执行 exec cat,再ctl+c,会中断连接。因为以前的那个 shell 进程 已经被 替代了。

[root@HTNICE home]# cat /proc/version Linux version 5.10.140 (ylc@ylc-Precision-3571) (aarch64-linux-gcc.br_real (Buildroot 2022.02.6) 10.4.0, GNU ld (GNU Binutils) 2.36.1) #7 SMP Mon Feb 20 12:59:02 CST 2023 [root@HTNICE home]# busybox BusyBox v1.35.0 (2023-02-21 13:21:21 CST) multi-call binary. BusyBox is copyrighted by many authors between 1998-2015. Licensed under GPLv2. See source distribution for detailed copyright notices. Usage: busybox [function [arguments]...] or: busybox --list[-full] or: busybox --show SCRIPT or: busybox --install [-s] [DIR] or: function [arguments]... BusyBox is a multi-call binary that combines many common Unix utilities into a single executable. Most people will create a link to busybox for each function they wish to use and BusyBox will act like whatever it was invoked as. Currently defined functions: [, [[, addgroup, adduser, ar, arch, arp, arping, ascii, ash, awk, base32, base64, basename, bc, blkid, bunzip2, bzcat, cat, chattr, chgrp, chmod, chown, chroot, chrt, chvt, cksum, clear, cmp, cp, cpio, crc32, crond, crontab, cut, date, dc, dd, deallocvt, delgroup, deluser, depmod, devmem, df, diff, dirname, dmesg, dnsd, dnsdomainname, dos2unix, du, dumpkmap, echo, egrep, eject, env, ether-wake, expr, factor, fallocate, false, fbset, fdflush, fdformat, fdisk, fgrep, find, flock, fold, free, freeramdisk, fsck, fsfreeze, fstrim, fuser, getopt, getty, grep, gunzip, gzip, halt, hdparm, head, hexdump, hexedit, hostid, hostname, hwclock, i2cdetect, i2cdump, i2cget, i2cset, i2ctransfer, id, ifconfig, ifdown, ifup, inetd, init, insmod, install, ip, ipaddr, ipcrm, ipcs, iplink, ipneigh, iproute, iprule, iptunnel, kill, killall, killall5, klogd, last, less, link, linux32, linux64, linuxrc, ln, loadfont, loadkmap, logger, login, logname, losetup, ls, lsattr, lsmod, lsof, lspci, lsscsi, lsusb, lzcat, lzma, lzopcat, makedevs, md5sum, mdev, mesg, microcom, mim, mkdir, mkdosfs, mke2fs, mkfifo, mknod, mkpasswd, mkswap, mktemp, modinfo, modprobe, more, mount, mountpoint, mt, mv, nameif, netstat, nice, nl, nohup, nologin, nproc, nslookup, nuke, od, openvt, partprobe, passwd, paste, patch, pidof, ping, ping6, pipe_progress, pivot_root, poweroff, printenv, printf, ps, pwd, rdate, readlink, readprofile, realpath, reboot, renice, reset, resize, resume, rm, rmdir, rmmod, route, run-init, run-parts, runlevel, sed, seq, setarch, setconsole, setfattr, setkeycodes, setlogcons, setpriv, setserial, setsid, sh, sha1sum, sha256sum, sha3sum, sha512sum, shred, sleep, sort, start-stop-daemon, strings, stty, su, sulogin, svc, svok, swapoff, swapon, switch_root, sync, sysctl, syslogd, tail, tar, tc, tee, telnet, telnetd, test, tftp, time, top, touch, tr, traceroute, true, truncate, ts, tty, ubirename, udhcpc, udhcpd, uevent, umount, uname, uniq, unix2dos, unlink, unlzma, unlzop, unxz, unzip, uptime, usleep, uudecode, uuencode, vconfig, vi, vlock, w, watch, watchdog, wc, wget, which, who, whoami, xargs, xxd, xz, xzcat, yes, zcat [root@HTNICE home]# 我尝试使用这个系统进行网安agent探针开发,目前先尝试使用C语言标准库和POSIX标准实现登录成功、退出登录、登陆失败、操作命令、串口数量信息、串口占用、串口释放等事件的日志记录
最新发布
09-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值