【问题标题】:How to reposition the second string of each line to start at the nth character on the same line如何重新定位每行的第二个字符串以从同一行的第 n 个字符开始
【发布时间】:2020-06-22 16:36:52
【问题描述】:

Ubuntu 16.04
Bash V 4.4

这是我的文件:

4u76kumtstring5="${ThisLine__4u76kumtstring5}" # this vreis ethees 445h45thb ervexplanation forevrev
wevdssdstring6="${ThisLine__wevdssdstring6}" # this is theerve explanationver for wevdssdstring6 kj
rjjstring7="${ThisLine__rjjstring7}" # thise isder evervthe explanation for rjjstring7 eji
5trstring8="${ThisLine__5trstring8}" # this is ververthe explanation for 5trstring8 k8bf2kl  

如果我使用 column -t 我会得到这样的结果:

4u76kumtstring5="${ThisLine__4u76kumtstring5}"  #  this   vreis  ethees     445h45thb       ervexplanation  forevrev
wevdssdstring6="${ThisLine__wevdssdstring6}"    #  this   is     theerve    explanationver  for             wevdssdstring6  kj
rjjstring7="${ThisLine__rjjstring7}"            #  thise  isder  evervthe   explanation     for             rjjstring7      eji
5trstring8="${ThisLine__5trstring8}"            #  this   is     ververthe  explanation     for             5trstring8      k8bf2kl

我希望第二个字符串从该行的第 n 个字符开始

4u76kumtstring5="${ThisLine__4u76kumtstring5}"               # this vreis ethees 445h45thb ervexplanation forevrev
wevdssdstring6="${ThisLine__wevdssdstring6}"                 # this is theerve explanationver for wevdssdstring6 kj
rjjstring7="${ThisLine__rjjstring7}"                         # thise isder evervthe explanation for rjjstring7 eji
5trstring8="${ThisLine__5trstring8}"                         # this is ververthe explanation for 5trstring8 k8bf2kl

【问题讨论】:

    标签: bash awk sed


    【解决方案1】:

    假设每行的前导 name="$variable" 部分不能有 #

    $ awk -v n=60 '{h=$0; sub(/[[:space:]]*#.*/,"",h); sub(/[^#]+/,""); printf "%-*s%s\n", n, h, $0}' file
    4u76kumtstring5="${ThisLine__4u76kumtstring5}"              # this vreis ethees 445h45thb ervexplanation forevrev
    wevdssdstring6="${ThisLine__wevdssdstring6}"                # this is theerve explanationver for wevdssdstring6 kj
    rjjstring7="${ThisLine__rjjstring7}"                        # thise isder evervthe explanation for rjjstring7 eji
    5trstring8="${ThisLine__5trstring8}"                        # this is ververthe explanation for 5trstring8 k8bf2kl
    

    数一下空格,看看60是否是正确的数字,按摩以适应。

    【讨论】:

      【解决方案2】:

      试试这个 awk 单行代码:

      awk -v n='60' -F'#' '{printf "%-*s#%s\n",n,$1,$2}' file
      

      您可以将n='60' 更改为其他值。

      【讨论】:

      • 这会将哈希放在第 61 列中。
      • 这会在一秒钟后删除任何内容#,例如foo # we're #1! 将变为 foo # we're
      • @Kent Ed 是对的。我没有场景自动取款机,但我确实看到它很快就会在这里介绍。尽管如此,我很感激他的帮助。
      【解决方案3】:

      纯重击 -

      c2=$columnYouWantTheHash
      while IFS='#' read -r a b; do printf "%-$((c2-1))s%s\n" "$a" "#$b"; done < file
      

      【讨论】:

      • 警告 - 如果您的最后一行没有换行符,您需要考虑这一点。我假设第一列不能嵌入哈希。
      • 真相。我很不小心在格式中嵌入了$b。已编辑。
      • /headsmack - 添加。谢谢,埃德。
      猜你喜欢
      • 2021-01-05
      • 2016-02-06
      • 2015-01-17
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 2021-05-26
      相关资源
      最近更新 更多