Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Lists unambigously names of all xml elements used in files in current directory

Terminal - Lists unambigously names of all xml elements used in files in current directory
grep -h -o '<[^/!?][^ >]*' * | sort -u | cut -c2-
2009-06-17 00:22:18
User: thebodzio
Functions: cut grep sort
2
Lists unambigously names of all xml elements used in files in current directory

This set of commands was very convenient for me when I was preparing some xml files for typesetting a book. I wanted to check what styles I had to prepare but coudn't remember all tags that I used. This one saved me from error-prone browsing of all my files. It should be also useful if one tries to process xml files with xsl, when using own xml application.

Alternatives

There are 5 alternatives - vote for the best!

Terminal - Alternatives
grep -Eho '<[a-ZA-Z_][a-zA-Z0-9_-:]*' * | sort -u | cut -c2-
2009-08-05 21:54:29
User: inkel
Functions: cut grep sort
Tags: sort grep cut xml
0

This one will work a little better, the regular expressions it is not 100% accurate for XML parsing but it will suffice any XML valid document for sure.

Know a better way?

If you can do better, submit your command here.

What others think

Just a few small hints ...

1. I think the "-e" option is not needed.

2. Why "$(pwd)/*" and not simply "*"?

3. "sort | uniq" can be abbreviated: "sort -u"

Comment by inof 287 weeks and 4 days ago

Thanks a lot for your advice! You're right in all pointed matters and thanks to you this one-liner became much shorter and elegant. I'm sending you a virtual can/bottle/pint/bathtub (whatever rows your boat) of your favorite beverage :).

Comment by thebodzio 285 weeks and 4 days ago

Actually this will display not only the names but also append any attribute that exists on the element, so it might give you errors. Try running it on a folder with XSLT files.

I'll try to submit a new version of the command if I've time.

Comment by inkel 280 weeks and 4 days ago

True. I've got a TODO note about that with the script.

Comment by thebodzio 277 weeks and 3 days ago

Okee. I've reviewed my entry. It's still quite far from caring about many aspects of xml specification but does the job much better than previous version. First of all it shows only element name not element name _and_ attributes as inkel noticed. It also shows element names that contain characters with diacritic signs (like polish "książka" ? book or "tytuł" ? title). Finally I took "cut -c2-" from inkel's script. @inkel: thanks for your interrest and tip about "cut"! :)

Comment by thebodzio 277 weeks and 3 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts