Find the answer to your Linux question:
Results 1 to 5 of 5
Hi. I've got a file I'm trying to edit. The format is: Code: ... SOME UNIQUE EXPRESSION ANOTHER EXPRESSION ... I'm trying to change "ANOTHER EXPRESSION" to be that of ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    5

    Question edit a file after a newline

    Hi.
    I've got a file I'm trying to edit. The format is:
    Code:
    ...
    SOME UNIQUE EXPRESSION
    ANOTHER EXPRESSION
    ...
    I'm trying to change "ANOTHER EXPRESSION" to be that of the filename minus the suffix

    Example:
    Code:
    bash-prompt$ ls
    foo.suffix
    
    bash-prompt$ ./some_magic_script foo.suffix > foo.suffix.changed
    bash-prompt$ cat foo.suffix.changed
    ...
    SOME UNIQUE EXPRESSION
    foo
    ...
    I know that this can be done in perl, sed, awk but am not sure how: I don't know how to use any of them (yet. I'm working on it).

    I've sort of fiddled with sed so far, but haven't been able to get the N command to work so that it doesn't chop off the newline characters.

    Thanks in advance.

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    this assumes you have suffix of .xxx
    Code:
    awk '{      
          gsub(/\....$/,"",FILENAME)
          gsub("ANOTHER",FILENAME)
    }1 ' file.txt

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Posts
    5
    Quote Originally Posted by ghostdog74 View Post
    this assumes you have suffix of .xxx
    Code:
    awk '{      
          gsub(/\....$/,"",FILENAME)
          gsub("ANOTHER",FILENAME)
    }1 ' file.txt
    Thanks ghostdog. I was able to modify your script to fit my needs:
    Code:
    bash-prompt$ ls
    file.suffix
    
    bash-prompt$ awk '{
    sub(/\*$/,"",file)
    sub("0.1","file")
    }1 ' file.suffix
    0.1 was the string that I need to replace in the experimental file. Is there some way to not have this as a magic number? I don't know exactly what its going to be for some other files. I think I may not have been clear in the OP (my bad):
    Code:
    ...
    SOME_UNIQUE_STRING
    SOME_UNKNOWN_STRING
    ...
    I've actually got close to 100 files that I need to edit in the same way, and in each of them the string that I need to replace is different. It will always follow the unique string in all the files however, so I was hoping to locate the unique string and the modify the line immediatly afterwards.
    I tried using some globbing characters to overwrite the line, but was either getting some missing operand failures of a replacement of more than just the desired text.

  4. #4
    Just Joined!
    Join Date
    Jan 2008
    Posts
    5

    SOLVED for future reference

    I decided to use perl to do this. In case anyone else runs into a similar problem, here's how I solved it:
    Code:
    #!/usr/bin/perl
    $file=$ARGV[0];
    $name="$file\n";
    $name =~ s/\.[^.]*$//;
    open(FILE, $file) || die("Could not open file!");
    while ($line = <FILE>) {
    	chomp($line);
    	if ($line =~ /MAGIC_STRING/){
    		print "$line\n";
    		$line = <FILE>;
    		chomp($line);
    		$line =~ s/.*/$name/;
    	}
    	print "$line\n";
    }
    close(FILE);
    It was a good opportunity to pick up some perl. Soon I'll be golfing the same

  5. #5
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    well, I just understood your requirement after running your perl script.
    Here's the Perl-less equivalent
    Code:
    awk '/MAGIC_STRING/{ 
        print;
        getline;
        sub(/\....$/,"",FILENAME)
        print FILENAME;
        next}1 ' file

Posting Permissions

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