UNIX:计算行号和字符串之间的字符

时间:2012-07-18 20:20:18

标签: svn unix

我目前正在将一个SVN服务器的内容迁移到另一个SVN服务器。长话短说,我尝试使用dos2unix转换一些行结尾,但它最终搞乱了大部分修订日志而没有修复任何内容。现在,由于文件错误格式错误,我无法使用svnadmin创建完整的转储文件。这些文件的结构如下:

K (# of chars in following line)
svn:author
V (# of chars in following line)
(author)
K ##
svn:date
V ## 
(date)
K ##
svn:log
V (# of chars in following lines)
revision
commit
text
END

错误源于上一个V错误后的数字。到目前为止,我一直在手动更改涉及复制文本的数字,在某处计算字符,更改数字,保存,然后运行svnadmin dump DIR > dumpfile.dmp以查找下一个。我一般不介意,但运行svnadmin dump命令需要一点时间,我将会运行它很多。

我的问题是:有没有办法编写一个脚本来计算从第12行到“END”之前的行尾的字符数,然后替换第11行中的数字?我对unix相对较新,并且调查了awk,sed等,但没有找到足够的东西。我意识到这些是相当独特的参数,所以任何有用的东西都是值得欢迎的。有没有办法列出char计数和数字不匹配的文件?这将大大加快我的工作。

感谢。

e:拼写

2 个答案:

答案 0 :(得分:0)

以下脚本应该有所帮助:

#!/bin/bash

# Print from line 12 to the end of the file
#   sed -n '12,$p' < $1 
#
# Then count the number of chars in those lines with 
#   wc -c 
#
# Later we take the number of chars in $NUM_CHARS and substract 4 from it 
# (the char count in the "END\n" token, assuming if has a line-end char, if not
# change 4 by 3) with
#   xargs -i expr {} - 4
NUM_CHARS=$(sed -n '12,$p' < $1 | wc -c | xargs -i expr {} - 4)

# Modify the line 11 in file 
sed "11 c\ $NUM_CHARS" $1  

将此代码保存到文件“script.sh”,赋予其执行权限并将其运行为:

./script.sh input_file

要将新生成的文件的内容保存到另一个,只需将以上脚本运行为:

./script.sh input_file > new_file

在原始文件中进行任何替换之前,请检查是否符合要求

答案 1 :(得分:0)

这是另一个版本:

file='path/to/file'
sed -i'' -e"11 s/\d+/$(expr $(tail -n+12 $file | wc -m) - 3)/" $file

sed -i'' -e"11 s/\d+/$(expr $(tail -n+12 /path/file | wc -m) - 3)/" /path/file

您需要先声明$file或将其替换为文件路径 运行这两行,它应该工作。