Sponsored Content
Homework and Emergencies Homework & Coursework Questions Refactoring via SHELL utilities Post 302562401 by Corona688 on Thursday 6th of October 2011 07:03:49 PM
Old 10-06-2011
sed is always ugly. it's unavoidable. :/

completely explaining how sed works would be a career, not a post, but I'll explain in brief.

Code:
# Just prints one big long string, and pipes it into sed.
echo "slartibartfast q;  int aslartibartfast;  slartibartfast b, c;  int d;" |
        # sed 's/a/b/g' searches for all a, replaces with b.
        # a can be an entire expression, not just a literal string.
        # Just 's/slartibartfast/qwerty/g' would change our
        # 'aslartibartfast' to 'aqwerty', which is wrong, so we need to check
        # where it is too.
        #
        # [ ;] matches either a semicolon or a space, so
        # [ ;]slartibartfast[ ;] would catch it in the middle of a line, but
        # not the beginning or end.
        #
        # ^ means beginning, $ means end, | means 'or', () group things.  So
        " ([ ;]|^) would mean the beginning of the line or space or semicolon.
        # ([ ;]|^)slartibartfast([ ;]|$) should catch it anywhere in the line.
        #
        # Except if we search-replace that, we're stripping out lots of
        # whitespace and semicolons -- you need those.  You can put back
        # anything you find in () with \1, \2, ... for match 1, match 2 ...
        #
        # So:
        #
        # s/([ ;]|^)slartibartfast([ ;]|)/\1qwerty\2/g'
        # ...tells it to replace ;slartibartfast; with qwerty and put the ;'s back.
        #
        # Except, sed is horrible and won't understand ( | ) unless
        # you put \ in front of them, which makes otherwise-simple expressions
        # capable of blinding small dogs and the elderly.
        #
        #If you have sed -r, you can leave them as-is.
        sed 's/\([ ;]\|^\)slartibartfast\([ ;]\|$\)/\1qwerty\2/g'
# And this is what it prints.  See how it left aslartibartfast alone.
qwerty q;  int aslartibartfast;  qwerty b, c;  int d;
$

 

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Zmodem Utilities

Where can I find Zmodem utilities for Soloris 7 ie: rz sz thnx (1 Reply)
Discussion started by: SmartJuniorUnix
1 Replies

2. Shell Programming and Scripting

How to manage multiple versions of a set of shell and SQL script utilities

Hi all --- I have the need to manage multiple versions of a set of utility scripts -- both shell and SQL and other .dat files. I am wondering if anyone out there knows of a good way to "PATH" to SQL and text files in a way similar to how PATH facilitates finding executables in a pre-specified... (2 Replies)
Discussion started by: DennisB
2 Replies

3. UNIX for Dummies Questions & Answers

top ten utilities in shell scripting?

Let's get some feedback about the top ten ute's you guys use in writing your scripts - I mean yeah it depends on the job and what you're trying to accomplish, but there ARE those commands (sed, grep, awk, cut, etc.) that most will use time and again... ...so, what do you use? (3 Replies)
Discussion started by: diego
3 Replies

4. UNIX for Dummies Questions & Answers

bc and wc utilities???

Hi, Can anybody explain bc and wc system utilities in Unix? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

5. UNIX for Advanced & Expert Users

utilities

hi experts, i have a file sample: ======= 000123 5 7 0008 00345 5 9 0004 how can i get an output as 123 5 7 8 345 5 9 4 thanks in an advance subhendu (5 Replies)
Discussion started by: subhendu81
5 Replies

6. UNIX and Linux Applications

Utilities for CSV

I'd like to be able to do something like 'select column_a column_c column_b from file.csv'. Scripting it probably wouldn't be too extraordinarily difficult but I'd rather not reinvent the wheel if someone's built a better one already. (3 Replies)
Discussion started by: Corona688
3 Replies

7. UNIX for Dummies Questions & Answers

Custom Reporting Utilities for SHell (CRUSH)

If you are interested in a set of utilities designed for reporting solutions within the shell, check out CRUSH on Google code. crush-tools - Project Hosting on Google Code (0 Replies)
Discussion started by: watingo
0 Replies

8. Shell Programming and Scripting

Crontab utilities

Hi gurus/lead, I have one doubt in crontab. I have shell script source code in one server. where I don't have priviliges acess R/W the shell script in that server. how can I setup the crontab to pick the code from the other unix server ? whether is there any possibility. in my... (1 Reply)
Discussion started by: ramkumar15
1 Replies
CHROOT(8)						    BSD System Manager's Manual 						 CHROOT(8)

NAME
chroot -- change root directory SYNOPSIS
chroot [-u -user] [-g -group] [-G -group,group,...] newroot [command] DESCRIPTION
The chroot utility changes its current and root directories to the supplied directory newroot and then exec's command, if supplied, or an interactive copy of the user's login shell. If the -u, -g or -G options are given, the user, group and group list of the process are set to these values after the chroot has taken place. See setgid(2), setgroups(2), setuid(2), getgrnam(3) and getpwnam(3). Note, command or the shell are run as your real-user-id. ENVIRONMENT
The following environment variable is referenced by : SHELL If set, the string specified by SHELL is interpreted as the name of the shell to exec. If the variable SHELL is not set, /bin/sh is used. SEE ALSO
chdir(2), chroot(2), environ(7), jail(8) HISTORY
The chroot utility first appeared in 4.4BSD. SECURITY CONSIDERATIONS
chroot should never be installed setuid root, as it would then be possible to exploit the program to gain root privileges. BSD
January 24, 2002 BSD
All times are GMT -4. The time now is 01:19 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy