Help with file stripping shell script

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Help with file stripping shell script
# 1  
Old 12-08-2010
Help with file stripping shell script

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 #.

Code:
 
if [ $# -eq 0 ]
then
  echo "Usage: $0 needs an argument. "
elif [ $# -eq 1 ]
then
  if [ -d "$1" ]
  then
    echo "\"$1\" is not a file. This is a directory."
  elif [ -f "$1" ]
  then
    cat $1 | grep -v "^#" > $1-strip.bash
    echo Showing the stripped file ... ; sleep 2
    cat $1-strip.bash | less
  fi
fi

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
# 2  
Old 12-08-2010
use

Code:
grep -v "^[ ^I]*#" $1 > $1-strip.bash          (where ^I represents tab and escape # character if required)

instead of

Code:
cat $1 | grep -v "^#" > $1-strip.bash

This User Gave Thanks to For This Post:
R0H0N
# 3  
Old 12-08-2010
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 ?
# 4  
Old 12-08-2010
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

Code:
#!/bin/awk -f
{ for (i=1;i<=length($0);i++) {
  C=substr($0,i,1);
  if (!SQuote && !DQuote && C=="#") Comm=1;
  if (Comm) continue;
  printf C;
  ...  Set SQuote DQoute here to indicate in quotes also handle '\' character ...
  }
  Comm=0;
  printf "\n";
}

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:
# 5  
Old 12-08-2010
Chubler_XL thank you very much for the input.

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.
# 6  
Old 12-09-2010
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 #

You probably just need something like this:

Code:
grep -E "^[[:blank:]]*#" MyBashScript.sh

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Shell Scrip in Masking particular columns in .csv file or .txt file using shell script

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)
Discussion started by: Mahesh G
5 Replies

2. Shell Programming and Scripting

Stripping ret of the lines in a file (sed question)

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)
Discussion started by: Hinnerk2005
2 Replies

3. Shell Programming and Scripting

Stripping characters from a file and reformatting according to another one

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)
Discussion started by: TheTransporter
2 Replies

4. Shell Programming and Scripting

Bash script - stripping away characters that can't be used in filenames

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)
Discussion started by: mglenney
5 Replies

5. Shell Programming and Scripting

how to read dbf file in shell script and to convert dbf file usinf shell script

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)
Discussion started by: gauara
6 Replies

6. Shell Programming and Scripting

Stripping out extensions when file has multiple dots in name

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)
Discussion started by: Nemelis
8 Replies

7. Shell Programming and Scripting

Stripping out extension in file name

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)
Discussion started by: devs
9 Replies

8. Shell Programming and Scripting

Stripping out the extension of a file name

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)
Discussion started by: ramky79
4 Replies

9. Shell Programming and Scripting

Stripping out the extension of a file name

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)
Discussion started by: ramky79
2 Replies

10. UNIX for Dummies Questions & Answers

stripping last lien off a file

Hi, how can i strip the last line off my file using shell script? Thanks and Regards Vivek.S (3 Replies)
Discussion started by: vivekshankar
3 Replies
Login or Register to Ask a Question