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 ...
- 01-22-2008 #1Just Joined!
- Join Date
- Jan 2008
- Posts
- 5
edit a file after a newline
Hi.
I've got a file I'm trying to edit. The format is:
I'm trying to change "ANOTHER EXPRESSION" to be that of the filename minus the suffixCode:... SOME UNIQUE EXPRESSION ANOTHER EXPRESSION ...
Example:
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).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'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.
- 01-22-2008 #2Linux User
- Join Date
- Aug 2006
- Posts
- 458
this assumes you have suffix of .xxx
Code:awk '{ gsub(/\....$/,"",FILENAME) gsub("ANOTHER",FILENAME) }1 ' file.txt
- 01-22-2008 #3Just Joined!
- Join Date
- Jan 2008
- Posts
- 5
Thanks ghostdog. I was able to modify your script to fit my needs:
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:bash-prompt$ ls file.suffix bash-prompt$ awk '{ sub(/\*$/,"",file) sub("0.1","file") }1 ' file.suffix
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.Code:... SOME_UNIQUE_STRING SOME_UNKNOWN_STRING ...
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.
- 01-22-2008 #4Just 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:
It was a good opportunity to pick up some perl. Soon I'll be golfing the sameCode:#!/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);
- 01-22-2008 #5Linux 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


Reply With Quote
