vendredi 30 janvier 2015

Unix: Add unique names after signs in text


I have a file looking like the following:



abcdefghijklmnopqrst
//
abcdefghijklmnopqrst
//
abcdefghijklmnopqrst


I use the following code to insert a line with // to the beginning of the document (as advised here: Adding text to beginning of text file), and save this update to a new file.



sed '1s/^/\/\/\'$'\n/' File_1.txt > File_2.txt


What I would like to do, however, is to add unique names after each //, getting a result like this:



// Text 1
abcdefghijklmnopqrst
// Text 2
abcdefghijklmnopqrst
// Text 3
abcdefghijklmnopqrst


etc.


I was thinking I could use tr to translate eg. "//" into "// Text 1". But how do I add Text 2 to the next //, text 3 to the third // etc.? I am complete newbie at this. The txt file is mac-osx made.


UPDATE:


Your solution looks good, @PM 2Ring, however, I have the addiditional problem, that the file actually looks like this:



//
abcdefghijklmnopqrstu
// x x
abcdefghijklmnopqrstu
// x x
abcdefghijklmnopqrstu


So the name is perfectly inserted in first line, but after the secons pair of /, the name is inserted after x, at the end of the line. I need to insert the name just after //, but without shifting the remaining text to the right. Is it possible to delete the same amount of spaces, as the there are characters in the name? This should then not be done in the first line, of course, as I assume this would delete characters from the second line.



Aucun commentaire:

Enregistrer un commentaire