LinuxQuestions.org
Register a domain and help support LQ
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 06-04-2013, 10:09 AM   #1
stevanity
LQ Newbie
 
Registered: Jun 2013
Posts: 1

Rep: Reputation: Disabled
Adding lines to each block in a multicolumn text file


Hello!
I have a text file with lots of addresses. I need to add a certain line on top of each address. My file looks like this now:

Code:
g51/b18468 Postgg On 30/05/2013      N51/b39897 Postgg On 30/05/2013      LR51/b23428 Postgg On 30/05/2013
Rgv. XXXXX                           Mr. bBnMbNNbN.M                      Bro. bBRbgbM .S,
KbgbNg bSSgMBLY Og gOg,              LOT 92, KbMPUNg                      gRbgg gOMMUNnTY ggNTRg,
52, gLN. TnMUR,                      BbTU 4, 43950                        4, LORONg SS 23/6g,
POST BOX 20, 43007 KbgbNg            SUNgbn PgLnK                         47400 PgTbLnNg gbYb,
MbLbYSnb                             SgLbNgUR                             SgLbNgOR, W.MbLbYSnb
                                     MbLbYSnb                             MbLbYSnb


ML51/b13179 Postgg On 30/05/2013     W51/b41363 Postgg On 30/05/2013      Lg51/b29053 Postgg On 30/05/2013
Bro. bBRbgbM .V                      Bro. bLbPPbN                         Pbstor. bLgRgg PRbgbSbM
NO:18,PgRSnbRbN                      NO.60, LORONg SgRbn PgRMbn 9,        1-2-2, MggbN nNTbN bPbRTMgNT,
RnSgbg .7                            TbMbN SgRbn PgRMbn,                  gbLbN 1/21g Ogg gbLbN gOMBbK
TbMbN RnSgbg                         34300, BbTbN SgRbn,                  53000 KUbLb LUMPUR
30100 nPOg, PgRbK                    PgRbK                                MbLbYSnb
MbLbYSnb                             MbLbYSnb


LR510b13172 Postgg On 30/05/2013     ML51/b9877 Postgg On 30/05/2013      LR51/b9905 Postgg On 30/05/2013
Sns. bLLbMbg (b) NgSbM               Bro. bLVnN bNbNg .b                  Mrs. bNnTgb VngTOR
NO:44,gbLbN gbMbn                    NO:3,gbLbN TgRbTbn 3                 7,gbLbN PbRn
TbMbN TbPbg gbYb                     TbMbN SbRn,TgRbTbn                   PbRn gbRggNS
35000 TbPbg                          44000 KUbLb KUBU BgbRU               nPOg,30100
PgRbK,                               SgLbNgOR                             PgRbK
MbLbYSnb                             MbLbYSnb                             MbLbYSnb


ML51/b13180 Postgg On 30/05/2013     ML51/b13203 Postgg On 30/05/2013     g51/b9942 Postgg On 30/05/2013
Bro. bNTONY                          Bro. bNTONYSWbMY                     Pbstor. bROKnbNbTgbN
NO:399,LORONg KgNbVn 3/2b            LbgbNg gUSUN gURnbM                  NO:M4 g/7
TbMbN KgNbRn                         42700 BbNTnNg                        gbLbN PbNgbN nNgbg 4/1b
09000 KULnM                          SgLbNgOR g.g                         PbNgbN nNgbg
Kggbg                                MbLbYSnb                             55100,KUbLb LUMPUR
MbLbYSnb                                                                  MbLbYSnb
I want it to look like this:
Code:
My New Line                          My New Line                          My New Line
g51/b18468 Postgg On 30/05/2013      N51/b39897 Postgg On 30/05/2013      LR51/b23428 Postgg On 30/05/2013
Rgv. XXXXX                           Mr. bBnMbNNbN.M                      Bro. bBRbgbM .S,
KbgbNg bSSgMBLY Og gOg,              LOT 92, KbMPUNg                      gRbgg gOMMUNnTY ggNTRg,
52, gLN. TnMUR,                      BbTU 4, 43950                        4, LORONg SS 23/6g,
POST BOX 20, 43007 KbgbNg            SUNgbn PgLnK                         47400 PgTbLnNg gbYb,
MbLbYSnb                             SgLbNgUR                             SgLbNgOR, W.MbLbYSnb
                                     MbLbYSnb                             MbLbYSnb
