如何使用bash在嵌套括号之间grep字符串?是否可以不使用循环?例如,如果我有一个像:
这样的字符串[[TargetString1:SomethingIDontWantAfterColon [[TargetString2]]]]
我希望只对[[]]
中的两个目标字符串进行grep:
TargetString1
TargetString2
我尝试了以下无法获取TargetString2
的命令grep -o -P '(?<=\[\[).*(?=\]\])'|cut -d ':' -f1
答案 0 :(得分:2)
使用GNU
&lt; P
选项:
grep -oP "(?<=\[\[)[\w\s]+"
当后跟两个括号(\w+
)时,正则表达式将匹配一系列单词字符([[
)。这适用于您的示例字符串,但不适用于更复杂的结构,如:
[[[[TargetString1]]TargetString2:SomethingIDontWantAfterColon[[TargetString3]]]]
仅匹配TargetString1
和TargetString3
。
答案 1 :(得分:1)
要从嵌套的public class RectangleTest {
public static void main(String[] args) {
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(5, 5);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(25, 5);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(-5, 5);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(0, 5);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(5, 25);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(5, 0);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(5, -5);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("Enter width and length: ");
Rectangle rectangle = new Rectangle(20,0);
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
括号中提取,您可以使用[[]]
sed
输出:
#!/bin/bash
str="[[TargetString1:SomethingIDontWantAfterColon[[TargetString2]]]]"
echo $str | grep -o -P '(?<=\[\[).*(?=\]\])'|cut -d ':' -f1
echo $str | sed 's/.*\[\([^]]*\)\].*/\1/g' #which works only if string exsit between []
答案 2 :(得分:1)
您可以使用grep regex e c n s
1 1 5 a 1
2 1 5 b 2
3 1 5 d 7
来执行此操作
grep -Eo '\[\[\w+' | sed 's/\[\[//g'