LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 08-22-2017, 07:54 AM   #1
jigneshpatel87
LQ Newbie
 
Registered: Aug 2017
Posts: 2

Rep: Reputation: Disabled
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.
 
Old 08-22-2017, 08:02 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,713

Rep: Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549
Homework ?. LQ has rules about that, which you should have read.
 
1 members found this post helpful.
Old 08-22-2017, 08:05 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,319
Blog Entries: 13

Rep: Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372
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.
Old 08-22-2017, 08:05 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,615
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
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.
Old 08-22-2017, 08:20 AM   #5
jigneshpatel87
LQ Newbie
 
Registered: Aug 2017
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
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.
 
Old 08-23-2017, 01:42 AM   #6
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
I'll give a hint. You're trying to make a delimited file with "~" as the delimiting character.
 
Old 08-23-2017, 04:21 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,744

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
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/;
 
Old 08-23-2017, 06:31 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,615
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
Quote:
Originally Posted by MadeInGermany View Post
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.
 
Old 08-23-2017, 10:47 AM   #9
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 633

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
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 06:44 PM.
 
1 members found this post helpful.
  


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
[SOLVED] insert a "space" on every line in a file at character 27 fortelr0923 Linux - Newbie 2 07-27-2012 12:00 AM
[SOLVED] Script to insert line into file that contains multiple """ characters tara Programming 2 02-15-2012 06:43 PM
bad: Want to insert a line into a text file using "sed" command eliote Linux - General 7 09-19-2010 02:55 AM
how to "split" a domain's email between multiple locations kishorerhce508 Linux - Server 6 06-22-2009 11:34 AM
Copying single file to multiple locations Ashiro Linux - Server 4 12-05-2008 05:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:14 PM.

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