for all addresses ofc while maintaining same formatting. I dont know how to achieve this. Can you guys help me out with this?

Your help is much appreciated!

Thank you.
 
Old 06-04-2013, 11:43 AM   #2
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 304

Rep: Reputation: 121Reputation: 121
In which word processor or programming language are you planning to accomplish this? What have you tried so far?

If every address field has the same height and width, including the blank padding below and to the right of each address, you don't even have to do anything sophisticated like searching for patterns. You could just write a word processor script (or macro) to repeatedly count lines and insert the new text. The new text would not be
Code:
My New Line
but rather
Code:
My New Line                          My New Line                          My New Line
 
Old 06-04-2013, 11:47 AM   #3
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 304

Rep: Reputation: 121Reputation: 121
By the way, I have suggested a way to handle this as a one-time problem. If you plan to maintain this address list and modify it in the future, you really should rearrange it into one column or some other structure or database that will make your future work easier.
 
Old 06-04-2013, 12:04 PM   #4
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
I can give you an idea.
When more than two blank lines are appeared, Replace last blank line with your new line.

And in the same line add your new line again after certain charaters.
 
Old 06-04-2013, 04:12 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
This works with the exact text above. You just have to get the line formatting right.
Code:
awk 'BEGIN{ RS=ORS="\n\n\n"; str="My New Line" ; sp="                          " } { $0=str sp str sp str"\n" $0 ; print }'  input.txt
The input and output separators have been set to three contiguous newlines, and each record is simply prepended with the desired string and then printed. I used variables for the string and padding spaces just to compact things a bit.

It could probably be done somewhat cleaner with printf instead, but this works well enough.

Finally, I agree with Beryllos. Lists of database-style entries are easier to manipulate if each record is kept separate.
 
2 members found this post helpful.
Old 06-04-2013, 05:04 PM   #6
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 304

Rep: Reputation: 121Reputation: 121
slightly off topic, but fun...

stevanity, It appears that your sample text is a substitution cipher. Before we all have some fun cracking it, I need to ask you if it is important for reasons of privacy or security to keep the names and addresses secret. If so, you should delete them.
 
Old 06-05-2013, 03:55 AM   #7
unclesamcrazy
Member
 
Registered: May 2013
Posts: 187

Rep: Reputation: 1
@David the H.

Excellent !!!

Can you please explain, what did you exactly do to achieve this using awk?

I have understood
Quote:
str="My New Line" ; sp=" " }
both are equal to 37 characters, exact space between two addresses but I could not understand $0. what does it do?
As well as I could not understand the functionality of RS=ORS="\n\n\n"; & "\n" $0 ;

Can you please explain this?

Thanks !!!

Last edited by unclesamcrazy; 06-05-2013 at 05:44 AM.
 
Old 06-06-2013, 02:06 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Well, a full explanation really requires understanding something of how awk works. Check out the links below.

But in brief, awk divides the input into records, and then further subdivides the records into fields. By default a record is a line, and a field is a word, but this can be changed. RS is the input record separator variable, which I set to match three consecutive newlines. ORS is the output record separator, which needs to be set to the same as the input if you want to keep the same formatting.

$0 refers to the current record as a whole. So the command just re-sets it to be equal to the new line plus itself, then prints it.

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/series/awk-one-liners-explained
 
Old 06-12-2014, 02:51 AM   #9
karim.ouda
Member
 
Registered: Jun 2013
Posts: 41

Rep: Reputation: 3
It doesn't look real data but if Postgg On and date format is common in first line of every record, it may work.
Code:
sed 's/\(.*\)Postgg On [0-9][0-9]\/[0-9][0-9]\(.*\)/My New Line                          My New Line                          My New Line\n&/' input.txt
 
  


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
Need a script to break a block of text on to different lines. Thaidog Programming 13 12-17-2010 05:46 AM
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 11:59 AM
Adding lines of text to beginning of a text file BillKat Programming 2 01-19-2009 11:40 AM
Extract lines NOT on a block of text from a file Renan_S2 Programming 3 10-05-2008 05:14 PM
adding text to lines in a file tpreitano Linux - General 2 10-04-2005 10:30 AM


All times are GMT -5. The time now is 09:22 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