1

I have a simple text file in below format.

1 12658003Y 2 34345345N 3 34653785Y 4 36452342N 5 86747488Y 6 34634543Y so on 10 37456338Y 11 33535555Y 12 37456378Y so on 100 23432434Y 

As you can see there are two white spaces after first number.

I'm trying to write SED command to remove the digits before whitespaces. Is there any SED command to remove spaces and number before spaces?

Output file should look like below.

12658003Y 34345345N 34653785Y 36452342N so on.. 

Please assist. I'm very new to shell scripting.

3
  • Did you see the manual yet? Commented Jun 27, 2014 at 14:35
  • If you don't absolutely need sed you can try with grep -o '[0-9]+Y' Commented Jun 27, 2014 at 14:35
  • @SandeepDongapure since you are back in the site, you can consider accepting an answer here as well! Commented Mar 19, 2015 at 12:27

4 Answers 4

1
sed 's/[0-9]\+\s\+//' infile > outfile 

Explanation:

s: we want to use substitution

/: mark start and end of the expression we want to match

[0-9]: match any digit

+: match the previous one or more time

\s: space

+: match the previous one or more time

/: mark start of what we want to change our matches to (which is nothing)

/: some special operators goes after this (we use no such)

infile: the file we want to change

>: pipe stdout to

outfile: where we want to store output

Sign up to request clarification or add additional context in comments.

1 Comment

Note that \+ is a documented GNU extension, and \s seems to be an undocumented one, as far as I can tell. They may not work with other versions of sed.
0

Your sed command would be,

sed 's/.* //g' file 

This would remove the first numbers along with the space followed.

2 Comments

No, that greedily matches everything before the last space, plus the space.
Right, sorry, it would work with this data, because there are only two fields. Not as robust as it could be, though.
0

Remove leading digits, then following spaces:

sed 's/^[0-9]* *//' file 

Comments

0
sed 's/^[0-9]*[ ]*//g' input.txt 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.