LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 05-05-2016, 07:24 AM   #1
substancev
LQ Newbie
 
Registered: Jul 2011
Posts: 7

Rep: Reputation: Disabled
Insert character in positions then remove all spaces after inserted character


Hello all,

I not familiar with regex and I'm having a hard time.

I have this file with 10 lines of text. The file is a position delimited file and I'm trying to convert it to CSV.

Code:
123456789012                                     0123456                                                     01234                                      01234   0123456789012345678901234567            0123456789012               0123456
What I am trying to do is to insert a comma after the 60, 120, 163, 171, 211,239 positions on each line. Then after it does that to go back and remove all spaces before each comma.

I've tried using sed -i 's/ \{3,\}/,/g' For the most part this works but there are cases where a long field will have 3 spaces in between words and it added commas where it shouldn't

And if someone could also point me in the right direction of learning regex. Its super complicating when I read up on it.

Thanks!
 
Old 05-05-2016, 08:48 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
Are the fields widths consistant, from your example if I use perl's unpack:
Code:
perl -lne 'print join ",", unpack("A49 A60 A43 A8 A40 A28 A7")' file.txt

-- output --
123456789012,0123456,01234,01234,0123456789012345678901234567,0123456789012,0123456
 
1 members found this post helpful.
Old 05-05-2016, 08:53 AM   #3
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,539

Rep: Reputation: 1419Reputation: 1419Reputation: 1419Reputation: 1419Reputation: 1419Reputation: 1419Reputation: 1419Reputation: 1419Reputation: 1419Reputation: 1419
Perhaps simply 'column -t -o, file.txt' would do what you want.
 
Old 05-05-2016, 11:25 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
I am with allend if we are simply removing white space between the numbers shown and replacing that with a comma?
 
Old 05-05-2016, 12:04 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
Op said
Quote:
there are cases long field will have 3 spaces in between words and it added commas where it shouldn't
Hoping he/she will come back with more infos...
 
Old 05-05-2016, 12:50 PM   #6
substancev
LQ Newbie
 
Registered: Jul 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
I can't depend on the spaces between numbers since the numbers could have spaces of 3 or more in between. I'm hoping keefaz's solution will work.
 
Old 05-05-2016, 01:01 PM   #7
substancev
LQ Newbie
 
Registered: Jul 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
perl -lne 'print join ",", unpack("A60 A60 A43 A8 A40 A28 A11")'

This did the trick. Now how do I make it permenant
 
Old 05-05-2016, 01:12 PM   #8
substancev
LQ Newbie
 
Registered: Jul 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
> seems to work Thanks guys... Hopefully i don't get any more surprises
 
Old 05-05-2016, 01:47 PM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
Quote:
Originally Posted by substancev View Post
perl -lne 'print join ",", unpack("A60 A60 A43 A8 A40 A28 A11")'

This did the trick. Now how do I make it permenant
Nice, so the fields were fixed, no?

For making it permanent you could write it in a file?
 
Old 05-05-2016, 02:52 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
So I am not sure if it is just me, but whilst the new numbers in unpack help with the comma insertion, I also appear to still have all the white space as well ... was this the desired outcome?
Code:
$ perl -lne 'print join ",", unpack("A60 A60 A43 A8 A40 A28 A11")' file
123456789012                                     0123456,                                                 01234,                                01234   012,34567890,12345678901234567            01234567890,12,
On top of that, why then did the original perl remove the spaces??
 
Old 05-05-2016, 04:16 PM   #11
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
The Op adjusted the field width according to the real data I think
(note how the field widths values (A60...) differ in his response and in my posted exemple)

Note sure I get your second question, perl automatically removes trailing whitespaces with the A pack format.
 
1 members found this post helpful.
Old 05-06-2016, 09:06 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Yes I did know about the 'A' pack format but wondered why they survived, I know see that your numbering was to terminate before the start of each new set of numbers ... cheers
 
  


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] Sed insert before character VladC Linux - Newbie 3 03-15-2012 12:44 AM
[SOLVED] Vim: Insert 2 spaces after 29th character of every line porphyry5 Programming 3 09-17-2011 03:34 PM
[SOLVED] How can I extract strings based on character positions? btacuso Linux - Newbie 8 03-25-2010 12:31 PM
insert character into a variable jadeddog Programming 4 11-04-2008 03:39 PM
I always have spaces between each character I type markraem Linux - General 2 01-26-2004 06:15 AM


All times are GMT -5. The time now is 10:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration