Monday, May 5, 2014

Unix Shell Print Text: pr

1. print text: pr
test_1:
 Hello  
 world  
 This  
 is  
 really  
 awesome  

terminal:
We are using "pr" to prepare the "text printing", "pr" is not used to send text to the physical printer. After formatting the text and use pipe to transfer text to grep to help to filter out empty lines, we can see that, by default, pr just add the header and print out text line by line.

-c3 means separate the entire text into 3 columns, so with following commands, you can see that text are separated into 1 column, 2 columns and 3 columns

 aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ pr ./test_1 | grep -v "^$"  
 2014-05-05 19:18           ./test_1           Page 1  
 Hello  
 world  
 This  
 is  
 really  
 awesome  
 aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ pr -c1 ./test_1 | grep -v "^$"  
 2014-05-05 19:18           ./test_1           Page 1  
 Hello  
 world  
 This  
 is  
 really  
 awesome  
 aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ pr -c2 ./test_1 | grep -v "^$"  
 2014-05-05 19:18           ./test_1           Page 1  
 Hello                  is  
 world                  really  
 This                  awesome  
 aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ pr -c3 ./test_1 | grep -v "^$"  
 2014-05-05 19:18           ./test_1           Page 1  
 Hello            This            really  
 world            is            awesome  

Another example:
news:
 The HTC One M8 must compete head-to-head against its arch-rival's freshest mobile machine, the Samsung Galaxy S5, and compete it does. Aesthetically speaking, the HTC One M8 runs rings around the GS5 with its elegant and intuitive Sense interface. What's more, though the M8 may lack every bell and whistle that Samsung packs into its phones, it matches the GS5 on almost every feature that really matters, from processing speed to user interface. Only the camera and its ever-so-slightly muddy shots give me pause.  
 Until the Samsung GS5 reviews are out, I can't say with certainty that the One M8 is a better phone or even if its the best Android alternative to the iPhone 5S. But I can say that the One M8 is a truly great phone all on its own--one worthy of anyone's investment.   

terminal:
Now we have 2 long lines in our news file, we know each line is a paragraph, because it doesn't have newline inside, from technical perspective, each paragraph is just one line.

Here is what happens: we only see "part" of each line. pr is just trying to pick up each line, and then put each line in different columns, if its length exceeds the limit, the line is just truncated.
 aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ pr -c3 ./test_1 | grep -v "^$"  
 2014-05-05 19:26           ./test_1           Page 1  
 The HTC One M8 must com             Until the Samsung GS5 r  

2. pr: suppress page headers
test_1:
 Hello  
 world  
 This  
 is   
 awesome  

terminal:
with -t option, the header just disappear
 aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ pr -t ./test_1 | grep -v "^$"  
 Hello  
 world  
 This  
 is   
 awesome  

No comments:

Post a Comment