Home > Net >  how can I remove some numbers at the end of line in a text file
how can I remove some numbers at the end of line in a text file

Time:08-20

I have a text file which contains a series of same line except at the end. eg

lesi-1-1500-1

lesi-1-1500-2

lesi-1-1500-3

how can I remove the last number? it goes upto 250

CodePudding user response:

You can do it with Awk by breaking it into fields.

echo "lesi-1-1500-2" > foo.txt
echo "lesi-1-1500-3" >> foo.txt

cat foo.txt | awk -F '-' '{print $1 "-" $2 "-" $3 }'

The -F switch allows us to set the delimiter which is -. Then we just print the first three fields with - for formatting.

CodePudding user response:

to change in the file itself

sed -i 's/[0-9]\ $//' /path/to/file

or

sed 's/[0-9]\ $//' /path/to/file > /path/to/output

see example

  • Related