我想在某些 fortran 程序中格式化函数调用的语法,但我找不到满足我需求的方法。
源代码中可能遇到的语法可以是:
func_example( x,y,z )
other_func_example(x,y,z, t)
another_one( x,y )
...
我需要的语法正是:
func_example(x, y, z)
other_func_example(x, y, z, t)
another_one(x, y)
...
我找到了一个完全去除大括号之间空格的 sed 解决方案:
echo -e "function{x,y,z}n function{ x,y,z}n function{x,y,z }n function{ x,y,z }"
| sed -e '/{/,/}/{s#s*##g}'
这提供了:
function{x,y,z}
function{x,y,z}
function{x,y,z}
function{x,y,z}
这接近我的需求,但仍然存在问题:
- 不适用于标准括号。我应该以一种我无法到达的方式逃离他们。
- 逗号后不留空格
我将不胜感激对此的任何帮助。在我看来,Sed 是最好的选择。我也会接受任何完整的bash或awk解决方案,但我想避免使用perl,因为我对它几乎一无所知。
以下内容:
echo 'function( x,y,z )
function(x,y,z)
function(x, y,z)' |
sed 's/(function)([[:space:]]*([a-z]*)[[:space:]]*,[[:space:]]*([a-z]*)[[:space:]]*,[[:space:]]*([a-z]*)[[:space:]]*)/1(2, 3, 4)/'
输出:
function(x, y, z)
function(x, y, z)
function(x, y, z)
它:
- 将
function([a-z]*,[a-z]*,[a-z]*)
与所有标记之间的任意数量的[[:space:]]*
白字符(空格、制表符)匹配。 - 使用 sed 的
(...)
记住函数的名称和参数名称 - 然后输出带有空格和逗号的标记
1(2, 3, )
- 请注意,它也可以处理奇怪的输入,例如
function(,,)
- 如果要格式化某些 C 代码,请使用为此创建的
astyle
或indent
或其他实用程序。
当您想先删除逗号周围的空格时,您可以使用
... | sed -r 's/([(,]) */1/g; s/ ([),])/1/g; s/,/, /g'
替代方案:您可以开始用tr
或简单的方式删除所有空格
... | sed 's/ //g; s/,/, /g'
我找到了一个 awk 解决方案,它可能不是最聪明的解决方案,但它适用于任何数量的昏迷。 这个想法是生成一个脚本,其中包含一些要启动的 sed 命令。我分享它,以防它对某些读者有任何帮助。
输入测试文件:
some_func(x, y,z)
some_func_a(x, y, z, t )
some_func_b(x,y,z)
some_func_z(x, y , z)
some_func(x, y , z )
! some comment
some_func( x, y)
some_func_1(x)
some text
some_func(x, z, a, b,e)
some_func_da(x, y,d, z)
some_func_bla( x, y, z)
some_text without parenthesis
我来的脚本看起来像:
while read line
do
# Gets what's in parenthesis and the function name
in_par=$(echo $line|awk -F "[()]" '/(/{print "(" $2 ")" }')
func_name=$(echo $line|awk -F "[()]" '/(/{print $1}')
# Formats to my desired format
in_par_mod=$(echo $line|awk -F "[()]" '/(/{print $2}'|awk '{ gsub (" ", "", $0); print}'|awk 'BEGIN{FS=","; OFS=", "} {$1=$1; print "(" $0 ")"}')
# Re-buils full patterns
in_name=$func_name$in_par
mod_name=$func_name$in_par_mod
printf " Before : %-30s After : %-30s n" "$in_name" "$mod_name"
# Generating script to be launched
if [ ! -z "$in_name" ]
then
printf "sed -i 's/%s/%s/g' %s n " "$in_name" "$mod_name" "$in_file" >> sed_script.sed
else
printf "Line contains nothing to change n"
fi
done < $in_file
运行它会产生:
Before : some_func(x, y,z) After : some_func(x, y, z)
Before : some_func_a(x, y, z, t ) After : some_func_a(x, y, z, t)
Before : some_func_b(x,y,z) After : some_func_b(x, y, z)
Before : some_func_z(x, y , z) After : some_func_z(x, y, z)
Before : some_func(x, y , z ) After : some_func(x, y, z)
Line contains nothing to change
Before : some_func( x, y) After : some_func(x, y)
Before : some_func_1(x) After : some_func_1(x)
Line contains nothing to change
Before : some_func(x, z, a, b,e) After : some_func(x, z, a, b, e)
Before : some_func_da(x, y,d, z) After : some_func_da(x, y, d, z)
Before : some_func_bla( x, y, z) After : some_func_bla(x, y, z)
Line contains nothing to change
Line contains nothing to change
并生成以下脚本:
sed -i 's/some_func(x, y,z)/some_func(x, y, z)/g' in.txt
sed -i 's/some_func_a(x, y, z, t )/some_func_a(x, y, z, t)/g' in.txt
sed -i 's/some_func_b(x,y,z)/some_func_b(x, y, z)/g' in.txt
sed -i 's/some_func_z(x, y , z)/some_func_z(x, y, z)/g' in.txt
sed -i 's/some_func(x, y , z )/some_func(x, y, z)/g' in.txt
sed -i 's/some_func( x, y)/some_func(x, y)/g' in.txt
sed -i 's/some_func_1(x)/some_func_1(x)/g' in.txt
sed -i 's/some_func(x, z, a, b,e)/some_func(x, z, a, b, e)/g' in.txt
sed -i 's/some_func_da(x, y,d, z)/some_func_da(x, y, d, z)/g' in.txt
sed -i 's/some_func_bla( x, y, z)/some_func_bla(x, y, z)/g' in.txt
我仍然不胜感激任何帮助或评论来改进这种方法。