shell文本行截取子串

博客介绍了Shell中对文本的操作,包括格式化文本内容展示,使用`cut -c 72-`命令从指定位置开始截取文本,还详细说明了`cut`命令的截取参数,如`-b`、`-c`、`-d`等选项的作用及参数格式表示范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、格式化的文本内容

# head 2620.log
logs/mdfs-monitor.nwp.py.log.10:2018-08-27 01:12:37,902 - root - INFO - rpush "filename:/nwp" "cassandra:/GRAPES_GFS/PRMSL/18082620.084"
logs/mdfs-monitor.nwp.py.log.10:2018-08-27 01:12:40,683 - root - INFO - rpush "filename:/nwp" "cassandra:/GRAPES_GFS/TMP_CHANGE_24H/850/18082620.087"
logs/mdfs-monitor.nwp.py.log.10:2018-08-27 01:12:41,326 - root - INFO - rpush "filename:/nwp" "cassandra:/GRAPES_GFS/PRMSL_CHANGE_24H/18082620.084"
logs/mdfs-monitor.nwp.py.log.10:2018-08-27 01:14:13,859 - root - INFO - rpush "filename:/nwp" "cassandra:/GRAPES_GFS/TMP/925/18082620.090"
logs/mdfs-monitor.nwp.py.log.10:2018-08-27 01:14:13,987 - root - INFO - rpush "filename:/nwp" "cassandra:/GRAPES_GFS/HGT/925/18082620.090"
logs/mdfs-monitor.nwp.py.log.10:2018-08-27 01:14:14,238 - root - INFO - rpush "filename:/nwp" "cassandra:/GRAPES_GFS/RH/925/18082620.087"
logs/mdfs-monitor.nwp.py.log.10:2018-08-27 01:14:14,238 - root - INFO - rpush "filename:/nwp" "cassandra:/GRAPES_GFS/RH/925/18082620.084"
logs/mdfs-monitor.nwp.py.log.10:2018-08-27 01:14:14,361 - root - INFO - rpush "filename:/nwp" "cassandra:/GRAPES_GFS/SPFH/925/18082620.087"
logs/mdfs-monitor.nwp.py.log.10:2018-08-27 01:14:14,639 - root - INFO - rpush "filename:/nwp" "cassandra:/GRAPES_GFS/VVEL_GEOMETRIC/925/18082620.087"
logs/mdfs-monitor.nwp.py.log.10:2018-08-27 01:14:15,221 - root - INFO - rpush "filename:/nwp" "cassandra:/GRAPES_GFS/TMP/850/18082620.090"

2、从指定位置开始截取

# cut -c 72- 2620.log | head
rpush "filename:/nwp" "cassandra:/GRAPES_GFS/PRMSL/18082620.084"
rpush "filename:/nwp" "cassandra:/GRAPES_GFS/TMP_CHANGE_24H/850/18082620.087"
rpush "filename:/nwp" "cassandra:/GRAPES_GFS/PRMSL_CHANGE_24H/18082620.084"
rpush "filename:/nwp" "cassandra:/GRAPES_GFS/TMP/925/18082620.090"
rpush "filename:/nwp" "cassandra:/GRAPES_GFS/HGT/925/18082620.090"
rpush "filename:/nwp" "cassandra:/GRAPES_GFS/RH/925/18082620.087"
rpush "filename:/nwp" "cassandra:/GRAPES_GFS/RH/925/18082620.084"
rpush "filename:/nwp" "cassandra:/GRAPES_GFS/SPFH/925/18082620.087"
rpush "filename:/nwp" "cassandra:/GRAPES_GFS/VVEL_GEOMETRIC/925/18082620.087"
rpush "filename:/nwp" "cassandra:/GRAPES_GFS/TMP/850/18082620.090"

3、截取参数说明

# cut --help
用法:cut [选项]... [文件]...
Print selected parts of lines from each FILE to standard output.

