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.
|
 |
06-04-2013, 09:09 AM
|
#1
|
LQ Newbie
Registered: Jun 2013
Posts: 1
Rep: 
|
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.
|
|
|
06-04-2013, 10:43 AM
|
#2
|
Member
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529
|
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
but rather
Code:
My New Line My New Line My New Line
|
|
|
06-04-2013, 10:47 AM
|
#3
|
Member
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529
|
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.
|
|
|
06-04-2013, 11:04 AM
|
#4
|
Member
Registered: Mar 2013
Posts: 650
Rep: 
|
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.
|
|
|
06-04-2013, 03:12 PM
|
#5
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
|
06-04-2013, 04:04 PM
|
#6
|
Member
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529
|
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.
|
|
|
06-05-2013, 02:55 AM
|
#7
|
Member
Registered: May 2013
Posts: 200
Rep:
|
@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 04:44 AM.
|
|
|
06-06-2013, 01:06 PM
|
#8
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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
|
|
|
06-12-2014, 01:51 AM
|
#9
|
Member
Registered: Jun 2013
Posts: 41
Rep:
|
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
|
|
|
All times are GMT -5. The time now is 03:17 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
|
|