Link to home
Start Free TrialLog in
Avatar of Tech_20
Tech_20

asked on

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.
SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of Tech_20
Tech_20

ASKER

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...

>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tech_20

ASKER

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tech_20

ASKER

Thanks. That worked!
Avatar of Tech_20

ASKER

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!