Shell Programming Blog Shell Programming Blog

Tuesday 23 October 2012

Linux Bash Scripting: How to modify the ls command output using pr command

Unknown | 11:29 | |

I have received the below question in our "Ask a question" section of this blog.
Q: Using set -A write a script to print the output of the ls command in 5 columns with two spaces between each column. Pretend that ls does not have multi column output.

Normally we will get a multi column output if we use the command ls -A as shown below.


To print the output of the ls command in five columns with two spaces between each column. You can use the following command.


The pr utility is a printing and pagination filter for text files.

From the man page,

-COLUMN, --columns=COLUMN
output COLUMN columns and print columns down, unless -a is used. Balance number of lines in the columns on each page.

-a, --across
print columns across rather than down, used together with -COLUMN 

-SSTRING, --sep-string[=STRING]
separate columns by STRING, without -S: Default separator <TAB> with -J and <space> otherwise (same as -S" "), no effect on column options

-T, --omit-pagination
omit page headers and trailers, eliminate any pagination by form feeds set in input files

The output of the command would be,


suppose if you want to have delimiter as :::: instead of spaces, then you can change the delimiter in the following command.


you can see the difference in the order in which the files are displayed from the above two commands. That is because I purposely did not use -a option to show the difference.

To pretend that ls does not have a multi column output, you need to add the above command as the alias to the ls command as below.


After adding alias, if you use the ls command, you will get the desired output.

0 Comments:

   

Post a Comment

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
3. Please do not spam, spam comments will be deleted immediately upon my review.

Regards,
Mohamed Abubakar Sittik A

 

Shell Programming Copyright © 2012 Shell Programming theme is Designed by Abusittik, Shell Programming