LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-24-2013, 01:33 PM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,091

Rep: Reputation: 288Reputation: 288Reputation: 288
Padding a character matrix with lines of unequal length


This is a learning exercise.

Have: a character matrix with lines of unequal length ...
(Note: $ signifies the end of each line)
Code:
ABC$
FGHIJ$
 LM $
PQR$
Want: a character matrix with lines of equal length, achieved by padding with blanks as necessary ...
(Note: $ signifies the end of each line)
Code:
ABC  $
FGHIJ$
 LM  $
PQR  $
I did this with awk ...
Code:
awk -F "" -v nc=$(wc -L < $Ragged)  \
  '{for (j=1;j<=nc;j++)
    {if (j<=NF) printf $j; else printf " ";}
    print ""}' $Ragged >$OutFile
Question: can the same transformation be made with sed?

Daniel B. Martin
 
Old 03-24-2013, 01:50 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Yes. This is a famous sed one-liner that basically explains the loops in sed:
Code:
sed -e :a -e 's/^.\{1,4\}$/& /;ta' file
To put the maximum number of characters in a line, you can do the same as in your example, using double quotes and arithmetic plus command substitution:
Code:
sed -e :a -e "s/^.\{1,$(($(wc -L < file)-1))\}$/& /;ta" file
You can find a detailed explanation here (see #26).

Last edited by colucix; 03-24-2013 at 02:27 PM. Reason: Corrected number in range: it should be N-1
 
1 members found this post helpful.
Old 03-24-2013, 02:05 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,502

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
You do know that from an awk point of view you could have just said:
Code:
awk  '{printf "%-5s\n",$0}' file
 
2 members found this post helpful.
Old 03-24-2013, 02:36 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,091

Original Poster
Rep: Reputation: 288Reputation: 288Reputation: 288
Quote:
Originally Posted by grail View Post
You do know that from an awk point of view you could have just said:
Code:
awk  '{printf "%-5s\n",$0}' file
I didn't know, but do now. Thank you!

I generalized your awk this way ...
Code:
echo "Pad all text with blanks to match longest line in the file"
echo "Based on method of LQ Guru grail"
echo; echo "Ragged ... ($ signifies end of line) ..."; cat -E $Ragged
awk  '{printf "%-'$(wc -L < $Ragged)'s\n",$0}' $Ragged >$OutFile
echo; echo "OutFile ...  ($ signifies end of line)"; cat -E $OutFile
... which produced this ...
Code:
Pad all text to match longest line in the file
Based on method of LQ Guru grail

Ragged ... ($ signifies end of line) ...
ABC$
FGHIJ$
 LM $
PQR$

OutFile ...  ($ signifies end of line)
ABC  $
FGHIJ$
 LM  $
PQR  $
Daniel B. Martin

Last edited by danielbmartin; 03-24-2013 at 04:31 PM. Reason: Improved wording of comments
 
Old 03-24-2013, 02:59 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,091

Original Poster
Rep: Reputation: 288Reputation: 288Reputation: 288
[QUOTE=colucix;4917849]
Code:
sed -e :a -e "s/^.\{1,$(($(wc -L < file)-1))\}$/& /;ta" file
Excellent. Instructive. Thank you for the reference. LQ teaches me every day!

SOLVED!

Daniel B. Martin
 
  


Reply

Tags
awk, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to write a script to sort pairs of lines by the length of the 2nd in the pair kmkocot Programming 3 06-02-2010 10:40 PM
Delete lines from a file by their's length dayamoon Linux - Newbie 15 04-27-2010 07:58 PM
[SOLVED] I can know the length in lines of a man page but not of an info manual. stf92 Linux - Newbie 3 12-10-2009 02:41 AM
Echo sequence of numbers with '0' padding for length of 3 chars in CLI koobi Programming 9 11-16-2009 04:40 AM
I need extend password length to 12 character or more HIDOKAI AIX 3 03-19-2006 11:58 PM


All times are GMT -5. The time now is 03:01 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration