bash 在 .txt 文件中查找最长的行并用“空格”将所有行填充到该长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3111870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:15:08  来源:igfitidea点击:

Find Longest Line in a .txt File and fill all Lines to that Length with 'blank Spaces'?

bashtextmacosshell

提问by noexpert

How can I find the longest line in a .txt file and then fill all other lines at their end to that length with blank spaces?

如何在 .txt 文件中找到最长的行,然后用空格将其末尾的所有其他行填充到该长度?

My guess is this is easy to answer. I know very little about using the awk, paste command and such. Maybe someone can help me out. Thanks!

我的猜测是这很容易回答。我对使用 awk、paste 命令等知之甚少。也许有人可以帮助我。谢谢!

A little more specific... so far I can do the following. This would get the longest Line from a .txt File:

更具体一点......到目前为止,我可以做到以下几点。这将从 .txt 文件中获得最长的行:

awk '{ if (length(
awk  'length <= 50 { printf "%-50s\n",
pr  -W 80 -mtT file1 file2
}' in.txt > out.txt
) > max) {max = length(
pr --page-width=80 --merge --omit-header --omit-pagination file1 file2
); maxline =
# Measure the longest line in the file
maxlen=`wc -L filename.txt | cut -d ' ' -f 1`

# Pad each line to $maxlen characters
while read line
do
    printf "%-${maxlen}s\n" "$line" >> outfile.txt
done < filename.txt
} } END { print maxline }' in.txt

This fills the lines with blank spaces (till 50):

这用空格填充行(直到 50):

maxlen=0
while read line
do
    thislen=`echo $line | wc -c`
    [ $[$thislen>$maxlen] ] && maxlen=$thislen
done < filename.txt

I just don't know to pass the value from one line to the other.

我只是不知道将值从一行传递到另一行。

Why am I asking this? I want to merge two .txt files using the paste command. Text B will be positioned to the right of Text A. Lines in Text A will have different lengths. So if there are not enough blank spaces the layout isn't right.

我为什么要问这个?我想使用 paste 命令合并两个 .txt 文件。文本 B 将位于文本 A 的右侧。文本 A 中的行将具有不同的长度。所以如果没有足够的空格,布局就不正确。

回答by Ron Savage

Usually I find that this type of question is a result of this thought process:

通常我发现这类问题是这种思考过程的结果:

  1. I am trying to solve problem A
  2. I think doing process B will solve A
  3. I will ask how to achieve process B
  1. 我正在尝试解决问题 A
  2. 我认为做过程B会解决A
  3. 请问怎么实现过程B

You will get literal answers on how to achieve process B - but if you include the context of problem A, you will get better answers and probably one that solves problem A in a simpler manner than process B.

您将获得有关如何实现流程 B 的字面答案——但如果您包括问题 A 的上下文,您将获得更好的答案,并且可能以比流程 B 更简单的方式解决问题 A。

So, what problem are you trying to solve by making all the lines in a file the same length?

那么,通过使文件中的所有行长度相同,您试图解决什么问题?

回答by Paused until further notice.

This is all you need:

这就是你所需要的:

$ more file
jlsf
slf
asdfasfs
sd

$ awk 'FNR==NR{t=(length>=t)?length:t;next}length<t{for(o=1;o<=t-length;o++)s=s "|";
while read line
do
    printf "%-${max}s\n" $line
done < in.txt > out.txt
=
maxLen = 0
infile = open("file.txt", 'r')
outfile = open("out.txt", 'w')
for line in infile:
    if len(line)>maxLen: maxLen = len(line)
infile.seek(0)
for line in infile:
    rawline = line.strip('\r\n')
    out.write (rawline + ''.join([' ' for i in range(maxLen-len(rawline))]) + "\n")
infile.close ()
outfile.close ()
s;s=""}1' file file jlsf|||| slf||||| asdfasfs sd||||||

Or, more verbosely:

或者,更详细地说:

##代码##

Vary the number to change the layout of the result.

改变数字以改变结果的布局。

回答by bta

You can use wcto count the number of characters in a line. Measure all lines in the file to find the longest length. For all other files, (max length - line length) gives you the number of space characters to print at the end of the line (which you can do with printf).

您可以使用wc来计算一行中的字符数。测量文件中的所有行以找到最长的长度。对于所有其他文件, (max length - line length) 为您提供要在行尾打印的空格字符数(您可以使用printf)。

Update:Is using awka requirement? If not, try this:

更新:使用awk要求吗?如果没有,试试这个:

##代码##

Edit #2:If you don't have the -Loption to wc, you can calculate the length of the longest line using the following loop:

编辑 #2:如果您没有-L选项wc,则可以使用以下循环计算最长线的长度:

##代码##

The ending value of $maxlenwill be the length of the longest line.

的结束值$maxlen将是最长行的长度。

回答by ghostdog74

here's how one way to it with just awk.

这是仅使用 awk 的一种方法。

##代码##

Change "|" to spaces as desired.

更改“|” 到所需的空间。

回答by mob

wc -Lor wc --max-line-lengthcomputes and displays the length of the longest line in the input (may not be available on all versions of wc).

wc -Lwc --max-line-length计算并显示输入中最长行的长度(可能不适用于 的所有版本wc)。



With the max line length in some variable (say, $max), run

使用某个变量中的最大行长(例如,$max),运行

##代码##

回答by S..

If 'shell scripting' can include Python scripts, something like this:

如果“shell 脚本”可以包含 Python 脚本,则如下所示:

##代码##

Fixing any off-by-one errors is left as an exercise for the reader! :-)

修复任何一个错误的错误留给读者作为练习!:-)

回答by Chris Koknat

You could combine your original ideas:

你可以结合你的原始想法:

awk '{ if (length($0) > max){max = length($0)} } END { print max }' in.txt

awk '{ if (length($0) > max){max = length($0)} } END { print max }' in.txt

awk 'length <= 50 { printf "%-50s\n",$0 }' in.txt

awk 'length <= 50 { printf "%-50s\n",$0 }' in.txt

Use a shell variable to pass the result from one script to another, using awk's -vargument parsing

使用 shell 变量将结果从一个脚本传递到另一个脚本,使用 awk 的-v参数解析

MAX=$(awk '{ if (length($0) > max){max = length($0)} } END { print max }' in.txt)

MAX=$(awk '{ if (length($0) > max){max = length($0)} } END { print max }' in.txt)

awk -v max=$MAX 'length <= max { printf "%-"max"sX\n",$0 }' in.txt

awk -v max=$MAX 'length <= max { printf "%-"max"sX\n",$0 }' in.txt

OR you could replace the hard-coded 50with shell $()command substitution to create this ugly one-line beast:

或者你可以50用 shell$()命令替换替换硬编码来创建这个丑陋的单行野兽:

awk -v max=$(awk '{ if (length($0) > max){max = length($0)} } END { print max }' in.txt) 'length <= max { printf "%-"max"s\n",$0 }' in.txt

awk -v max=$(awk '{ if (length($0) > max){max = length($0)} } END { print max }' in.txt) 'length <= max { printf "%-"max"s\n",$0 }' in.txt