How can I indent a file such as its first line?
Example:
A file containing
x=1+2+3+4+ 5+6+7+8 +9+10+12 should be converted to
x=1+2+3+4+ 5+6+7+8 +9+10+12 I need this inside a shell-script on a Linux system. One-liners are preferred.
One way using perl:
perl -pe 'if ($. == 1) { m/^(\s*)/; $space = $1 || q{}; next } s/^\s*/$space/' infile It yields:
x=1+2+3+4+ 5+6+7+8 +9+10+12 You can do this in awk:
awk 'NR==1{split($0,a,/[^ \t]/)}{sub(/^[ \t]*/,a[1]);print}' file.in With sed:
sed -e '1{h;s/[^[:blank:]].*//;x;b' -e '}' -e 'G;s/[[:blank:]]*\(.*\)\n\(.*\)/\2\1/'
x=?Perl. @Birei below posted a good answer. albeit i am increasingly likingawkas well