Sponsored Content
Homework and Emergencies Homework & Coursework Questions Bash Scripting - sed (substitue) Post 302524065 by bakunin on Saturday 21st of May 2011 05:37:15 AM
Old 05-21-2011
This still leaves you with the problem of writing your script. Let us restart from the beginning:

When you say you want to "remove comments" you have to know what such a comment constitutes: there must be a sequence of characters which start a comment and a sequence, which ends a comment. Consider the following (phantasy-) shell code:

Code:
#!/bin/myshell

some_command    # this is a comment

# this is a comment too
Is this still a comment?

other_command

Lets establish what starts a comment: the "#" sign obviously. And what ends it? The line end (which answers the question if the line 6 is a comment - it is not, because it is after the end of comment-end-marker).

Still there are some possible problems: What about comments in comments? Lines like this:

Code:
command  # comment # what status has this?

Or what about using the comment-start sequence quoted:

Code:
echo "something # is this a comment?"  # what about this?

or escaped:

Code:
echo "something" \# comment?


There is a certain class of programs designed to deal with these problems. They are called "parsers". These programs constitute the first part of a compiler, where the program code gets read, stripped of everything unnecessary (like comments, which the program doesn't need) and is syntax-checked. It all sounds quite complicated and "building compilers" sounds quite like high-level stuff, but this is astonishingly easy.

Lets take stock: we have a set of rules (what starts a comment, what ends a comment) and we have an action (filter the comments out). We saw above, that we need some more rules regarding the quoting and the escaping, let's go over the rules again:
  1. Do not look at characters inside "..." or '....' (quoting)
  2. If a character "\" is encountered treat the one following it as a normal character, regardless of it's usual meanings (escaping)
  3. When you encounter a comment-start-marker throw away it and the text you read until you encounter a comment-end-marker
  4. Output what you have read

Parsers do their work the following way: read in one character after the other. After each character decide if you have found a sequence with a rule attached to it. Finally, after applying all the applicable rules, output the result (if some rule doesn't forbid it).

In fact parsers are just while-loops reading one character after the other and long case-constructs, trying to apply one rule after the other to the character read. It is clear that we will have to maintain some status-flags when we are only to look at one character at a time. We will have to "remember" if we are inside a comment, inside a quoted string, etc..

How about you trying to write a program (in pseudo-code only, just the logic) for your problem and posting it. Then we will go over your solution and implement that in real code.

I hope this helps.

bakunin
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need help for VNS substitue solution.....

Hello Unix Gurus, We are doing large system upgrade. We expect upgrade to last 180-200 hours. The servers are located remotely. I am looking for solution which allows me to reconnect to the same session active on unix server where I launched the process. This would protect from local client... (0 Replies)
Discussion started by: mehtasa
0 Replies

2. Shell Programming and Scripting

Need to substitue space with \n

I have a file with a single line in it as below. field1 field2 field3 Different fields separated by spaces. I need the output as below. field1 field2 field3 Any sed/awk solution you can suggest? (6 Replies)
Discussion started by: krishmaths
6 Replies

3. Shell Programming and Scripting

substitue of values.

$db2 connection ...........Q a=`$db2 -x "select A from tablename where z in (select z from tablename Q where condition fetch first 1 rows only ) with ur"` b=`$db2 -x "select B from tablename where z in (select z from tablename Q where condition fetch first 1 rows only) with ur"` $db2... (2 Replies)
Discussion started by: rollthecoin
2 Replies

4. Shell Programming and Scripting

sed command - substitue first instance

hi i have one file where i want to substitute only first instance of swap with swap1 i want to replcae only first instance of swap in my script i know we can do this with awk. but i need to do this with sed only i tried follwoing code sed 's/swap/swap1' filename but here all... (15 Replies)
Discussion started by: d_swapneel14
15 Replies

5. Shell Programming and Scripting

please help with Bash Scripting????

Hi, can anyone help me with my scrip please. I wanted do following tasks: 1. List all the directory 2. A STDIN to ask user to enter a directory name from listed directories 3. command to check if the directory exists( or a command to validate if the user entered a valid directory name) ... (2 Replies)
Discussion started by: eminjan
2 Replies

6. UNIX for Dummies Questions & Answers

Substitue 'Special Characters' in VI

Hi All, I am using LATEX and need to delete all the lines in a file matching: \begin{work} I know there are several ways to do this, but I am trying to do it with the substitute command in VI. The problem is I can't get substitute to recognize the character '\'! How do I do it? ... (7 Replies)
Discussion started by: ScKaSx
7 Replies

7. Shell Programming and Scripting

bash scripting help

have this code but when i run it i get this error ./pulse: line 2: and here is the code #!/bin/bash if ; then pulseaudio -k; fi what am i doing wrong thanks Adam (5 Replies)
Discussion started by: ab52
5 Replies

8. Homework & Coursework Questions

Bash Scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Try running 'phone4 xyz' and see what happens. Modify your program so that if no matching name is found, an... (1 Reply)
Discussion started by: OmgHaxor
1 Replies

9. Shell Programming and Scripting

Sort, sed, and zero padding date column csv bash scripting

Hello people, I am having problem to sort, sed and zero padding of column in csv file. 7th column only. Input of csv file: 1,2,3,4,5,6,4/1/2010 12:00 AM,8 1,2,3,4,5,6,3/11/2010 9:39 AM,8 1,2,3,4,5,6,5/12/2011 3:43 PM,8 1,2,3,4,5,6,12/20/2009 7:23 PM,8 Output:... (5 Replies)
Discussion started by: sean1357
5 Replies

10. Shell Programming and Scripting

sed substitue whole file + 1 substitue with variables on one sed line?.

I'm trying to remove '--X' from the whole file and using variables replace $oldvar with $newvar. I have tried with double quotes but it doesn't seem to work. $newvar is set to /usr/bin/bash. Would appreciate some guidance. newvar=$(which bash) oldvar=/bin/bash sed... (1 Reply)
Discussion started by: itman73
1 Replies
All times are GMT -4. The time now is 07:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy