Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
08-22-2017, 08:54 AM
|
#1
|
LQ Newbie
Registered: Aug 2017
Posts: 2
Rep: 
|
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
Thanks in advance and appreciate your help.
|
|
|
08-22-2017, 09:02 AM
|
#2
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,334
|
Homework ?. LQ has rules about that, which you should have read.
|
|
1 members found this post helpful.
|
08-22-2017, 09:05 AM
|
#3
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,939
|
Hi jigneshpatel87 and welcome to LQ.
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
|
|
1 members found this post helpful.
|
08-22-2017, 09:05 AM
|
#4
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,679
|
Welcome.
I would look at sed, awk, or perl for that. The s/// command in sed is a good way to get started with that:
Code:
sed -e 's///; s///; s///; s///; s///;' < input.txt > output.txt
For an authoritative reference see
Code:
man sed
man 7 regex
For a tutorial or guide, try these:
|
|
1 members found this post helpful.
|
08-22-2017, 09:20 AM
|
#5
|
LQ Newbie
Registered: Aug 2017
Posts: 2
Original Poster
Rep: 
|
Quote:
Originally Posted by rtmistler
Hi jigneshpatel87 and welcome to LQ.
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.
|
|
|
08-23-2017, 02:42 AM
|
#6
|
LQ Guru
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
|
I'll give a hint. You're trying to make a delimited file with "~" as the delimiting character.
|
|
|
08-23-2017, 05:21 AM
|
#7
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 3,019
|
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
Code:
s/\(.\{4\}\)\(.\{3\}\)/\1,\2/;
|
|
|
08-23-2017, 07:31 AM
|
#8
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,679
|
Quote:
Originally Posted by MadeInGermany
But it is easier to have one big s command, . . .
|
Quite right. Now that I think about it another time, that seems to be the way to go.
|
|
|
08-23-2017, 11:47 AM
|
#9
|
Member
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634
|
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))
EDIT:
notebook preview of code output and refactor using a function for inserting the delimiter.
https://gist.github.com/anonymous/ca...ed379fd1211395
Code:
#!/usr/bin/env python3
def insert_delimiter(string, locations, delimiter='|'):
string = list(string)
locations = sorted(list(locations), reverse=True)
for location in locations:
string.insert(location, delimiter)
string = ''.join(string)
string = string.strip('\n {}'.format(delimiter))
return string
locations = [10, 14, 20, 23, 30]
with open('delimited_file') as _f:
for row in _f.readlines():
delimited_row = insert_delimiter(row, locations=locations, delimiter='~')
print(delimited_row)
Last edited by Sefyir; 08-28-2017 at 07:44 PM.
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 07:18 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|