How to add 100 spaces at the end of each line of a file in Unix

I have a file that should contain 200 characters in each line. I got a source file containing only 100 characters in each line. I need to add 100 extra spaces to each line. If there were a few spaces, we could use sed as:

 sed 's/$/     /' filename > newfilename

Since it's 100 spaces, can someone tell me if it can be added to Unix?

+4
source share
7 answers

WITH awk

awk '{printf "%s%100s\n", $0, ""}' file.dat

$0 applies to the entire line.

+3
source

n ( m ), . :

$ cat file
1
12
123
1234
12345

10 .

$ awk '{printf "%-10s\n", $0}' file | cat -e

1         $
12        $
123       $
1234      $
12345     $

, 10 200 script. $ , , . cat -e, , .

+3

, , , - , , , say .

perl -nlE 'say $_," "x100' file

Perl:

perl -pe 's/$/" " x 100/e' file

... " (s) ($) (e) 100 ".

, , 200 , ( ), - :

perl -pe '$pad=200-length;s/$/" " x $pad/e'

83, 102 197 200 .

+2

bash,

while IFS=  read -r line
    do
    printf "%s%100s\n" "$line" 
done < file > newfile

Test

, 3 ,

$ wc -c file
      16 file
$ wc -c newfile
     316 newfile

spaces=$(echo {1..101} | tr -d 0-9)
while read line
    do
    echo -e "${line}${spaces}\n" >> newfile
done < file
+2

printf awk:

awk '{printf "%s%*.s\n", $0, 100, " "}' filename > newfile

printf 100 .

+1

GNU awk - sprintf.

awk 'BEGIN{s=sprintf("%-100s", "");}{print $0 s}' input-file > file-with-spaces

: -

$ cat input-file
1234jjj hdhyvb 1234jjj
6789mmm mddyss skjhude
khora77 koemm  sado666
nn1004  nn1004 457fffy
$ wc -c input-file
      92 input-file
$ awk 'BEGIN{s=sprintf("%-100s", "");}{print $0 s}' input-file > file-with-spaces
$ wc -c file-with-spaces
      492 file-with-spaces
+1

Bash, sed, readline, 100 (. manual for "Readline arguments" ).

:

sed 's/$/

100 . , readline, , , 100 , .. , , :

M-1 0 0 \040

, alt: Alt + 1 0 0 Space

100 ,

sed 's/$/                                                                                                    /' filename

.

This is useful for working in an interactive shell, but not very beautiful for scripts - use any of the other solutions for this.

+1
source

Source: https://habr.com/ru/post/1663000/


All Articles