another unix script
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: another unix script

  1. #1
    Senior Member deftones12's Avatar
    Join Date
    Jan 2003
    Location
    cali forn i a
    Posts
    333

    Unhappy another unix script

    alright i feel crappy askin for more help, but ive read all the man pages i can take, and im still left with some difficulties. I know i should either use sed or awk or maybe even vi, but can anyone help me out some of those to scane a file (.profile) for a line and then rewrite that whole line? an example would be to add a new printer or something, in the .profile would be a line say like "printerdest=" and after that line could be xxxxxx or whatever, i want vi awk or sed to scan the file for "printerdest=" and replace it with what im specifying in the script. is there a specific command in awk or sed that im not seeing? help or guidance would be much appreciated. thanks.

  2. #2
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    Perl and regular expressions.
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

  3. #3
    Senior Member
    Join Date
    Nov 2002
    Posts
    186
    Sed will work just fine. Say I have a file called users.txt. It looks like this:
    Al
    Ben
    Jon

    Now say Jon's name is spelled wrong. To change it to John, we would do:
    sed s/Jon/John/ users.txt >users.temp
    mv users.temp users.txt

    The sed command uses a regular expression substitution (s/). It looks for Jon and replaces it with John in the file called users.txt Since sed is a stream editor it sends the output to stdout, or the terminal, so use > to redirect to a temp file, then rename the temp file to the original.

    Apply this to your .profile and it should work fine.
    \"When you say best friends, it means friends forever\" Brand New
    \"Best friends means I pulled the trigger
    Best friends means you get what you deserve\" Taking Back Sunday
    Visit alastairgrant.ca

  4. #4
    Senior Member deftones12's Avatar
    Join Date
    Jan 2003
    Location
    cali forn i a
    Posts
    333
    ok, see that seems fine but sometimes the original strings are different, what if it was something like "printerdest=xxxxxxx" and sometimes "printerdest=printer1", is there a way it will just scan for "printerdest=" and it will rewrite the whole line, regardless if there is a printer there or not?

  5. #5
    PHP/PostgreSQL guy
    Join Date
    Dec 2001
    Posts
    1,164
    I haven't done a good simple Q&D in perl lately, so here you go...

    Code:
    #!/usr/bin/perl -w
    
    $print = $ARGV[0] ? $ARGV[0] : "new_printer";
    
    $file = "text.txt";
    open FOO, "<$file" or die "Can't open $file: $!\n";
    
    $new_file = "test2.txt"; 
    open BAR, ">$new_file" or die "Can't open $new_file: $!\n";
    
    while (<FOO>) {
      $string = $_;
    
      if (/^printerdest=/i) {
        $new_line = "printerdest=$printer\n";
      } else {
        $new_line = $string;
      }
    
      print BAR "$new_line";
    }
    The above is a perl script that will rewrite the given file test.txt into test2.txt with the new printer destination. Take the below example:

    If test.txt has this:
    This is a test file that has
    some information relating
    to a profile and some
    printerdest=blahblahblah
    user information for you
    to peruse...
    Then if you ran './foo 12345', and did a 'cat test2.txt', you would see the following:

    This is a test file that has
    some information relating
    to a profile and some
    printerdest=12345
    user information for you
    to peruse...
    Otherwise, if you ran './foo', you would get the following:

    This is a test file that has
    some information relating
    to a profile and some
    printerdest=new_printer
    user information for you
    to peruse...
    Hope this helps!
    We the willing, led by the unknowing, have been doing the impossible for the ungrateful. We have done so much with so little for so long that we are now qualified to do just about anything with almost nothing.

  6. #6
    Banned
    Join Date
    Jun 2003
    Posts
    1,302
    I am along with Juridan an Vorlin on this one, perl is the way to go.

    I am not sure that I get your question right, so I don't know if Vorlin's script will work. What I think your are asking for is just a script that will scan a file and check for printer* and if it scans and gets that it will rewrite that to another file, with just that info.

    Vorlins script does just about that just a little more also.

    hmm...

    Let me know if Vorlins is what you are looking for, if it isn't I will write something up for you real quick.

    Though if you don't like using perl you could always write up a bash with some grep in it.

  7. #7
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,534
    like so.. (did something quite similar a while back)

    Code:
    #!/bin/bash
    
    #
    #       (c) 2004 the_JinX ( Anne Jan Brouwer )
    #                       GNU GPL
    #
    
    if [[ -z $1 ]]||[[ -z $2 ]]||[[ $1 == "--help" ]]; then
        echo
        echo "    Parameter replacer v0.1"
        echo "Usage:    $0 keyword value < input"
        echo "Optional: $0 keyword value < input > output"
        echo
        exit
    fi
    
    while read line; do
        if [[ `expr match "$line" ".*$1=*."` -gt 0 ]]; then
            echo ${line%%=*}=$2
        else
            echo $line
        fi
    done
    as you can see the actual code is only about 7 lines..

    To match vorlins example..

    ./foo printerdest 12345 < test.txt > test2.txt

    I use this script on /etc files sometimes (placed this script in the file /usr/local/bin/chparams on my main box)
    ASCII stupid question, get a stupid ANSI.
    When in Russia, pet a PETSCII.

    Get your ass over to SLAYRadio the best station for C64 Remixes !

  8. #8
    PHP/PostgreSQL guy
    Join Date
    Dec 2001
    Posts
    1,164
    My script will only change one line, and that's any line beginning with 'printerdest=', otherwise it simply writes out line for line into another file.

    To make the prior script a lot more beefier, one could do this:

    Code:
    #!/usr/bin/perl -w
    
    # Set parameters and test stuff...
    
    $printer = $ARGV[0] ? $ARGV[0] : "new_printer";
    $curr_file = $ARGV[1] ? $ARGV[1] : NULL;
    $new_file = $ARGV[2] ? $ARGV[2] : NULL;
    
    # Set counters and other integer variables
    
    $count = @ARGV;
    $i = 0;
    $found = 0;
    
    if ($count < 3) {
    	print "Usage: $0 <printer> <file> <new_file>\n";
    	exit 1;
    } elsif (! -e $curr_file || (-e $curr_file && ! -r $curr_file)) {
    	print "Error: file '$curr_file' doesn't exist or can't be read, aborting.\n";
    	exit 1;
    }
    
    # Got this far so now open files and such...
    
    open FOO, "<$curr_file";
    open BAR, ">$new_file";
    
    while (<FOO>) {
    	$i++;
    	chomp($string = $_);
    
    	if (/^printerdest=/i) {
    		$found = $i;
    		$old_printer = $string;
    		$old_printer =~ s/printerdest=//i;
    		$new_line = "printerdest=$printer";
    	} else {
    		$new_line = $string;
    	}
    	
    	print BAR "$new_line\n";
    }
    
    if ($found > 0) {
    	print "Current file: $curr_file\n";
    	print "New file: $new_file\n";
    	print "Old printer: $old_printer\n";
    	print "New printer: $printer\n";
    	print "Found on line: $found\n";
    } else {
    	print "No printer destination line found in '$curr_file'.\n";
    	print "Unlinking new file '$new_file'.\n";
    	unlink($new_file);
    }
    Now, when you do it, say with './foo blarghorama test.txt test2.txt', and your test.txt has the following:
    This is a test of some stuff in a file to check to
    see if it has a printerdest= in it somewhere and
    I hope it does because then this works and some
    printerdest=foobar
    test test test
    Then the output would be as such:
    Current file: test.txt
    New file: test2.txt
    Old printer: foobar
    New printer: blarghorama
    Found on line: 4
    If file 'test.txt' didn't have a printerdest= line, it would say this:
    No printer destination line found in 'test.txt'.
    Unlinking new file 'test2.txt'.
    Sure, we could check for null printerdest fields, and do some other stuff like formatted output, but who's counting at this point, hehe...
    We the willing, led by the unknowing, have been doing the impossible for the ungrateful. We have done so much with so little for so long that we are now qualified to do just about anything with almost nothing.

  9. #9
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,534
    OK Vorlin..
    You win the pissing contest, your script is bigger then mine

    I think you are compensating :P

    Thx for the previous version idea Vorlin..
    I've updated to version 0.2
    version two now adds the old keyword and parameter(s) with a # in front of it, to comment it out


    Code:
    #!/bin/bash
    
    #
    #       (c) 2004 the_JinX ( Anne Jan Brouwer )
    #                       GNU GPL
    #
    
    if [[ -z $1 ]]||[[ -z $2 ]]||[[ $1 == "--help" ]]; then
        echo
        echo "    Parameter replacer v0.2"
        echo "Usage:    $0 keyword value < input"
        echo "Optional: $0 keyword value < input > output"
        echo
        exit
    fi
    
    while read line; do
        if [[ `expr match "$line" ".*$1=*."` -gt 0 ]]; then
            echo ${line%%=*}=$2
            echo "#"$line
        else
            echo $line
        fi
    done
    ASCII stupid question, get a stupid ANSI.
    When in Russia, pet a PETSCII.

    Get your ass over to SLAYRadio the best station for C64 Remixes !

  10. #10
    PHP/PostgreSQL guy
    Join Date
    Dec 2001
    Posts
    1,164
    ROFL, nah...yours is much more concise as far as brevity is concerned. I generally like to comment on sections of code, do a lot of error checking and print out a lot of stuff for the end-user's benefit (having done customized output for departments and the like). It also makes it easier for me to debug later.

    That's the beauty of programming...as Larry Wall said about perl oh-so-long ago, "There's more than one way to do it" and that definitely applies here.
    We the willing, led by the unknowing, have been doing the impossible for the ungrateful. We have done so much with so little for so long that we are now qualified to do just about anything with almost nothing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •