The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   Applications (http://hintsforums.macworld.com/forumdisplay.php?f=5)
-   -   Looking for a "add characters" script (http://hintsforums.macworld.com/showthread.php?t=102529)

paldo 06-14-2009 08:25 AM

Looking for a "add characters" script
 
I'm working with text edit and Word for Mac.
I have a list with 110 lines and I want to add at the beginning of each line the same characters ( in my case <track><location>..)and some other characters at the end.
Is there an existing script that can do that for me automatically?
Thank you

cwtnospam 06-14-2009 10:02 AM

What's wrong with using find/replace?
In word:
find: ^p
replace: other_characters^p<track><location>

hayne 06-14-2009 10:58 AM

I'd recommend getting "TextWrangler" (free) and using that instead of TextEdit. TextWrangler has various ways to do what you want - e.g via the Search/Replace dialog.

cwtnospam 06-14-2009 11:22 AM

Yes, TextWrangler is much better. In it, you'd do:

find: \r
replace: other_characters\r<track><option>

paldo 06-14-2009 02:42 PM

Quote:

Originally Posted by cwtnospam (Post 537990)
What's wrong with using find/replace?
In word:
find: ^p
replace: other_characters^p<track><location>

Nothing wrong but thank you, I did not know.
^p is for after the phrase; which is for the beginning of the phase?
Do you know how do I duplicate each single line, one after the other?

Example:
title1
title2
should become:
title1
title1
title2
title2

Thank you.

DaleMox 06-14-2009 02:44 PM

in vi you can do this using the 'I' command (insert at beginning of line)
and the 'A' command (append at end of line)
Also, vi provides for what's called visual block editing ('Ctrl-V') where you
select a block then using 'I'/'A' you can insert/append to that internal block
or use 'd' to delete the block you've selected

I used it extensively when editing some xml and xsl files.

Dale

paldo 06-14-2009 02:50 PM

Sorry, what means vi?

DaleMox 06-14-2009 02:51 PM

Quote:

Originally Posted by paldo (Post 538050)
Nothing wrong but thank you, I did not know.
^p is for after the phrase; which is for the beginning of the phase?
Do you know how do I duplicate each single line, one after the other?

Example:
title1
title2
should become:
title1
title1
title2
title2

Thank you.

Also in vi (but also any text editor with regular expression search and replace functionality)
you can use:
^(.*)$
replaced with:
\1\r\1
but some editors prefer:
$1\r$1

in vi you can use:
%s/^\(.*\)$/\1\r\1/g

(you must input backslash '\' before parenthesis for vi to understand you want to group)

Dale

DaleMox 06-14-2009 02:54 PM

Quote:

Originally Posted by paldo (Post 538053)
Sorry, what means vi?

vi is a text editor that lives in the terminal
(the terminal is found in /Applications/Utilities -> Terminal.app)

However it is a very powerful editor which needs a lot of time to learn,
because it doesn't have a graphical interface like TextEdit

If you are interested in using it, there is MacVim which a bit of
a graphical interface which can help you at the beginning to learn
how to use vi (or vim which is an extension of vi)

MacVim:
http://code.google.com/p/macvim/

paldo 06-15-2009 03:58 AM

Quote:

Originally Posted by cwtnospam (Post 538017)
Yes, TextWrangler is much better. In it, you'd do:

find: \r
replace: other_characters\r<track><option>

You are right, textwrangler is a powerful tool, much better then textedit.
I went through the tutorial but I did not found the following:
a)find and replace at the beginning of lines
b)\r is for find and replace at the end of lines
c) how to duplicate each single line following this example:

line1
line2
should become:
line1
line1
line2
line2

Thanks for your help

blubbernaut 06-15-2009 04:33 AM

In Word, (and I assume Textwrangler) ^p (sometimes /p) just means "paragraph break". So you can use that at the start or the end of a line.
Example:
Search for line1^p replace with line1^pline1^p
Search for line2^p replace with line2^pline2^p

paldo 06-15-2009 05:26 AM

Sorry, I was wrong
 
Quote:

Originally Posted by paldo (Post 538156)
You are right, textwrangler is a powerful tool, much better then textedit.
I went through the tutorial but I did not found the following:
a)find and replace at the beginning of lines
b)\r is for find and replace at the end of lines
c) how to duplicate each single line following this example:

line1
line2
should become:
line1
line1
line2
line2

Thanks for your help

Sorry, I was wrong. \r is for find and replace at the beginning of lines. So which is for at the end of the phrase of a line?

paldo 06-15-2009 05:43 AM

OK, I got it.

