在Unix上打印出整数文字 [英] Printing out integer literals occurrences on unix

查看:82
本文介绍了在Unix上打印出整数文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用grep命令打印出foo.txt文件中所有出现的整数文字,但是我似乎找不到方法.

I"m trying to use the grep command to print out all occurrences of integer literals in a foo.txt file, but I can't seem to find a way how.

为此,我唯一想到的就是使用[0-9]作为我的模式,但是它会打印出所有带有数字出现的行,而不仅仅是文字.

The only thing I can think of for this is using [0-9] as my pattern, but that prints all lines with occurrences of the number rather than just the literals.

说foo.txt包含:

Say foo.txt contains:

string string1 = "Hello world";
cout << string1.at(2) << endl;
cout << string1at(3) << endl;

那么预期的输出将是:

cout << string1.at(2) << endl;
cout << string1at(3) << endl;

推荐答案

类似于以下内容:

awk -F"[()]" '$2' foo.txt
cout << string1.at(2) << endl;
cout << string1at(3) << endl;

或确保括号中包含数字:

Or to make sure it contains numbers between parentheses:

awk -F"[()]" '$2~/[0-9]+/' file
cout << string1.at(2) << endl;
cout << string1at(3) << endl;

这篇关于在Unix上打印出整数文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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