Any Linux command output to delimited format. Script should work for any command.
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Any Linux command output to delimited format. Script should work for any command.
I would like to develop a script capable to split my command results into a delimited text. See the examples below and see the desired output. Would be wonderful if the script could work for most of the Linux commands.
command used: $ finger
EXAMPLE 1: The column "Name" has 2 names separated by a blank space
---------
Login Name Tty Idle Login Time Office Office Phone
abcdefg Firstname Secondname *:0 Sep 2 09:58 ABCDE/32th (111)111-1111
abcdefg Firstname Secondname pts/1 1d Sep 2 09:58 (:0)
abcdefg Firstname Secondname *pts/2 Sep 3 15:08 (:0)
abcdefg Firstname Secondname pts/3 6:04 Sep 3 09:03 (:0.0)
abcdefg Firstname Secondname *pts/7 5:08 Sep 3 09:59 (:0)
The s means "substitute". The item between the first forward slash and the second one is the pattern to search for to do substitution on and the item between the second and third is what to substitute with. The final g means do it globally on the line rather than just on the first one it finds.
For the first item the special characters shown:
The \s means "any whitespace" and the \+ means treat successive whitespace all as one (e.g 3 spaces, 2 tabs etc...).
Of course this may have limited use for some commands given that delimiting some things (e.g. a file name that contains a space) will make it misreport what you really want.
Unfortunately there's no general solution, because you have to take into account the semantic (meaning of the fields) and their formatting, furthermore some fields may be empty for some records but not for others and for instance the number of white spaces in a given field can vary.
Often the command has formatting options that can help knowing what character string represents what field, for instance "finger -l".
When (but only when) you know what is the content of each field you can include separators in each record with the usual text processing commands like sed, awk, grep, or tr to name a few, and parameter expansion.
Last edited by Didier Spaier; 09-03-2014 at 02:01 PM.
Which "first sed example"? The one I listed certainly works as I tested it before I sent it to you.
Look at his desired output again. sed is replacing ALL spaces with |, which is not what he wants. He wants each FIELD to be separated by a pipe, not each WORD to be separated by a pipe. Since there's no way to know which spaces are part of a field or between fields, there is no easy solution.
Again, you need to have a reliable way to find fields boundaries to do what you want, and the way output is formatted can vary upon options used, so there can't be a general answer.
Usually "man <command>" gives information about output formatting and its options.
I do not see a one size fits all solution based on the changing format of your data, however, if we compare the first and second sets of data provided and say that the only diff
is the number of names in the second field (Name), you could try looking at something like Perl or Ruby and use the unpack command to delimit fixed width data and then you would have 2 options for the
two examples presented.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.