You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Fortran函数调用语法格式化:求sed/bash/awk解决方案

Solution using sed

Here's a sed command that will format your Fortran function calls exactly as you want:

sed -E '
  # Remove spaces between function name and opening parenthesis
  s/([a-zA-Z0-9_]+)\s+\(/\1(/g;
  # Remove spaces immediately before closing parenthesis
  s/\s+\)/)/g;
  # Remove leading spaces right after opening parenthesis
  s/\(\s+/(/g;
  # Remove trailing spaces right before closing parenthesis
  s/\s+\)/)/g;
  # Standardize comma spacing: one space after, no space before
  s/([^(),]+)\s*,\s*/\1, /g;
' your_input_file.f90 > formatted_output.f90

How it works:

Let's break down each step with your example inputs:

  1. Remove spaces between function name and (:

    • Turns func_example( x,y,z ) into func_example(x,y,z )
    • Matches any valid Fortran function name (letters, numbers, underscores) followed by one or more spaces and an opening parenthesis, then replaces the spaces with nothing.
  2. Remove spaces before ):

    • Turns func_example(x,y,z ) into func_example(x,y,z)
    • Eliminates any whitespace right before the closing parenthesis.
  3. Remove leading spaces inside (:

    • Turns another_one( x,y ) into another_one(x,y )
    • Gets rid of any spaces immediately after the opening parenthesis.
  4. Remove trailing spaces inside ):

    • This cleans up any remaining spaces right before the closing parenthesis (redundant in some cases but safe to keep).
  5. Standardize comma spacing:

    • Turns func_example(x,y,z) into func_example(x, y, z)
    • Ensures exactly one space after each comma, removing any extra spaces before or after the comma. Crucially, this ignores nested parentheses (like func(a, b(c), d)) because it only matches sequences of characters that don't include (, ), or commas.

For older sed versions (without -E):

If your sed doesn't support extended regular expressions, use the basic regex version (escape parentheses):

sed '
  s/\([a-zA-Z0-9_]\+\)\s\+(/\1(/g;
  s/\s\+)/)/g;
  s/(\s+/(/g;
  s/\s+)/)/g;
  s/\([^(),]\+\)\s*,\s*/\1, /g;
' your_input_file.f90 > formatted_output.f90
Alternative: awk solution

If you prefer awk, here's an equivalent script that does the same transformations:

{
  # Remove spaces between function name and opening parenthesis
  gsub(/([a-zA-Z0-9_]+)\s+\(/, "\1(");
  # Remove spaces before closing parenthesis
  gsub(/\s+\)/, ")");
  # Remove leading spaces after opening parenthesis
  gsub(/\(\s+/, "(");
  # Remove trailing spaces before closing parenthesis
  gsub(/\s+\)/, ")");
  # Standardize comma spacing
  gsub(/([^(),]+)\s*,\s*/, "\1, ");
  print;
}

Save this as format_funcs.awk and run it with:

awk -f format_funcs.awk your_input_file.f90 > formatted_output.f90

Both solutions handle all your test cases correctly, including nested parentheses, varying whitespace around commas, and spaces around parentheses.

内容的提问来源于stack exchange,提问作者EdouardIFP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:54:04