Unixスクリプトを使用して複数行のif文を1行のif文に変換するには? [閉鎖]

Unixスクリプトを使用して複数行のif文を1行のif文に変換するには? [閉鎖]

ファイルには次の条件がいくつかあります。

if ( a==b ||
c!=d &&
(e>f))
{
do something
}

複数行のifステートメントを1行のifステートメントに変換する必要があります。つまり、

if ( a==b || c!=d && (e>f))
{
do something
}

Unixスクリプトを使用してこの変換をどのように実行できますか?

ベストアンサー1

インデントツール(またはindent)を介して実行しますclang-format。 C および C++ の構文規則を理解し、構成方法に応じてコード形式を再指定します。clang-formatコンパイラclangと同じ言語パーサも使用します。

たとえば、

$ cat test.c
if ( a==b ||
c!=d &&
(e>f))
{
/* do something */
}

$ clang-format test.c
if (a == b || c != d && (e > f)) {
  /* do something */
}

$ clang-format --style="{BreakBeforeBraces: Allman}" test.c
if (a == b || c != d && (e > f))
{
  /* do something */
}

$ indent -kr -st <test.c
if (a == b || c != d && (e > f)) {
/* do something */
}

$ indent -kr -bl -st < test.c
if (a == b || c != d && (e > f))
{
/* do something */
}

indentindent上記の例では、BSDシステムのデフォルト値の代わりにGNUを使用しています。indent

おすすめ記事