在bash shell中的文本行数读 [英] Reading numbers from a text line in bash shell

查看:143
本文介绍了在bash shell中的文本行数读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个bash shell脚本,打开某个文件 CATALOG.dat ,包含以下行,取得字符和数字的:

I'm trying to write a bash shell script, that opens a certain file CATALOG.dat, containing the following lines, made of both characters and numbers:

event_0133_pk.gz
event_0291_pk.gz
event_0298_pk.gz
event_0356_pk.gz
event_0501_pk.gz

我想什么做的是打印新的文件中的数字(只有数字) NUMBERS.dat ,使用类似> ./NUMBERS.dat ,可以得到:

What I wanna do is print the numbers (only the numbers) inside a new file NUMBERS.dat, using something like > ./NUMBERS.dat, to get:

0133
0291
0298
0356
0501

我的问题是:我怎么提取文本行的数字?有一件让阅读只是数量在C变量,如 event_0%d_pk.gz 脚本/ C ++?

推荐答案

A 的grep 解决方案:

grep -oP '[0-9]+' CATALOG.dat >NUMBERS.dat

A SED 解决方案:

sed 's/[^0-9]//g' CATALOG.dat >NUMBERS.dat

和一个 AWK 解决方案:

awk -F"[^0-9]+" '{print $2}' CATALOG.dat >NUMBERS.dat

这篇关于在bash shell中的文本行数读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