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

后端 未结 7 1215
名媛妹妹
名媛妹妹 2021-01-12 07:17

I have a file which is supposed to contain 200 characters in each line. I received a source file with only 100 characters in each line. I need to add 100 extra white spaces

7条回答
  •  一生所求
    2021-01-12 07:27

    Updated after Glenn's suggestion

    Somewhat how Glenn suggests in the comments, the substitution is unnecessary, you can just add the spaces - although, taking that logic further, you don't even need the addition, you can just say them after the original line.

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

    Original Answer

    With Perl:

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

    That says... "Substitute (s) the end of each line ($) with the calculated expression (e) of 100 repetitions of a space".

    If you wanted to pad all lines to, say, 200 characters even if the input file was ragged (all lines of differing length), you could use something like this:

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

    which would make up lines of 83, 102 and 197 characters to 200 each.

提交回复
热议问题