LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-02-2014, 06:50 PM   #1
jeffrey1289
LQ Newbie
 
Registered: Dec 2014
Posts: 10

Rep: Reputation: Disabled
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

Last edited by jeffrey1289; 12-02-2014 at 06:51 PM.
 
Old 12-02-2014, 06:55 PM   #2
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,158
Blog Entries: 10

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
Sure, show us what you've done so far, except procrastinate.
 
Old 12-02-2014, 06:58 PM   #3
jeffrey1289
LQ Newbie
 
Registered: Dec 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-02-2014, 06:59 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 7,002

Rep: Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520
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

Last edited by dugan; 12-02-2014 at 07:04 PM.
 
Old 12-02-2014, 07:05 PM   #5
jeffrey1289
LQ Newbie
 
Registered: Dec 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
I am confuse of how to use those code. I tried it for hours and all got errors
 
Old 12-02-2014, 07:29 PM   #6
jeffrey1289
LQ Newbie
 
Registered: Dec 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
Can someone help? I am confuse with the codes
 
Old 12-02-2014, 07:31 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 7,002

Rep: Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520
What did you try that you thought would work but gave you errors?
 
Old 12-02-2014, 07:35 PM   #8
jeffrey1289
LQ Newbie
 
Registered: Dec 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
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.
 
Old 12-02-2014, 07:50 PM   #9
jeffrey1289
LQ Newbie
 
Registered: Dec 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
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?
 
Old 12-02-2014, 08:06 PM   #10
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 7,002

Rep: Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520
Your regex is wrong.

Code:
bash-4.1$ line="a                    
;b
c"
bash-4.1$ echo "$line" | grep '^[^;]'
a
c
bash-4.1$

Last edited by dugan; 12-02-2014 at 08:12 PM.
 
Old 12-02-2014, 08:14 PM   #11
jeffrey1289
LQ Newbie
 
Registered: Dec 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
What is this mean?
 
Old 12-02-2014, 08:17 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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
 
1 members found this post helpful.
Old 12-02-2014, 08:29 PM   #13
jeffrey1289
LQ Newbie
 
Registered: Dec 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
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 ;
 
Old 12-02-2014, 09:00 PM   #14
jeffrey1289
LQ Newbie
 
Registered: Dec 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
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]
 
Old 12-02-2014, 09:33 PM   #15
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 16,817

Rep: Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408Reputation: 2408
Quote:
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 ;
what were the examples that were used in the textbook you are using ?
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:
[code]
if length "$line" | grep '^[^;]' > max
max=length "$line" | grep '^[^;]'
[code]
you missed the close tag
in software
if you OPEN something you need to CLOSE that something
PHP Code:
[code]
the copied 
code
line3
line4
[/code
you missed the closing "/" in the last tag
Code:
if length "$line" | grep '^[^;]' > max
max=length "$line" | grep '^[^;]'

Last edited by John VV; 12-02-2014 at 09:37 PM.
 
  


Reply

Tags
code


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off




All times are GMT -5. The time now is 03:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration