我们有以下文件
如何使用 sed 或 perl 单行衬垫将行对齐到左侧
more conf.env
# Extra ssh options. Empty by default.
export HADOOP_SSH_OPTS="-o ConnectTimeout=5 -o SendEnv=HADOOP_CONF_DIR"
# Where log files are stored. $HADOOP_HOME/logs by default.
export HADOOP_LOG_DIR={{hdfs_log_dir_prefix}}/$USER
# History server logs
export HADOOP_MAPRED_LOG_DIR={{mapred_log_dir_prefix}}/$USER
# Where log files are stored in the secure data environment.
export HADOOP_SECURE_DN_LOG_DIR={{hdfs_log_dir_prefix}}/$HADOOP_SECURE_DN_USER
# File naming remote slave hosts. $HADOOP_HOME/conf/slaves by default.
# export HADOOP_SLAVES=${HADOOP_HOME}/conf/slaves
预期成绩
more conf.env
# Extra ssh options. Empty by default.
export HADOOP_SSH_OPTS="-o ConnectTimeout=5 -o SendEnv=HADOOP_CONF_DIR"
# Where log files are stored. $HADOOP_HOME/logs by default.
export HADOOP_LOG_DIR={{hdfs_log_dir_prefix}}/$USER
# History server logs
export HADOOP_MAPRED_LOG_DIR={{mapred_log_dir_prefix}}/$USER
# Where log files are stored in the secure data environment.
export HADOOP_SECURE_DN_LOG_DIR={{hdfs_log_dir_prefix}}/$HADOOP_SECURE_DN_USER
# File naming remote slave hosts. $HADOOP_HOME/conf/slaves by default.
# export HADOOP_SLAVES=${HADOOP_HOME}/conf/slaves
答案1
这将删除每行开头的所有空格和制表符:
sed 's/^[ \t]*//'
在哪里
^
匹配行首[ \t]
匹配空格或制表符*
使前一个[…]
块工作零次或多次,尽可能多地工作(因此它可以匹配多个空格和/或制表符)
整个s/…//
命令将…
模式(^[ \t]*
在本例中)替换为无。
使用示例:
sed 's/^[ \t]*//' <input.txt >output.txt
或者就地(注意-i
这不是 POSIX 所要求的):
sed -i 's/^[ \t]*//' file.txt