Learner_51
Learner_51

Reputation: 1075

How to remove words from a file in UNIX?

first file of information page
name/joe/salary1    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

Upvotes: 1

Views: 5617

Answers (5)

hluk
hluk

Reputation: 6016

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

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

Upvotes: 0

codaddict
codaddict

Reputation: 455020

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

Upvotes: 1

mouviciel
mouviciel

Reputation: 67829

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

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 342363

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

Upvotes: 2

wkl
wkl

Reputation: 79921

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.

Upvotes: 1

Related Questions