Mandatory arguments to long options are mandatory for short options too.
-b, --bytes=列表 只选中指定的这些字节
-c, --characters=列表 只选中指定的这些字符
-d, --delimiter=分界符 使用指定分界符代替制表符作为区域分界
-f, --fields=LIST select only these fields; also print any line
that contains no delimiter character, unless
the -s option is specified
-n with -b: don't split multibyte characters
--complement 补全选中的字节、字符或域
-s, --only-delimited 不打印没有包含分界符的行
--output-delimiter=字符串 使用指定的字符串作为输出分界符,默认采用输入
的分界符
--help 显示此帮助信息并退出
--version 显示版本信息并退出

仅使用f -b, -c 或-f 中的一个。每一个列表都是专门为一个类别作出的,或者您可以用逗号隔
开要同时显示的不同类别。您的输入顺序将作为读取顺序,每个仅能输入一次。
每种参数格式表示范围如下:
N 从第1 个开始数的第N 个字节、字符或域
N- 从第N 个开始到所在行结束的所有字符、字节或域
N-M 从第N 个开始到第M 个之间(包括第M 个)的所有字符、字节或域
-M 从第1 个开始到第M 个之间(包括第M 个)的所有字符、字节或域

当没有文件参数,或者文件不存在时,从标准输入读取

GNU coreutils online help: <http://www.gnu.org/software/coreutils/&gt;
请向<http://translationproject.org/team/zh_CN.html&gt; 报告cut 的翻译错误
要获取完整文档,请运行:info coreutils 'cut invocation'

转载于:https://blog.51cto.com/dressame/2164839

在 C++ 中,`std::string` 提供了 `substr()` 方法用于截取子字符串。该方法接受两个参数:起始位置和子串长度。如果省略第二个参数,则会截取从起始位置到字符串末尾的所有字符。 ### 常用语法 ```cpp std::string substr(size_t pos = 0, size_t len = npos) const; ``` - `pos`:起始索引(从 0 开始)。 - `len`:要截取的字符数。若未指定或超出字符串长度,则截取至末尾。 ### 示例代码 ```cpp #include <iostream> #include <string> int main() { std::string str = "Hello, World!"; // 截取从索引 7 开始直到末尾的子串(输出 "World!") std::string sub1 = str.substr(7); std::cout << "sub1: " << sub1 << std::endl; // 截取从索引 0 开始,长度为 5 的子串(输出 "Hello") std::string sub2 = str.substr(0, 5); std::cout << "sub2: " << sub2 << std::endl; // 截取中间部分(输出 "ell") std::string sub3 = str.substr(1, 3); std::cout << "sub3: " << sub3 << std::endl; return 0; } ``` ### 注意事项 - 索引越界不会抛出异常,但可能导致不可预期的结果,因此建议提前验证索引范围。 - 若字符串包含中字符或其他多字节字符,应考虑使用宽字符 `std::wstring` 并结合 `MultiByteToWideChar` 进行编码转换[^4]。 ### 字符串中查找并提取特定子串 当需要从路径等复杂字符串中提取特定部分时,可以结合 `find_last_of()` 或 `find()` 来定位边界,再使用 `substr()` 提取内容。例如,提取 `"E:\\数据\\2018\\2000坐标系\\a.shp"` 中的 `"2000坐标系"`: ```cpp #include <iostream> #include <string> int main() { std::string strPath = "E:\\数据\\2018\\2000坐标系\\a.shp"; std::string::size_type nPos1 = strPath.find_last_of("\\"); std::string::size_type nPos2 = strPath.find_last_of("\\", nPos1 - 1); if (nPos1 != std::string::npos && nPos2 != std::string::npos) { std::string folderName = strPath.substr(nPos2 + 1, nPos1 - nPos2 - 1); std::cout << "Extracted folder name: " << folderName << std::endl; // 输出 "2000坐标系" } return 0; } ``` 通过这种方式,可以在复杂的字符串结构中灵活地提取所需信息[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值