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 Code:
My New Line My New Line My New Line Your help is much appreciated! :) Thank you. |
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 Code:
My New Line My New Line My New Line |
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.
|
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. |
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 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. |
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. |
@David the H.
Excellent !!! Can you please explain, what did you exactly do to achieve this using awk? I have understood Quote:
As well as I could not understand the functionality of RS=ORS="\n\n\n"; & "\n" $0 ; Can you please explain this? Thanks !!! :) |
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 |
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 12:43 PM. |