Need help with bash and/or perl commands on OS X Terminal

I”m having issues using the bash command ‘sed’ and with Perl commands. I need a solution in bash or Perl to do the following:

1. replace existing carriage returns in a file with a pipe symbol (just one not a pipe symbol for each space)
2. replace an equals sign with a colon (without OS X terminal crashing)
3. add a carriage return to any specific word
Please help ASAP. Thanks.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

can you provide sample text?

1) perl -pi.bak -e 's/\n$/\|//' filename
2) perl -pi.bak -e 's/\=/\:/g' filename
3) perl -pi.bak -e 's/word/word\n/g' filename

sed can be used in the same way.
sed can have all patterns sequentially
sed -i.bak -e 's/\n$/\|//' -e  's/\=/\:/g'  -e  's/word/word\n/g' filename

test first the -i.bak means that if there are matches, a backup will be created of filename with the backup name of filename.bak
Perl can do all patterns sequentially as well and there's no need to escape the = and : characters.

perl -pi.bak -e 's/\n$/|/; s/=/:/g; s/word/word\n/g' filename

Open in new window

Tech_20Author Commented:
Thanks. However, when I enter the following code below...

sed -i.bak -e 's/\n$/\|//‘ filename.txt
perl -pi.bak -e 's/\n$/\|//‘ filename.txt

...all I get is the following line with no change to the file...

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

You have 2 problems.
1) You have an extra / forward slash at the end of the regex
2) The second quote is a "smart quote" character (which is usually a sign that you used a word processor instead of a text editor).

Also, you don't need to escape that | pipe character.

Here's the corrected command
perl -pi.bak -e 's/\n$/|/' filename.txt

Open in new window

Tech_20Author Commented:
Thanks @FishMonger. That helped. Finally I try place a carriage return right after some text in a certain location. I type in...

perl -pi.bak -we 's/word||||/word||||\n/' filename

and for some odd reason after trying it 3 times, it adds the replacement text to the very beginning where there was no such text. Any suggestions?
| is a logical or in your case you need to pre-append \ before each | to make its matching literal.
You have too many pipe characters or some of them need to be escaped.

The pipe character has special meaning when used in the left side of the regex (i.e., it's the alternation character) and needs to be escaped BUT when used on the right side of the regex, it does not have that special meaning so it doesn't need to be escaped on that side.

What does the line look like and how should it look after the substitution?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tech_20Author Commented:
Thanks. That worked!
Tech_20Author Commented:
Thanks to @arnold who helped provide the direction to go in regarding code choice (sed & Perl) and also @FishMonger who helped me connect the dots and finish the Perl code!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.