jof*_*fel 2 shell-script text-processing indentation
我怎样才能缩进一个文件,比如它的第一行?
例子:
一个文件包含
x=1+2+3+4+
5+6+7+8
+9+10+12
Run Code Online (Sandbox Code Playgroud)
应该转换为
x=1+2+3+4+
5+6+7+8
+9+10+12
Run Code Online (Sandbox Code Playgroud)
我需要在 Linux 系统上的 shell 脚本中使用它。优选单衬。
一种使用方式perl
:
perl -pe 'if ($. == 1) { m/^(\s*)/; $space = $1 || q{}; next } s/^\s*/$space/' infile
Run Code Online (Sandbox Code Playgroud)
它产生:
x=1+2+3+4+
5+6+7+8
+9+10+12
Run Code Online (Sandbox Code Playgroud)
您可以在 awk 中执行此操作:
awk 'NR==1{split($0,a,/[^ \t]/)}{sub(/^[ \t]*/,a[1]);print}' file.in
Run Code Online (Sandbox Code Playgroud)