Well my last assignment came and i tried some things but i guess it's pretty puzzling for me to figure out all the points of the problem. I was able to achieve something but it's not complete.
1. The problem statement, all variables and given/known data:
Write a bash program which strips the comments from a bash source file:
a) Input the name of the bash source file as a command line argument, or
Input lines of bash source from standard input (your choice).
b) Remove single-line comments beginning with #
(# inside ' ' or inside " " or after \ does not begin a comment)
c) Output the stripped source file to a disk file with the string -strip
inserted between the basename and the extension.
(e.g. input file foo.bash should produce output file foo-strip.bash), or
Output the stripped lines of bash source to standard output (your choice).
It is not necessary to check if the source file is readable or is a valid bash program.
2. Relevant commands, code, scripts, algorithms:
In terms of commands i believe either sed, grep or awk.
3. The attempts at a solution (include all code and scripts):
I was able to create a script that strips the file of comments that at the beginning of a line they start with #. here is the script. However that doesn't fulfill the requirements of the problem.
#This script removes single-line comments begining with #.
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
diablo valley college, ca, usa, stuart fogg, 171 intro to unix
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
Last edited by Scott; 12-08-2010 at 04:21 AM..
Reason: Added code tags and indentation for readability
I have tried that command but here is what happens. it strips the comment from the line when it starts with # but also if you have a command and then you have # than it will delete the whole line including the command. that's not good for the file.
What this script should do is eliminating comments that are also in the middle of a command keeping the command and cutting the # and whatever is after it.
also should ignore # if it is quoted or escaped like ' ... # ... " " ... # ..." \# ...
he was saying about using a lot of ifs statements for this but i couldn't think of what else to use for the if statements ... like sed or still grep ?
Trick here is to process the file 1 character at a time. Use variables to keep track of being in a single string, double string or comment. Awk probably makes it the easiest
Try something like this
Remember that if you're in a double quote string, single quotes don't apply (eg A="It's Fun"), and vica-verca.
Last edited by Chubler_XL; 12-08-2010 at 09:25 PM..
This User Gave Thanks to Chubler_XL For This Post:
But i have a question for you ? do you think that this type of problem is for a introduction to UNIX class or is more for an advanced in unix? The reason i am asking is that I would love to be able to make it work using an awk format, but the code that you posted it is too advanced to even understand its logic, for me right now. Again thanks for the effort.
Yes it is too advanced for a intro course. The requirement is probably only lines that begin with comments and ignore strings that span multiple lines and may contain #
Hello Unix Shell Script Experts,
I have a script that would mask the columns in .csv file or .txt file.
First the script will untar the .zip files from Archive folder and processes into work folder and finally pushes the masked .csv files into Feed folder.
Two parameters are passed
... (5 Replies)
Hi all,
I didn't use SED for 20 years and was never an expert. So my current knowledge is about zero. Please be patient with me. I'm neither a native speaker.
I have a huge dictionary file and want the rest of the lines stripped. Everything after (and including) the "/" should be stripped. I... (2 Replies)
Dear experts,
my problem is pretty tricky.
I want to change a file (see attached input.txt), according to another file (help.txt). The output that is desired is in output.txt. The example is attached.
Note that
-dashes should not be treated specially, they are considered normal characters,... (2 Replies)
I want to create a temp file which is named based on a search string. The search string may contain spaces or characters that aren't supposed to be used in filenames so I want to strip those out.
My thought was to use 'tr' with but the result is the opposite of what I want:
$ echo "test... (5 Replies)
Hi all,
I am new to shell scripting.
I have dbf file and I need to convert it into csv file.
OR, can i read the fields from a .dbf file and OR seprate the records in dbf file and put into .csv or txt.
Actually in the .dbf files I am getting , the numbers of fields may vary in very record and... (6 Replies)
I posted this already in another thread, but was told that I should create a seperate thread for the following question:
How do I strip the extension when the delimiter might occur multiple times in the filename?
For example:
I have 2 files as input for my script.
test.extension... (8 Replies)
This command gives me just the filename without any extension:
evrvar =`echo filename.tar | sed 's/\.*$//'`
I am trying to make a change to this command... to make it work for... filename.tar.gz to get just the filename....
currently the command gives me filename.tar by removing only gz... I... (9 Replies)
I have written a shell script and in my script i have a variable
filename=myfile.txt
now, i want another variable to be defined for which i have to strip out the extension fo the file name,
i.e. newvariable= myfile
how do i strip out the ".txt" part from my first variable.
Any kind of help... (4 Replies)
I have two variables to be dynamically defined in my shell script
variable1= myfile.txt
variable2= myfile
The second variable depends on the first ( i mean , it is a part of the first variable) Now, I need to strip out the ".txt" part from the first variable how do i do that in a shell script. (2 Replies)