Berkeley CSUA MOTD:Entry 26866
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/04/04 [General] UID:1000 Activity:popular
4/4     

2002/12/19 [Computer/SW/Languages/Perl, Computer/SW/Unix] UID:26866 Activity:nil
12/19   Newbie question - I'm not a CS or engineering or even a tech major so
        please don't jump all over this.  What I'd like to do is replace all
        occurrences of a word in an original file with a word in an infile,
                                                   ^^^^^^^^^^^^^^^^^^^^^^^???
        and output a new file.  I want to iterate this process for all the
        words in the infile list (the word to be replaced in the original file
        remains the same).  Greatly appreciate any suggestion on which
        utility or script would be most suitable for this kind of job.
        \- emacs, sed, perl can all do this. i dont understand precisely
           what you are trying to do, however. --psb
           \_ ED is the standard text editor.
           \_Thanks for your guys help so far.  What geordan summarized below
                is what I'm trying to do
                \_ I'm not on crack!  Yay!  ...Ohhhh -geordan
        \_ So the original file is basically a template which contains
           one or more words that needs to be replaced, and the infile
           contains a list of words that will be substituted into the
           template file?  It's late and it's hard for me to word that.
           Anyway the output is a bunch of files, one for each word in
           the infile?  I'd use perl. -geordan, who should just go to sleep
           \_ in bash:
                for i in `cat infile`;
                do
                        cat ORIGINALFILE | \
                        perl -p -i -e 's/YOURWORD/$i/g' > \
                        ORIGINALFILE.$i;
                done
              [ reformatted - formatd ]
        \_ i thought grep was the standard quickest solution for this sort of
           thing but okay the above looks fine. or you can grep using the same
           regexp 's/YOURWORD/$i/g' ?  i think
        \_ Uh you sure?  I don't think this is doing quite what the OP was
           asking but I admit I'm not sure what the OP really wanted either.
           \_ I think it's along the lines of what he wants but I'm not
              sure if the $i in the perl regexp will be properly escaped.
              \_ Replace the single quotes with double quotes and it will
                 be interpolated.  Also the -i flag doesn't really make
                 sense as you're not really modifying the file in-place.
                 -geordan
2025/04/04 [General] UID:1000 Activity:popular
4/4     

You may also be interested in these entries...
2012/8/30-11/7 [Computer/SW/Apps, Computer/SW/Unix] UID:54470 Activity:nil
8/30    Is wall just dead? The wallall command dies for me, muttering
        something about /var/wall/ttys not existing.
        \_ its seen a great drop in usage, though it seems mostly functional.
            -ERic
        \_ Couldn't open wall log!: Bad file descriptor
           Could not open wall subscription directory /var/wall/ttys: No such file or directory
	...
2011/11/20-2012/2/6 [Computer/Companies/Apple, Computer/SW/Unix] UID:54237 Activity:nil
11/20   Are there tools that can justify a chunk of plain ASCII text by
        replacing words with words of similar meaning and inserting/removing
        commas into the text?  I received a 40-line plain text mail where
        all the lines are justified on left and right.  Every word and comma
        is followed by only one space, and every period is followed by two
        spaces.  The guy is my kid's karate instructor which I don't think is
	...
2011/10/26-12/6 [Computer/SW/Unix] UID:54202 Activity:nil
10/24  What's an easy way to see if say column 3 of a file matches a list of
       expressions in a file? Basically I want to combine "grep -f <file>"
       to store the patterns and awk's $3 ~ /(AAA|BBB|CCC)/ ... I realize
       I can do this with "egrep -f " and use regexp instead of strings, but
       was wondering if there was some magic way to do this.
       \_ UNIX has no magic. Make a shell script to produce the ask or egrep
	...
2011/3/12-4/20 [Consumer/CellPhone, Computer/HW/Laptop] UID:54057 Activity:nil
3/12    I am curious what others think of tablets like iPad. They don't seem
        useful to me, but I use my computer for more than web browsing,
        Facebook, and Twitter. Why would I buy one instead of a laptop?
        They seem like a disabled laptop to me, but at a higher price.
        \_ You are most likely a coder.  iPad is not for coders.  They are
           what you get your non-technical friends.  Or musicians.  Look at
	...
2011/2/6-19 [Computer/Networking] UID:54028 Activity:nil
2/5     hmm.
$netstat -at | grep LISTEN
tcp        0      0 *:43300                 *:*                     LISTEN
        \_ this is an sshd
tcp        0      0 *:49416                 *:*                     LISTEN
tcp        0      0 *:36201                 *:*                     LISTEN
	...