sed replace filename以quote开头

时间:2015-07-08 00:22:06

标签: regex linux string replace sed

我有img src="../image/popup.jpg"bg: 'images/xxx/popup.png'等文件字符串。我想用sed替换另一个字符串/名称,我试过

sed 's/^"[^\s]*/somethingelse/g' 

3 个答案:

答案 0 :(得分:0)

试试这个,

 sed 's|/some/UNIX/path|/a/new/path|g' files

答案 1 :(得分:0)

当使用sed替换路径名时,更改其默认的demiliter有助于,如天网所指出的,并且明智地使用单引号允许变量插值,如下所示:

//
//  ViewController.m

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textfield;
@property (strong, nonatomic) NSTimer * searchTimer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // declare this view controller as the delegate
    self.textfield.delegate = self;

    // handle the change event (UIControlEventEditingChanged) by calling (textFieldDidChange:)
    [self.textfield addTarget:self
                  action:@selector(textFieldDidChange:)
        forControlEvents:UIControlEventEditingChanged];

}

// reset the search timer whenever the text field changes
-(void)textFieldDidChange :(UITextField *)textField{

    // if a timer is already active, prevent it from firing
    if (self.searchTimer != nil) {
        [self.searchTimer invalidate];
        self.searchTimer = nil;
    }

    // reschedule the search: in 1.0 second, call the searchForKeyword: method on the new textfield content
    self.searchTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0
                                                        target: self
                                                      selector: @selector(searchForKeyword:)
                                                      userInfo: self.textfield.text
                                                       repeats: NO];

}


- (void) searchForKeyword:(NSTimer *)timer
{
    // retrieve the keyword from user info
    NSString *keyword = (NSString*)timer.userInfo;

    // perform your search (stubbed here using NSLog)
    NSLog(@"Searching for keyword %@", keyword);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

来自http://linux.die.net/man/1/sed sed不支持POSIX。 2个基本的正则表达式“完全是因为性能问题”,但它接近了。根据{{​​3}},POSIX正则表达式的一个规则是“使用反斜杠来转义从不是元字符的字符是一个错误。”由于双引号和单引号不是元字符,因此根据此不应将它们反映在POSIX正则表达式中。

答案 2 :(得分:0)

sed与BRE一起使用:

sed "s/\([\"']\)[^\"']*\([\"']\)/\1somethingelse\2/g" file

使用ERE:

sed -r "s/([\"'])[^\"']*([\"'])/\1somethingelse\2/g" file

假设文件名中没有转义引号,即"..."'...'内没有引用