LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2023, 03:49 AM   #1
rizitis
Member
 
Registered: Mar 2009
Location: Greece,Crete
Distribution: Slackware64-current
Posts: 247

Rep: Reputation: 188Reputation: 188
command to remove or rename numbers


I have thousands of *.txt files which using an command line program can translate them from Greeks to Swahili. Then I emailed them to others volunteers in central Africa and they print them in plain A4 papers and give them to local schools. So children can have access to knowledge...
The problem is that if a line starts with number , program skip that line and not translate it.
example:
Code:
1. blabla 1234
2. blabla 1234
etc
I need a command (sed -i... ?) for the .txt file to remove numbers only if a line start with number.Not all the numbers in txt file.
to be like this:
Code:
. blabla 1234
. blabla 1234
or to replace the number if line start with number with something else that will not break translation program
assume:
Code:
*. blabla 1234
*.blabla 1234
etc...
Then I can translate them without issue.

thank you
 
Old 03-07-2023, 03:54 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,784
Blog Entries: 3

Rep: Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514
There is a Perl-base utility called rename which can process file names using full Perl regular expression pattern matching and indeed even full Perl expressions. It is part of the package "rename". Watch out, there is another utility with the same name but fewer capabilities.

Code:
rename -n -v 's/^\d+\.\s+//' *.txt
You can use the -n option for a dry run. See "man rename" for the details.

Last edited by Turbocapitalist; 03-07-2023 at 03:55 AM.
 
1 members found this post helpful.
Old 03-07-2023, 03:58 AM   #3
rizitis
Member
 
Registered: Mar 2009
Location: Greece,Crete
Distribution: Slackware64-current
Posts: 247

Original Poster
Rep: Reputation: 188Reputation: 188
thank you. i will try it and give feed back in few hours...
 
Old 03-07-2023, 04:27 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,751

Rep: Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972Reputation: 3972
I think the OP wants to edit lines within the files rather than rename them. Something like this might do (not that I like using -i)
Code:
sed -ir 's/^[[:digit:]]+//' *.txt
Test it on a small subset of copies of your files - or better get sed to take backups for you.
 
1 members found this post helpful.
Old 03-07-2023, 04:35 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,880

Rep: Reputation: 6734Reputation: 6734Reputation: 6734Reputation: 6734Reputation: 6734Reputation: 6734Reputation: 6734Reputation: 6734Reputation: 6734Reputation: 6734Reputation: 6734
if I understand well you want to do something like this (I don't like -i too):
Code:
sed -ir 's/^[^[:alpha:]]*//' *.txt
 
1 members found this post helpful.
Old 03-07-2023, 04:38 AM   #6
rizitis
Member
 
Registered: Mar 2009
Location: Greece,Crete
Distribution: Slackware64-current
Posts: 247

Original Poster
Rep: Reputation: 188Reputation: 188
sorry for my english... i will try to make it clear.. i hope.
i have this example.txt
Code:
1. Οι μεταφράσεις χρειάζονται επιβεβαίωση από κάποιον γνώστη της γλώσσας Σουαχίλι. 1234
2. Για την ώρα έχουμε μια αυτόματη μετάφραση κάποιου προγράμματος και ενδέχεται να υπάρχουν αστοχίες ίσως και σοβαρές. 
4. Είναι όμως μια αρχή κι αυτό... 1234
23. Όποιος μπορεί να διορθώσει και έχει τη γνώση και (1-3) τη διάθεση ας το κάνει, παρακαλώ να με ενημερώνει να διορθώνω κι εγώ εδώ τα κείμενα.
and i want to make it like this
Code:
. Οι μεταφράσεις χρειάζονται επιβεβαίωση από κάποιον γνώστη της γλώσσας Σουαχίλι. 1234
. Για την ώρα έχουμε μια αυτόματη μετάφραση κάποιου προγράμματος και ενδέχεται να υπάρχουν αστοχίες ίσως και σοβαρές. 
. Είναι όμως μια αρχή κι αυτό... 1234
. Όποιος μπορεί να διορθώσει και έχει τη γνώση και (1-3) τη διάθεση ας το κάνει, παρακαλώ να με ενημερώνει να διορθώνω κι εγώ εδώ τα κείμενα.
or like this
Code:
*. Οι μεταφράσεις χρειάζονται επιβεβαίωση από κάποιον γνώστη της γλώσσας Σουαχίλι. 1234
*. Για την ώρα έχουμε μια αυτόματη μετάφραση κάποιου προγράμματος και ενδέχεται να υπάρχουν αστοχίες ίσως και σοβαρές. 
*. Είναι όμως μια αρχή κι αυτό... 1234
*. Όποιος μπορεί να διορθώσει και έχει τη γνώση και (1-3) τη διάθεση ας το κάνει, παρακαλώ να με ενημερώνει να διορθώνω κι εγώ εδώ τα κείμενα.
perl rename in Slackware named rename.pl , i installed it but I think its only for renaming files name only.
I think sed -i is more close what i need , but I cant make it work... as i need.

Last edited by rizitis; 03-07-2023 at 04:49 AM.
 
1 members found this post helpful.
Old 03-07-2023, 04:40 AM   #7
rizitis
Member
 
Registered: Mar 2009
Location: Greece,Crete
Distribution: Slackware64-current
Posts: 247

Original Poster
Rep: Reputation: 188Reputation: 188
Quote:
Originally Posted by pan64 View Post
if I understand well you want to do something like this (I don't like -i too):
Code:
sed -ir 's/^[^[:alpha:]]*//' *.txt
YES THANKS! tha was exactly what i need! thank you all !
 
Old 03-07-2023, 05:34 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,307

Rep: Reputation: 1021Reputation: 1021Reputation: 1021Reputation: 1021Reputation: 1021Reputation: 1021Reputation: 1021Reputation: 1021
Substitute leading digits by a #
Code:
sed -ir 's/^[[:digit:]]+/#/' *.txt
or
Code:
sed -ir '/^[[:digit:]]+/ s//#/' *.txt
Prefix a # to a leading digit
Code:
sed -ir 's/^[[:digit:]]/#&/' *.txt
or
Code:
sed -ir '/^[[:digit:]]/ s/^/#/' *.txt

Last edited by MadeInGermany; 03-07-2023 at 05:44 AM.
 
Old 03-08-2023, 06:17 AM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,129

Rep: Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213Reputation: 2213
Quote:
Originally Posted by rizitis View Post
The problem is that if a line starts with number , program skip that line and not translate it.
What's stopping this bug from being fixed?

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How to Bulk Rename Files in Linux with Thunarís Bulk Rename Tool LXer Syndicated Linux News 0 11-27-2019 07:03 PM
LXer: GUI To Batch Rename Files On Linux With Exif And Music Tags Support: Inviska Rename LXer Syndicated Linux News 0 05-25-2019 12:07 PM
LXer: How to rename user in Linux (also rename group & home directory) LXer Syndicated Linux News 0 06-26-2018 12:54 PM
LXer: How to rename user in Linux (also rename group & home directory) LXer Syndicated Linux News 0 11-28-2017 07:53 PM
Rename network interface (Rename ppp0 ...) Nicolas1390 Linux - Newbie 2 08-08-2011 02:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration