如何将每行的第二个字符串重新定位为从同一行的第n个字符开始

时间:2020-06-18 13:09:16

标签: bash awk sed

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  

如果我使用-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

3 个答案:

答案 0 :(得分:1)

尝试一下这种单线:

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

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

答案 1 :(得分:1)

假设每行的开头clear derived data中没有#

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 是否正确,按摩一下以适应。

答案 2 :(得分:0)

纯重击-

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