- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Limiting characters upto <xyz> columns
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2007 05:34 PM
10-17-2007 05:34 PM
Limiting characters upto <xyz> columns
Recently i did some code changes in one of the text file.
During the code review,i've been asked to allign the comments
in this file to XYZ columns(say XYZ=40).
Now the problem is that this file is a huge one and
it would be really pathetic if i go ahead and do it manually.
I think there must be a better way of doing it,Can it be done
through a shell script ?
Can this variable(COLUMNS) is used for doing the same?
hpia14:/>set|grep -i columns
COLUMNS=155
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2007 07:10 PM
10-17-2007 07:10 PM
Re: Limiting characters upto <xyz> columns
To cut all lines to specified maximum length, you might do something like:
cut -c 1-40
This cuts off all lines after the 40th character.
But I'm not sure whether this is really what "align comments to XYZ columns" means. I'd think it would mean moving all comments towards the right to _begin_ at the requested column. A task like this would be called "pretty-printing" or "beautifying" your source code.
For this, you need a program that understands enough of your text file to identify what is a comment and what isn't.
Since you did not specify the programming language (or whatever) your text file is in, it's difficult to find a suitable program for you.
For example, a common tool for C programming language would be "indent". To move all comments to column 40 (or further, if there is more than 40 characters on the left side of the comment) could be done with:
indent -c40 -o newfile.c oldfile.c
Indent is available in the HP-UX Porting Archive:
http://hpux.asknet.de/hppd/hpux/Gnu/indent-2.2.9/
You might want to Google for a suitable
program for your programming language. Good search words are the name of your programming language, "prettyprint" and/or "beautifier".
MK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2007 07:41 PM
10-17-2007 07:41 PM
Re: Limiting characters upto <xyz> columns
My text file contains shell scripts.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2007 01:35 AM
10-18-2007 01:35 AM
Re: Limiting characters upto <xyz> columns
However, none of these are suitable for shell scripts. As you mentioned, there are comments that are very long (an error by the script writer) and if you simply break the line at column 40, the next line will not have the requisite # at the beginning of the line. So the script will fail as it tries to run the newline of comments.
But even more difficult to handle will be command lines that are too long and must be split with a special characters (\) to escape the special meaning of the end-of-line. This is a very common situation when a programmer uses the tab character. 8 characters is a massive waste of space on the line. I typically use and indent of 3, sometimes just 2 characters for deep indentation. I am always aware that 80 characters is the industry default screen width so i don't write code beyond that width. Instead, I will break up lines using \ and especially use a line break for every command in a long pipe.
COLUMNS=155 sound like you are using Xwindows and have fallen into the trap of instantly resizable terminal screens. It is nice to drag a new character space with one click but as you have seen, code becomes quite difficult to manage and most certainly becomes a pain to print and distribute for code reviews. You can handle this in the future by using a restricted screen size for your editing window, say COLUMNS=40 (LINES can be any length). Use a separate wide screen for debug and testing.
Sorry, but every long line will have to be processed by hand. NOTE: this will be highly invasive!!! That means that just getting all the lines to fit in 40 columns will require a lot of new lines that are correctly formatted, comments with # and line breaks with \ at meaningful locations. 40 characters is quite unreasonable for scripts. Now if this restriction only applies to comments, the task gets a little easier and less invasive to the code. 40 sounds like a limit imposed so the text will be visible in the back of the room during a code review -- not a reasonable request considering the possibility of creating mistake in the code. Instead, I would download the Microsoft magnfier called ZoomIt:
http://www.microsoft.com/technet/sysinternals/utilities/zoomit.mspx
Now you can zoom in on a few lines and read the text quite easily at the back of the room.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2007 02:16 AM
10-18-2007 02:16 AM
Re: Limiting characters upto <xyz> columns
Are those comments to (structured) 'code' or (free format) random 'text'?
As Bill indicates, this may be rather tricky, sacrificing the actual intend for the purpose of readability.
You would have to very clearly define what a comment looks like, how a script can recognize a comment without getting confused by text data which just looks like a comment.
You would have to very clearly identify what to do with lines exceeding the comment start location: Split brute force? Truncate? Recognize words boundaries? Spacing? Ignore? Break the aligment rule?
As Bill implies... what problem are you really trying to solve?
- a presentation problem?
- a permanent record
- a temporary aid?
If you want to pursue automting this, and would appreciate further help, then please reply with a (text!) attachment showing a relevant sample input and desired output.
And you may also want to indicate which tools are at your disposal (shell? perl? awk? python?...)
Hope this helps some,
Hein.