URGENT!!!! Anyone knows how to programme in linux?
I have an assignment that's due on Friday, which ask me to write a programme in linux to format the original source code. I think of it for so long, anyone can help me with it?
Assignment: Write a bash program to format the comments in an assembly language source file. Conditions: Comments begin with a ; (semicolon) and end with a newline. The source file will contain only printing characters, spaces, and newlines. It is not necessary to check for invalid input. Requirements: Lines without comments should not be changed. Entire lines which are comments (begin with ;) should not be changed. Lines containing both code and comments (; preceded by other characters) should have spaces inserted before the comments so all comments begin in the same column, at least 4 columns after the end of the longest code. Be sure your program includes all of the following: comments with your name, the date, and the assignment comments or output with instructions for using the program prompts for any interactive inputs descriptive names and/or comments explaining variables & functions indentation of code blocks comments explaining any non-obvious control flow The following is the code before and after. We should write a programme which reads a file contains the original code, and turn the original one to the formatted one, then output it on the screen. Please someone help me with it! Thank you! Source Code: http://50.79.210.49/171/171-asm-14f.xhtml |
Sure, show us what you've done so far, except procrastinate. ;)
|
I am having difficulties with calculating each line's length as it reads through, but it must read just up to the semicolon (;), then add the differences between the longest line and the rest to all lines before the semicolon, so to do the format. This is what I have done up to now:
#!/bin/bash echo "Start!" echo "Please enter a file name: " read name cat $name| while read line; do |
Sounds like a job for sed and awk.
The sed and awk chapters in this free book are good: http://linuxcommand.org/tlcl.php String manipulation is obviously also useful: http://www.faqs.org/docs/abs/HTML/st...ipulation.html |
I am confuse of how to use those code. I tried it for hours and all got errors
|
Can someone help? I am confuse with the codes
|
What did you try that you thought would work but gave you errors?
|
I was trying to cut the lines after the semicolon, and those lines start at the seconde line. The first line is just semicolon, and end with a new line, so all of the contents got deleted.
|
I was trying to do display the content that are not begin with a ;(semicolon), after the first line.
I type this: echo "$line" | grep _C 1 '^[^[;]]' it doesn't work. WHY? |
Your regex is wrong.
Code:
bash-4.1$ line="a |
What is this mean?
|
As it is a bash assignment, I see no direct need to use other tools (not to say you couldn't)
So a few points of housekeeping first, seeing you are new: 1. Please read the LQ rules. Here you will see that no one will do your homework but we are happy to assist 2. The question is only urgent to you and as this is a community site it will not tend to yield you any quicker results 3. Please place all code and data in [code][/code] tags to maintain formatting and make it easier to read So, on to your question. Like most first time programmers you are jumping in and have as yet not defined what you need your programme to do. Having looked at the before and after code and reading the brief my thoughts would be: 1. Ignore all lines starting with a semi-colon 2. First pass of the file is needed to find what position is the greatest when it comes to semi-colons after code 3. Second pass would utilise number found in 2 to set all semi-colons after code, ie. add spaces prior to any that do not have semi-colon at desired position So see if that helps give you some direction |
How can I find the longest line before the semicolon? I tried use length but it just gives me the number, and I can't ignore the ;
|
I just tried some new things, but still it can't process what I needed. I define the max as zero already.
[code] if length "$line" | grep '^[^;]' > max max=length "$line" | grep '^[^;]' [code] |
Quote:
and there "SHOULD" be something like this in a end of the chapter exercise of that text book remember software is NOT !!!! "route memorization " it is DEDUCTIVE and symbolic logic oops i let the cat out of the bag-- it is MATH!!! How would "Sherlock Homes" solve a problem ? then the logical steps that Sherlock used to get from point A to point D ? Quote:
in software if you OPEN something you need to CLOSE that something PHP Code:
Code:
if length "$line" | grep '^[^;]' > max |
All times are GMT -5. The time now is 10:44 PM. |