使用awk匹配以/ root /开头并在字符前结束的PATH

时间:2017-06-06 09:53:54

标签: awk

我有一个命令可以产生一个带有路径的随机输出 我需要使用awk提取此路径。 路径始终以相同的模式开始(假设它始终以/ root开头),但其余的可以更改

例如:

error reason  05/30/2017 12:18:37 [74575:45687]: "/root/this/is/my/path/" random text random text random text random text random text random    text 

提取/ root / this /是/ my / path /:我尝试用以下方式使用grep + awk:

command.sh | grep error  |awk '/root/{mypath=$6}'. 

但我发现我的路径并不总是位于同一个地方。更重要的是,我不想提取双引号。

i need to have /root/this/is/my/path/ not "/root/this/is/my/path/"

任何人都可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

这也应该有效:

private void listBoxProducts_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        ProductList_Variables selected = ProductList_Variables)listBoxProducts.SelectedItem;

        textBoxProduct.Text = selected.Product;
        comboBoxCategory.SelectedItem = selected.Category;
        comboBoxMarket.SelectedItem = selected.Market;
        comboBoxContainer.SelectedItem = selected.Container;
        textBoxPrice1.Text = selected.Price.ToString();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
class ProductList_Variables
{
    public int Id { get; set; }
    public string Product { get; set; }
    public string Category { get; set; }
    public string Size { get; set; }
    public string Market { get; set; }
    public string ProductName { get { return Product + " - " + Category + " - Size: " + Size +", Market: "+ Market; } }
    public string Flavour { get; set; }
    public decimal Price { get; set; }
    public string Container { get; set; }
    public int IdContainer { get; set; }


}
void Fillcombo()// is filling the combobox
{
    try
    {
        using (var db = new GelatoProjectDBEntities())
        {
            var products = (from x in db.ProductsLists
                            select new ProductList_Variables { Id = x.Id, Product = x.Product, Category = x.Category, Size = x.Size, Market = x.Market, Container=x.Container, Price=x.Price, IdContainer=x.IdContainer }
                            ).OrderBy(c => c.Product).ToArray();
            listBoxProducts.Items.AddRange(products);
            listBoxProducts.DisplayMember = "ProductName";
            listBoxProducts.ValueMember = "Id";
            var goods = (from x in db.Goods
                         select new ProductList_Variables { Id = x.Id, Product=x.item, Container = x.item}
                         ).OrderBy(c => c.Product).ToArray();

           comboBoxContainer.Items.AddRange(goods);
           comboBoxContainer.DisplayMember = "Product";
           comboBoxContainer.ValueMember = "Id";
       }
   }
   catch (Exception ex)
   {
       MessageBox.Show(ex.Message);
   }
    }

输出:

command.sh | awk '/^error/{ for ( i = 1; i <= NF; i++) if ( $i ~ /^"/){ gsub(/"/, "", $i); print $i} }' -

答案 1 :(得分:0)

awk 解决方案:

command.sh | awk '/^error/{ match($0,/\/root[^"]+/); print substr($0,RSTART,RLENGTH) }'

输出:

/root/this/is/my/path/

答案 2 :(得分:0)

您可以采用的一种简单方法:

command.sh | awk -F\" '/^error/{ print $2}'

输出:

/root/this/is/my/path/