Bash 获得时间和削减与SOX

Bash 获得时间和削减与SOX,bash,sed,grep,sox,bc,Bash,Sed,Grep,Sox,Bc,我有一个小问题,采取音频时间从一个简单的文本文件和削减和音频文件与SoX 我有一个时间列表,如: 0 4.053 8.879 15.651 19.684 21.853 我需要用Sox做音频分区,如下所示: sox NAME.wav NEW_NAME.wav trim "$time" "$duration" 为此,我需要初始时间和持续时间。对于持续时间变量,我从讲座中跳一行,得到下一个值amb,进行减法运算: cat FILE.txt | while read line; do end_ti

我有一个小问题,采取音频时间从一个简单的文本文件和削减和音频文件与SoX

我有一个时间列表,如:

0
4.053
8.879
15.651
19.684
21.853
我需要用Sox做音频分区,如下所示:

sox NAME.wav NEW_NAME.wav trim "$time" "$duration"
为此,我需要初始时间和持续时间。对于持续时间变量,我从讲座中跳一行,得到下一个值amb,进行减法运算:

cat FILE.txt | while read line; 
do
end_time=`grep -A 1 $line FILE.txt | sed 1d`
start_time=$line
if [ -z "$end_time" ];
then
    end_time='21.853'
fi
#echo "This is start: $start_time"
#echo "This is end: $end_time"

duration=$(echo "$end_time-$start_time" | bc) 
#echo "DURATION: $duration"
done
但是我在Duration变量中遇到了一些错误,有人能帮我处理这个脚本吗


非常感谢您

不确定它是否适合您的需要,但这样做看起来更符合逻辑:

FILE.txt

0       4.053
8.879   15.651
19.684  21.853
DURATION: 4.053                                                                                                                                                          
DURATION: 6.772                                                                                                                                                          
DURATION: 2.169 
yourscript.sh

cat FILE.txt | while read start_time end_time; 
do
    if [ -z "$end_time" ];
    then
        end_time='21.853'
    fi
    #echo "This is start: $start_time"
    #echo "This is end: $end_time"

    duration=$(echo "$end_time-$start_time" | bc) 
    echo "DURATION: $duration"
done
输出

0       4.053
8.879   15.651
19.684  21.853
DURATION: 4.053                                                                                                                                                          
DURATION: 6.772                                                                                                                                                          
DURATION: 2.169 

不确定是否符合您的需要,但这样做看起来更符合逻辑:

FILE.txt

0       4.053
8.879   15.651
19.684  21.853
DURATION: 4.053                                                                                                                                                          
DURATION: 6.772                                                                                                                                                          
DURATION: 2.169 
yourscript.sh

cat FILE.txt | while read start_time end_time; 
do
    if [ -z "$end_time" ];
    then
        end_time='21.853'
    fi
    #echo "This is start: $start_time"
    #echo "This is end: $end_time"

    duration=$(echo "$end_time-$start_time" | bc) 
    echo "DURATION: $duration"
done
输出

0       4.053
8.879   15.651
19.684  21.853
DURATION: 4.053                                                                                                                                                          
DURATION: 6.772                                                                                                                                                          
DURATION: 2.169 
在awk里不容易吗?或批处理是必需的(使用printf内容和任何其他信息调整输出)

在awk里不容易吗?或批处理是必需的(使用printf内容和任何其他信息调整输出)


只要使用awk,这类任务就是它的设计目的:

$ awk '!(NR%2){print "sox NAME.wav NEW_NAME.wav trim", p, $0-p} {p=$0}' file
sox NAME.wav NEW_NAME.wav trim 0 4.053
sox NAME.wav NEW_NAME.wav trim 8.879 6.772
sox NAME.wav NEW_NAME.wav trim 19.684 2.169

只要使用awk,这类任务就是它的设计目的:

$ awk '!(NR%2){print "sox NAME.wav NEW_NAME.wav trim", p, $0-p} {p=$0}' file
sox NAME.wav NEW_NAME.wav trim 0 4.053
sox NAME.wav NEW_NAME.wav trim 8.879 6.772
sox NAME.wav NEW_NAME.wav trim 19.684 2.169

区别在FILE.txt中。我只有一个专栏,但谢谢dekkardYes,这就是我的想法——你把相关的值放在同一行。至少它更明显,并且在某种程度上防止忘记应该有偶数个值。即使通过UOOC,任何时候只要在shell中编写一个循环来处理文本,您都有错误的方法。以上所有内容都可以替换为
awk'{print“DURATION:,$2-$1}'文件
。区别在于file.txt。我只有一个专栏,但谢谢dekkardYes,这就是我的想法——你把相关的值放在同一行。至少它更明显,并且在某种程度上防止忘记应该有偶数个值。即使通过UOOC,任何时候只要在shell中编写一个循环来处理文本,您都有错误的方法。以上全部内容可以替换为
awk'{print“DURATION:,$2-$1}'文件