Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
I need to add apostrophes to a bunch of filenames. I think that maybe Sed is the best way to do this but it's years since I used it and I forgot almost everything.

So these file names vary in length and other things, though they are separated by newlines in a text file such as:

my cat is calico
my cat is fat and happy
there are 3 cats in my house
their names are - Pantalaimon Serenity

What are the symbology that I need to capture the lowercase, uppercase, numbers, and other symbology?

I know it's generally something such as this:

sed -i 's/my cat is calico/"my cat is calico"/g' filename.txt

but obviously I need to generalize it to capture all filenames in the file
 
1. get rid of the white space at the end of the Serenity line.
2. Wrap quots around the text of each line.
3. Direct the altered content to a new file.

Code:
sed -e 's/[[:space:]]*$//' -e 's/^.*$/\"&\"/' < filename.txt > filename2.txt
 
thanks! Could you elaborate on the -e option flag? I can't find any information about it. Actually, if it isn't too much trouble, help me to understand each character? :p The *$ does that represent the entire filename? and what about ^.$ and &\ ?

I'm trying to find a good online resource to remind me of all this stuff but I guess I need to buy a book...

Does ^ represent each line?
[doublepost=1539991215][/doublepost]I was able to make this part work -

Code:
 sed 's/^.*$/\"&\"/'  filename.txt

But the output went to the terminal screen instead of overwriting the original file...
[doublepost=1539991341][/doublepost]For any future readers of this thread - I found this to help https://www.gnu.org/software/sed/manual/sed.html
 
Last edited:
The -e means the following is one of many sed commands to execute.

The stuff in the first half of each command is a regular expression.

The asterisk indicates to match zero or more of the preceding character.

The [[:space:]] refers to white space. If I recall right, that includes the space character of course, but also includes things like tabs and line feeds.

The $ sign indicates the end of the line being tested. So in the first command, I’m looking at replacing one or more spaces that occur at the end of the line. Your sample test has a line like that.

The period means to match any character, so combined with he asterisk, we are matching zero or more of all characters.

The caret ^ means to test from the beginning of the line. So in the second command, I’m capturing all text from the beginning (^) of the line to the end ($). The and (&) symbol in the second part of that statement is using the captured test from the first part of that command. (I had to look that up) I’m then wrapping it with quote marks.

The less than sign and greater than signs are for input and output. So I’m outputting to a new file. Do NOT output to the same name as the input file.

Open terminal and type ‘man sed’ For more info.

I believe the awk command can do all of the above against a single input file. I haven’t played with that though.
 
Okay, I'm experiencing trouble using Sed yet. I have another list that I need to arrange differently so I can see it more clearly. Basically there's a dictionary I'm trying to use which has duplicate keys.

The list is very long and wraps around to the next line. So I'm trying to used sed to separate it by new line so I can find the duplicate more easily.

Just as an example I have:

"test": ["everything"], "Next": ["anything2"], "last1": ["4all"],

and I want:

"test": ["everything"],
"Next": ["anything2"],
"last1": ["4all"],

so the command I'm trying to use is:

sed 's/\[.*\]\: \[.*\]\,/\[.*\]\: \[.*\],\n/'

or maybe? 's/.*\: .*,/.*\: .*,\n/'
 
Last edited:
Think it through. What is the minimum for the delimiter between the items of separation. If any of your quoted strings can have commas, then the delimiter is more than just the comma. Focus on the delimiter.
 
Last edited:
After much MUCH searching and a great deal of frustration I have found the solution here: https://superuser.com/questions/307165/newlines-in-sed-on-mac-os-x

and also here: https://cafenate.wordpress.com/2010/12/05/newlines-in-sed-on-mac/#comment-200

For the sake of helping others I decided to post my solution here.

this was the command I used:

Code:
   sed -e 's/, /\'$'\n/g' < duplicates.txt > outfile.txt
[doublepost=1546562898][/doublepost]I have read several accounts that Sed has become outdated severely. Is there a more recent up to date unix tool with similar capabilities?
 
I should note that my solution was for a dictionary with keys with single values, not arrays of values. Incidentally, where could I write to the Apple MacOS developers to ask them to update the Sed tool to the latest version which is apparently from 2009?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.