开发者

How to remove words from a file in UNIX?

first file of information page
name/joe/sa开发者_高级运维lary1    50  10  2
name/don/miles2
                   20   4   3
name/sam/lb3        0   200 50

can some one please tell me how can I remove all the words in the above file, so my output will looks as follows

    50  10  2

    20  4   3
    0   200 50


Use awk instead. The following code says to go through each field, check if its an integer. If it is, print them out. No need complicated regex.

$ awk '{for(i=1;i<=NF;i++) if($i+0==$i) {printf $i" "} print ""}'  file

50 10 2

20 4 3
0 200 50


sed -e "s/[a-zA-Z/]/ /g" file

will do it, though I like codaddict's way more if you want to preserver number and whitespace. This way strips out all letters and the '/' symbol, replacing them all with space.

If you want to modify the file in place, pass the -i switch. This command will output what the file would look like.


Looks like you want to preserve only the digits and the space. If yes, you can do:

sed 's/[^0-9 ]//g' inputFile

EDIT: Change in requirements, if a digit is found with a letter, it should be treated as part of the word.

This Perl script does it:

perl -ne 's/(?:\d*[a-z\/]+\d*)*//g;print' input


If your file has this structure, I suggest first to filter out the first line, then remove all characters from beginning of line up to the first space:

sed -ni '2,$s/^[^ ]*//p' file


Remove everything on each line until first space character (also removes leading spaces):

sed 's/\S*\s*//' file
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