我有一个文本文件,其结构如下:
....
"/home/letizia/Documents/SpanishSegmentation/Recordings/segmented/mfc/F001.0.rec"
COGE
LAS
HOJAS
Y
LAS
QUEMAS
TODAS
EN
EL
FUEGO
"/home/letizia/Documents/SpanishSegmentation/Recordings/segmented/mfc/F002.0.rec"
LA
LIGA
DE
PAZ
SE
REUNIO314201
PARA
TRATAR
EL
TEMA
....我想选择"F0001.0“和"F0002.0”。
我正在使用:
ID="F"
if [[ "$LINE" == *Recordings* ]]
then
SEGMENT=`echo $LINE | grep -o $ID.* | cut -d'.' -f1-2`
fi但它不起作用。错误在哪里?
非常提前感谢您。
发布于 2013-02-18 20:37:19
您需要一个while循环:
while IFS= read -r line; do
id="F"
if [[ "$line" =~ /Recordings/ ]]; then
segment=$(echo $line | grep -o "$id.*" | cut -d '.' -f1-2)
echo "$segment"
fi
done < file.txt结果:
F001.0
F002.0但是,更好的方法是使用sed
sed -n '/Recordings/s#.*/\(F[^\.]*\.[^\.]*\).*#\1#p' file.txt发布于 2013-02-18 19:48:11
尝试使用sed:
sed -n 's@^".*/Recordings/.*/\(.*\)"$@\1@p' file.txt快速演练:
-n:不会打印任何内容,除非有特别要求(根据最终p).s@:的要求,在that.^".*/Recordings/.*/\(.*\)"$:之后的部分匹配以双引号开头和结尾、包含//的行,并在最后一个slash.\1:之前将匹配的字符串替换为最后一个部分(在parentheses).中捕获)之前,请将匹配的字符串替换为下一个@之前的部分
https://stackoverflow.com/questions/14935165
复制相似问题