string - 如何在 bash 中处理长字符串并仍然获得格式良好的代码?

标签 string bash indentation

这个和我的 previous question 有点相关。现在我正在处理一个长字符串:

sed -b -i.old $inifile \
    -e "s/a_very_long_pattern_i_should_problably_improve/terribly_long_replace_string" 

有没有办法让它格式正确(即缩进且不超过 80 个字符)?使用反斜杠会添加额外的空格:

$ echo 'first_part'\
>      'second_part'
first_part second_part

最佳答案

$ echo 'first_part'\
>      'second_part'
first_part second_part

Bash 连接相邻的字符串文字。

例如,如果您echo "first_part""second_part",您将得到

第一部分第二部分

但是,如果您echo "first_part""second_part",您将得到first_partsecond_part

因此,请利用这一点为您服务

   -e "s/a_very_long_pattern_i_should_problably_improve/terribly_long_replace_string"

你可以尝试做类似的事情

sed -b -i.old $inifile \  
    -e "s/a_very_long_pattern_"
                   "i_should_problably_improve/"
                   "terribly_long_replace_string" 

关于string - 如何在 bash 中处理长字符串并仍然获得格式良好的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37857867/

相关文章:

java - 在特定的其他子字符串之后获取子字符串的最有效方法

jquery - 如何向 jQuery 选择器添加字符串?

c# - 返回新字符串与 .ToString()

git - 尝试重定向 'git gc' 输出

bash - 从/proc/stat 计算 CPU 使用率

vim中的html缩进

emacs - 使 Emacs 选项卡的行为与 vim 完全一样

c# - 什么时候应该使用 StringComparison.InvariantCulture 而不是 StringComparison.CurrentCulture 来测试字符串相等性?

linux - `SIGWINCH` 如何通过 Bash?

c - 将 C 代码中的左大括号移动到下一行?