zoukankan      html  css  js  c++  java
  • Linux替换文件行首的空白字符

    使用命令sed、cp、tail、cat

    1、拷贝一个任意文件(生产环境切勿操作)

    cp /etc/profile /tmp

    查看文件部分格式

    cat /tmp/profile

    # /etc/profile
    
    # System wide environment and startup programs, for login setup
    # Functions and aliases go in /etc/bashrc
    
    # It's NOT a good idea to change this file unless you know what you
    # are doing. It's much better to create a custom.sh shell script in
    # /etc/profile.d/ to make custom changes to your environment, as this
    # will prevent the need for merging in future updates.
    
    pathmunge () {
        case ":${PATH}:" in
            *:"$1":*)
                ;;
            *)
                if [ "$2" = "after" ] ; then
                    PATH=$PATH:$1
                else
                    PATH=$1:$PATH
                fi
        esac
    }
    
    
    if [ -x /usr/bin/id ]; then
        if [ -z "$EUID" ]; then
            # ksh workaround
            EUID=`/usr/bin/id -u`
            UID=`/usr/bin/id -ru`
        fi
        USER="`/usr/bin/id -un`"
        LOGNAME=$USER
        MAIL="/var/spool/mail/$USER"
    .....
    .....
    .....

    2、使用sed命令替换

    sed 's/^ *//' /tmp/profile

    root@CentOS7[12:31:34]:/tmp# sed 's/^ *//' profile 
    # /etc/profile
    
    # System wide environment and startup programs, for login setup
    # Functions and aliases go in /etc/bashrc
    
    # It's NOT a good idea to change this file unless you know what you
    # are doing. It's much better to create a custom.sh shell script in
    # /etc/profile.d/ to make custom changes to your environment, as this
    # will prevent the need for merging in future updates.
    
    pathmunge () {
    case ":${PATH}:" in
    *:"$1":*)
    ;;
    *)
    if [ "$2" = "after" ] ; then
    PATH=$PATH:$1
    else
    PATH=$1:$PATH
    fi
    esac
    }
    
    
    if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
    # ksh workaround
    EUID=`/usr/bin/id -u`
    UID=`/usr/bin/id -ru`
    .....
    .....
    .....

    上述方法仅仅是输出到屏幕,如果想要应用到文件加上 -i参数即可

    sed -i 's/^ *//' /tmp/profile

  • 相关阅读:
    [九度][何海涛] 顺时针打印矩阵
    [何海涛] 求二元查找树的镜像
    [九度][何海涛] 二叉树中和为某一值的路径
    [面试] 水杯题实现
    [九度][何海涛] 最小的K个数
    [九度][何海涛] 字符串的排序
    如何扩展Orchard
    IoC容器Autofac(3) 理解Autofac原理,我实现的部分Autofac功能(附源码)
    使用PrivateObject帮助单元测试
    Nuget如何自动下载依赖DLL引用
  • 原文地址:https://www.cnblogs.com/stationing/p/11976076.html
Copyright © 2011-2022 走看看