I have a file of 2000 rows and 1 column
1007_s_at1
1007_s_at2
1007_s_at3
1007_s_at4
1007_s_at5
1007_s_at6
1007_s_at7
1007_s_at8
1007_s_at9
1007_s_at10
looks like above, I want to remove the last numeric value after "at". In principle whatever number is in the last should be truncated.
I have tried things like splitting them and then rejoioning it, but it just complicates the problem and I am far away from answer.
Could you please suggest something in bash or shell or python or perl to solve this.
An output like below is desired
1007_s_at
1007_s_at
1007_s_at
1007_s_at
1007_s_at
1007_s_at
1007_s_at
1007_s_at
1007_s_at
1007_s_at
Thank you
使用Perl :
perl -p -e "s/\d+$//" input.txt > output.txt
sed -i -e 's/[[:digit:]]*$//' filename
Just pass string.digits
to .rstrip()
to remove digits from the right-hand side of your strings:
import string
with open('inputfile') as infile, open('outputfile') as outfile:
for line in infile:
outfile.write(line.rstrip().rstrip(string.digits) + '\n')
If the only the number at the end changes you could potentially splice:
>>> a = '1007_s_at1'
>>> a[0:9]
'1007_s_at'
Python
Just strip all digits from the end.
>>> "1007_s_at10".rstrip('01234567890')
'1007_s_at'
If you are using Linux or Unix a simple one liner solution would be:
perl -i.bak -pe 's/\d+$//g' file.txt
if Windows:
perl -i.bak -pe "s/\d+$//g" file.txt
If you already know what it is doing then well and good otherwise, in very simple terms, -i
switch with .bak
would first create a backup of your file.txt
and name it file.txt.bak
.
The -p
option would then loop over the entries in the file and print/save the output in file.txt
after s/\\d+$//g
removes the digits in the end.
Nobody's suggested a bash solution yet:
shopt -s extglob
while read line; do
echo "${line%%*([0-9])}"
done < filename
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.