2013-10-05 31 views
0

我需要更改一些行我使用此while循环另一种格式。sed虽然循环文本格式

while IFS= read -r line; 
do 
var=$(echo "$line" | grep -oE "http://img[0-9].domain.xy/t/[0-9][0-9][0-9]/[0-9][0-9][0-9]/" | uniq); 
echo "$line" | sed -e 's|http://img[0-9].domain.xy/t/[0-9][0-9][0-9]/[0-9][0-9][0-9]/||g' -e "s|.*|&"${var}"|g" >> newFile; 
done < file; 

改变这种格式

<iframe src="http://domain.xy/load.php?file=2259929" frameborder="0" scrolling="no"></iframe>|http://img9.domain.xy/t/929/320/1_2259929.jpg;http://img9.domain.xy/t/929/320/2_2259929.jpg;http://img9.domain.xy/t/929/320/3_2259929.jpg;http://img9.domain.xy/t/929/320/4_2259929.jpg;http://img9.domain.xy/t/929/320/5_2259929.jpg;http://img9.domain.xy/t/929/320/6_2259929.jpg;http://img9.domain.xy/t/929/320/7_2259929.jpg;http://img9.domain.xy/t/929/320/8_2259929.jpg;http://img9.domain.xy/t/929/320/9_2259929.jpg;http://img9.domain.xy/t/929/320/10_2259929.jpg|13m5s 

,并给出我的输出。

<iframe src="http://domain.xy/load.php?file=2259929" frameborder="0" scrolling="no"></iframe>|1_2259929.jpg;2_2259929.jpg;3_2259929.jpg;4_2259929.jpg;5_2259929.jpg;6_2259929.jpg;7_2259929.jpg;8_2259929.jpg;9_2259929.jpg;10_2259929.jpg|13m5s|http://img9.domain.xy/t/929/320/ 

所有的作品都是正确的!

但也有一个时间值,我想改变。 13m5s到00:13:5或更好的其他13m5s到00:13:05

我尝试在循环结束时使用另一个grep + sed命令。

while IFS= read -r line; 
do 
var=$(echo "$line" | grep -oE "http://img[0-9].domain.xy/t/[0-9][0-9][0-9]/[0-9][0-9][0-9]/" | uniq); 
echo "$line" | sed -e 's|http://img[0-9].domain.xy/t/[0-9][0-9][0-9]/[0-9][0-9][0-9]/||g' -e "s|.*|&"${var}"|g" >> newFile; 
done < file; 
grep -oE "[0-9]*m[0-9]*[0-9]s" newFile | sed -e 's|^|00:|' -e s'|m|:|' -e s'|s||' 

这给了我只有输出的数字不是完整的行。

00:13:5
00:3:18
00:1:50

我怎样才能得到充分的线,只是改变13m5s至00:13 :5?

如果只是在没有grep的while循环之后使用sed,它会更改错误的字母。并把00:放在每一行的开头。

什么是最好的处理方法。我认为它是最好的将命令集成到现有的循环中。但我已经尝试了许多不同的变化,结果。

THX帮助

THX

+0

如果你想要整行而不是匹配的部分,你为什么使用'-o'选项? – Barmar

+0

@ barmar如果-o不是sed,输出仍然是13m5s。所以我想我需要grep完全匹配,然后运行sed命令。 – Malik

回答

1

我分开了你的代码中一些额外的作品进行了解是怎么回事更容易。下面是我相信结果是正确的:

# Read each field in to separate variables 
while IFS='|' read iframe urls time; do 
     # Get the first URL from the ';'-separated list 
     url="${urls%%;*}" 
     # Get the base URL by matching up to the last '/' (and add it back since the match is exclusive) 
     base_url="${url%/*}"'/' 

     # Remove the base URL from the list of full URLs so only the filenames are left 
     files="${urls//$base_url/}" 

     # Parse the minute and second out from the '##m#s' string 
     IFS='ms' read min sec <<<"$time" 

     # Print the new line - note the numeric formatting in the third column 
     printf '%s|%s|00:%02d:%02d|%s\n' "$iframe" "$files" "$min" "$sec" "$base_url" 
done <file 

是回答有关如何打开13m5s00:13:05具体要求线这两个:

IFS='ms' read min sec <<<"$time" 

printf '%s|%s|00:%02d:%02d|%s\n' "$iframe" "$files" "$min" "$sec" "$base_url" 

read行使用IFS告诉它在字符ms上进行拆分,使其能够轻松读取分钟和秒钟变量。

printf00:%02d:%02d具体将$min$sec变量格式化为零填充的两位数字。

+0

那个soloution很不错。大thx为好摘要! – Malik

1

grep仅输出匹配表达式的所有行。用sed的内置线路匹配,限制替代某些行:

sed '/[0-9]*m[0-9]*[0-9]s/{s|^|00:|;s|m|:|;s'|s||;}' 

也许这样的:

sed 's/\([0-9]*\)m\([0-9]*[0-9]\)s/00:\1:\2/' 
+0

是的,这就是我的例子工作。来自用户iscfrc的解决方案也非常有效,并为我工作。 – Malik