bash - Deleting the word and the word/character next to it in a string inside file
问题描述
I am looking to solve a particular problem in Bash scripting
Consider file.txt
AAAA BBBB CCCC D EEEE F
I am looking to delete CCCC and D and restore the space it occupies so that there is not extra space as such
I have tried using sed but to no avail. Tried using the s option but it is working only for the string where I can replace it with ''
The expected result is
AAAA BBBB EEEE F
解决方案
If I'm correctly interpreting that "extra space" means more than one space, then I'm confused as to why the expected result has two spaces between "BBBB" and "EEEE". Assuming you really meant one space, the following should work:
$ echo "AAAA BBBB CCCC D EEEE F " | sed 's/CCCC D//g' | tr -s ' '
AAAA BBBB EEEE F
推荐阅读
- c# - 如何从 3 个输入中生成一个数字
- node.js - 在反应中为ckfinder设置服务器目录
- python - 在 Pandas/Jupyter 中使用日期和时间列作为 DatetimeIndex?
- matlab - 我想用matlab制作一个csv文件,但我的csv很奇怪
- javascript - 创建 ISO 格式的 UTC 时间戳
- javascript - 反应 axios 试图发送数据对象和图像文件,但服务器不会读取文件
- javascript - 无法将数据推送到 FormArray 中的 mat-table 并且该表单处于循环中所以我有多个表单每个表单都有一个 mat-table
- python - Python 在新列数据框中生成 16 个随机数字和字母
- reactjs - DatePicker react-datepicker 不读取 NVDA 中的日期,宣布空白
- embedded - Avrdude 无法使用 usbasp 检测到 mcu