Re: right justify each line?

"Daniel Goldman [email protected] [sed-users]" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
Yes, I think you understand it perfectly. It was just tricky for me to 
explain the scenario because of the yahoo spaces behavior.

$ cat input.x
99---
---99
-9---
-9-9-
99999

$ sed -r 's|-*$||; s|^|-----|; s|.*(.....$)|\1|' input.x
---99
---99
----9
--9-9
99999

The shell script also works correctly, to build the sed command, when 
operating on the input file with spaces. As you suggest:

$ cat input.x
99---
---99
-9---
-9-9-
99999

1) remove trailing blanks (shown as dashes), the basic right 
justification step. Result:

99
---99
-9
-9-9
99999

2) put 5 blanks (shown as dashes) at start of line. Result:

-----99
--------99
------9
------9-9
-----99999

3) retain last five chars in line. Result:

---99
---99
----9
--9-9
99999

(BTW, I know it actually goes one line at a time, does not generate 
"intermediate files". I am just showing the intermediate forms above to 
illustrate what happens to each line)

I was hoping for something simpler, but this is fine, plenty simple 
enough. I think that if different lines had different lengths, this 
might not work correctly, because the output lines would all be same 
length, perhaps an undesired result. But in my case, all the lines are 
the same length, so no problem. :) Also, I actually know the line length 
before calling the shell script, so I could pass that as an argument, 
and forgo the wc command.

If someone else has an alternative solution, I'm interested, but this 
solution works just fine, thank you Thierry. And thank you Rakesh for 
seeing the formatting problem with the spaces.

Daniel

On 11/14/2015 12:17 PM, Thierry Blanc [sed-users] wrote:

> Now I understand the problem, hopefully.
>
>
> sed -r 's|-*$||;s|^|-----|;s|.*(.....$)|\1|'
>
> delete trailing spaces at end of line first.
> then like before
>
> How to get line length is another issue. If you can script, then
>
> wc -L < file
>
> for line length.
> script (spaces, not hyphen):
>
> #!/bin/bash
>
>
> L=$(wc -L < $1); SPACES=$(printf %${L}s)
> DOTS=$(printf %${L}s |tr " " ".")
>
> sed -r 's| *$||;s|^|'"$SPACES"'|;s|.*('"$DOTS"'$)|\1|' $1
>
>
>
> On 14/11/15 20:40, Daniel Goldman [sed-users] wrote:
>> Thank you for trying. But I don't think that right justifies. Actually,
>> I don't think it does any net effect...
>>
>> Correct me if I'm wrong, but AFAICT: 1) s #1 adds 5 spaces to beginning
>> of each line. 2) s #2 only retains last 5 chars in the line (omits 5
>> leading spaces), leaving us back where we started. :(
>>
>> Even if it did work, it can't have a hard coded character count like
>> five dots in (.....$) because as I mentioned, in this case, each line
>> has five chars, but it could be a different number of chars.
>>
>> Given how yahoo mangles the posts (it messed up Thierry's post on yahoo
>> website, squeezing the blanks down to one blank), I would like to recast
>> the input and output files to the form with the dashes.
>>
>> $ cat input.x
>> 99---
>> ---99
>> -9---
>> -9-9-
>> 99999
>>
>> $ cat output.x
>> ---99
>> ---99
>> ----9
>> --9-9
>> 99999
>>
>> I believe it's exactly the same scenario, just pad on the left with
>> dashes instead of blanks, and we can avoid confusion from yahoo playing
>> around with blanks. If the solution works for the case with the dashes,
>> it will work for the case with spaces.
>>
>> sed -r "s/^(.*)(-*)$/\2\1/" would be my initial failed try
>> sed -r 's|^|-----|;s|.*(.....$)|\1|' would be Thierry's try
>>
>> Thanks,
>> Daniel
>>
>> On 11/14/2015 10:33 AM, Thierry Blanc [sed-users] wrote:
>>>
>>> sed -r 's|^|     |;s|.*(.....$)|\1|'
>>>
>>> add spaces to the beginning of line, as many as the maximum line width
>>> cut from the end of the line the amount of chars of the line width
>>>
>>>
>>>
>>> On 14/11/15 19:19, Daniel Goldman [email protected] [sed-users] wrote:
>>>> I double-checked my sent folder, and a copy sent to myself. Actually, I
>>>> did post sensibly, but yahoo did not transmit sensibly. Unfortunately,
>>>> yahoo stripped out leading blanks from the lines, so yes it ended up
>>>> input.x = output.x :( So so thanks for asking.
>>>>
>>>> Please look at the following, and imagine that the colons are not there,
>>>> and that's how I posted. In this case, each line has five characters,
>>>> but it could be a different number of characters.
>>>>
>>>> $ cat input.x
>>>> :99   :
>>>> :   99:
>>>> : 9   :
>>>> : 9 9 :
>>>> :99999:
>>>>
>>>> $ cat output.x
>>>> :   99:
>>>> :   99:
>>>> :    9:
>>>> :  9 9:
>>>> :99999:
>>>>
>>>> In case yahoo mangles the above, here is another way to display it,
>>>> where you would please imagine that each '-' is a blank.
>>>>
>>>> $ cat input.x
>>>> 99---
>>>> ---99
>>>> -9---
>>>> -9-9-
>>>> 99999
>>>>
>>>> $ cat output.x
>>>> ---99
>>>> ---99
>>>> ----9
>>>> --9-9
>>>> 99999
>>>>
>>>> Thanks,
>>>> Daniel
>>>>
>>>> On 11/14/2015 9:36 AM, Rakesh Sharma wrote:
>>>>> Your input.x = output.x and also what do you mean each line in input.x has the same length? look at the line 99999.
>>>>>
>>>>> You need to post sensibly first.
>>>>>
>>>>>
>>>>> ________________________________
>>>>> From: [email protected] <[email protected]> on behalf of Daniel Goldman [email protected] [sed-users] <[email protected]>
>>>>> Sent: Saturday, November 14, 2015 9:25 AM
>>>>> To: [email protected]
>>>>> Subject: right justify each line?
>>>>>
>>>>>
>>>>>
>>>>> Hi,
>>>>>
>>>>> Assume each line in input.x has the same length.
>>>>>
>>>>> $ cat input.x
>>>>> 99
>>>>> 99
>>>>> 9
>>>>> 9 9
>>>>> 99999
>>>>>
>>>>> I want to right justify each line, so output.x would be:
>>>>>
>>>>> $ cat output.x
>>>>> 99
>>>>> 99
>>>>> 9
>>>>> 9 9
>>>>> 99999
>>>>>
>>>>> What is simplest way to convert input.x to output.x?
>>>>>
>>>>> I tried the following, but does not work, because of greedy evaluation,
>>>>> \2 always encompasses the entire line.
>>>>>
>>>>> $ sed "s/^\(.*\)\( *\)$/\2\1/" input.x
>>>>>
>>>>> Thanks,
>>>>> Daniel
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> [Non-text portions of this message have been removed]
>>>>>
>>>>>
>>>
>>>
>>> ------------------------------------
>>>
>>> ------------------------------------
>>>
>>
>> ------------------------------------
>>
>> ------------------------------------
>>
>
>
>
>
> ------------------------------------
>
> ------------------------------------
>


------------------------------------

------------------------------------

-- 

------------------------------------

Yahoo Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/sed-users/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/sed-users/join
    (Yahoo! ID required)

<*> To change settings via email:
    [email protected] 
    [email protected]

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo Groups is subject to:
    https://info.yahoo.com/legal/us/yahoo/utos/terms/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.