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?
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!
A little more specific... so far I can do the following. This would get the longest Line from a .txt File:
awk '{ if (length($0) > max) {max = length($0); maxline = $0} } END { print maxline }' in.txt
This fills the lines with blank spaces (till 50):
awk 'length <= 50 { printf "%-50s\n",$0 }' in.txt > out.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 b开发者_开发技巧e 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.
Usually I find that this type of question is a result of this thought process:
- I am trying to solve problem A
- I think doing process B will solve A
- I will ask how to achieve process 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.
So, what problem are you trying to solve by making all the lines in a file the same length?
This is all you need:
pr -W 80 -mtT file1 file2
Or, more verbosely:
pr --page-width=80 --merge --omit-header --omit-pagination file1 file2
Vary the number to change the layout of the result.
wc -L
or wc --max-line-length
computes and displays the length of the longest line in the input (may not be available on all versions of wc
).
With the max line length in some variable (say, $max
), run
while read line
do
printf "%-${max}s\n" $line
done < in.txt > out.txt
You can use wc
to 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
).
Update:
Is using awk
a requirement? If not, try this:
# 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
Edit #2:
If you don't have the -L
option to wc
, you can calculate the length of the longest line using the following loop:
maxlen=0
while read line
do
thislen=`echo $line | wc -c`
[ $[$thislen>$maxlen] ] && maxlen=$thislen
done < filename.txt
The ending value of $maxlen
will be the length of the longest line.
here's how one way to it with just awk.
$ 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 "|";$0=$0s;s=""}1' file file
jlsf||||
slf|||||
asdfasfs
sd||||||
Change "|" to spaces as desired.
If 'shell scripting' can include Python scripts, something like this:
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 ()
Fixing any off-by-one errors is left as an exercise for the reader! :-)
You could combine your original ideas:
awk '{ if (length($0) > max){max = length($0)} } END { print max }' 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 -v
argument parsing
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
OR you could replace the hard-coded 50
with shell $()
command substitution to create this ugly one-line beast:
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
精彩评论