Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sed to delete leading digits?

Tags:

linux

sed

I have a file:

$ cat file 
1 Fred
2 Fred3
3 Fred3
4 Fred3
5 Fred3
6 Fred3
7 Fred3
8 Fred3
9 Fred3
10 Fred3
11 Fred3
12 Fred288

I want to delete the leading numbers, the output should be like this:

$ cat file 
Fred
Fred3
Fred3
Fred3
Fred3
Fred3
Fred3
Fred3
Fred3
Fred3
Fred3
Fred288

How can I achieve this goal using sed?

like image 275
Yishu Fang Avatar asked Oct 21 '25 04:10

Yishu Fang


2 Answers

With sed:

sed 's/^[0-9]\+ //' file
Fred
Fred3
Fred3
Fred3
Fred3
Fred3
Fred3
Fred3
Fred3
Fred3
Fred3
Fred288

Do sed -i 's/^[0-9]\+ //' file to store the changes back to the file.


I would also recommend using cut for this

cut -d' ' -f2 file

Options:

-d, --delimiter=DELIM use DELIM instead of TAB for field delimiter

-f, --fields=LIST select only these fields; also print any line that contains no delimiter character, unless the -s option is specified

like image 161
Chris Seymour Avatar answered Oct 23 '25 19:10

Chris Seymour


This will print the second word in each line:

awk '{print $2}' file
like image 40
P.P Avatar answered Oct 23 '25 20:10

P.P