beginning of line: find:\r, replace:\r text
end of line: find:\r, replace:text \r
There is still my question about how to duplicate. Consider that I have 110 lines:
line1
line2
...
should become:
line1
line1
line2
line2
...
...

hayne 06-15-2009 09:48 AM

If you want every line in the file to be duplicated, you could do that using the 'paste' command (in a Terminal window).
For example, if the file is named "foo.txt":
Code:

% cat foo.txt
abc
def
ghi

% paste -d "\n" foo.txt foo.txt > bar.txt

% cat bar.txt
abc
abc
def
def
ghi
ghi

It would also be possible to do that in TextWrangler using a regular expression replacement.

cwtnospam 06-15-2009 09:54 AM

You could use this Applescript with TextWrangler:
Code:

tell application "TextWrangler"
        activate
        set x to text of window 1
        set mytext to ""
        set y to every paragraph of x
        repeat with eachline in y
                set z to eachline as text
                set mytext to mytext & z & return & z & return
        end repeat
        set text of window 1 to mytext
end tell


paldo 06-15-2009 11:39 AM

Perfect, your applescript works perfectly. Thank you very much.

DaleMox 06-15-2009 02:53 PM

Quote:

Originally Posted by DaleMox (Post 538054)
Also in vi (but also any text editor with regular expression search and replace functionality)
you can use:
^(.*)$
replaced with:
\1\r\1
but some editors prefer:
$1\r$1

in vi you can use:
%s/^\(.*\)$/\1\r\1/g

(you must input backslash '\' before parenthesis for vi to understand you want to group)

Dale

Nobody liked my regular expression solution :(

cwtnospam 06-15-2009 02:56 PM

It was good, but regexp are intimidating, especially if you aren't used to coding at all. That's why I went with Applescript. It's the least intimidating option I know.

anika123 06-15-2009 04:43 PM

apple script rules, I would also say regexp rules but I have no idea what it is exactly.

paldo 06-15-2009 05:52 PM

Quote:

Originally Posted by cwtnospam (Post 538178)
You could use this Applescript with TextWrangler:
Code:

tell application "TextWrangler"
        activate
        set x to text of window 1
        set mytext to ""
        set y to every paragraph of x
        repeat with eachline in y
                set z to eachline as text
                set mytext to mytext & z & return & z & return
        end repeat
        set text of window 1 to mytext
end tell


Dear cwtnospam,
may I ask you some more help? Can you write another applescript?
I have 220 lines with 110 titles. Here the example:
title1
title1
title2
title2
....
This should become:
<location>title1</location>
<title>title1</title>
<location>title2</location>
<title>title2</title>
...

I think we have two way to solve it:
a)write an applescript
b)use the find/replace function (but every second line must be replaced)

Can you help me please?

DaleMox 06-15-2009 06:10 PM

%s/^\(.*\)\n\1$/<location>\1<\/location>\r<title>\1<\/title>/g

cwtnospam 06-15-2009 07:20 PM

Scary!

Code:

tell application "TextWrangler"
        activate
        set x to text of window 1
        set mytext to ""
        set y to every paragraph of x
        repeat with eachline in y
                set z to eachline as text
                set mytext to mytext & "<location>" & z & "</location>" & return & "<title>" & z & "</title>" & return
        end repeat
        set text of window 1 to mytext
end tell


paldo 06-17-2009 09:48 AM

Thank you very much
 
Great job, thank you. I wish I can write applescripts.

cwtnospam 06-17-2009 10:35 AM

It's easy! Just do one step at a time until you're done. Here it is with comments:
Code:

tell application "TextWrangler"  -- Tells Applescript that you want to instruct TextWrangler what to do
        activate  -- Tells TextWrangler to be the active application, as if you'd clicked on its window.
        set x to text of window 1  -- Sets a variable "x" to be all of the text in the active window
        set mytext to "" -- Initializes mytext to be empty
        set y to every paragraph of x -- Sets variable "y" to be a list of the paragraphs in "x" (paragraphs are separated by returns)
        repeat with eachline in y  -- Sets up a loop where "eachline" takes on each successive value in "y"
                set mytext to mytext & "<location>" & eachline & "</location>" & return & "<title>" & eachline & "</title>" & return
                -- Adds to "mytext" the location and title tags around "eachline" and separates them with carriage return characters
        end repeat -- End of loop is reached when "eachline" has been every item in the list "y"
        set text of window 1 to mytext -- Tells TextWrangler to replace the text in its current window with the text in "mytext"
end tell -- Done with TextWrangler

It's slightly different code because I saw that I was being redundant. See if you can spot where!


All times are GMT -5. The time now is 11:07 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.