How to insert "~" at specific locations (multiple times in a single line) in .txt file
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to insert "~" at specific locations (multiple times in a single line) in .txt file
Hi All,
I am new on Linux.
My requirements is I have to insert "~" symbol at many specific location in single line in a .txt file.
I have to insert "~" at flowing locations.
first "~" at after 10 character
second "~" at after 4 character
third "~" at after 6 character
forth "~" at after 3 character
fifth "~" at after 7 character
Input .txt file as below:
SDBPRSNL100390ANNUALSDBPAYMENT
Output in .txt file should be as below:
SDBPRSNL10~0390~ANNUAL~SDB~PAYMENT
Please review the guidelines for how to prepare your question and ask it more effectively in the LQ FAQ
Note that you have presented your question with highly specific things to do, however you have given no background about what attempts you have done to solve this problem, what language you intend to solve this problem with, or what experience you have with scripting or languages. You also have not shown any representative data from your input file.
People here will be happy to help you, however you should show some effort and also note that if this is an assignment, it is not bad that it is, however please be considerate enough to note that it is, as well as to not post any assignments verbatim, per the LQ Rules
Please review the guidelines for how to prepare your question and ask it more effectively in the LQ FAQ
Note that you have presented your question with highly specific things to do, however you have given no background about what attempts you have done to solve this problem, what language you intend to solve this problem with, or what experience you have with scripting or languages. You also have not shown any representative data from your input file.
People here will be happy to help you, however you should show some effort and also note that if this is an assignment, it is not bad that it is, however please be considerate enough to note that it is, as well as to not post any assignments verbatim, per the LQ Rules
Thank you very much. I will make sure next time when I post any thread.
Multiple s commands have the principal complication that the insertion changes the positions for the following insertions
Code:
s///; s///; s///; s///;
Of course you can solve it with some calculations and adapted positions.
But it is easier to have one big s command, with multiple \( \) groups and \n back references. A \{m\} repetition of the "any character" directly reflects the given relative positions.
Example: 4 times any character in group 1 and 3 times any character in group 2, in the substitution print group 1 plus a comma plus group 2
By adding to the string, you change the structure of it so that it needs to be offset (as noted)
However, if you make the changes in reverse order, this is no longer true.
I changed the locations from the given "relative" values to more absolute ones. Adding or changing where they go should be more trivial now.
Code:
#locations = [10, 4, 6, 3, 7]
locations = [10, 14, 20, 23, 30]
locations.sort(reverse=True)
dlm = '~'
with open('delimited_file') as _f:
reader = _f.readlines()
for row in reader:
row = list(row)
for location in locations:
row.insert(location, dlm)
print(''.join(row))
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.