Sponsored Content
Homework and Emergencies Homework & Coursework Questions [Scripting]Find & replace using user input then replacing text after Post 302372396 by bakunin on Tuesday 17th of November 2009 10:53:28 PM
Old 11-17-2009
I see two fundamental problems with your script:

1. User input has to be validated
You write the user has to enter the name of the part. That raises the question: what happens, if the part s/he enters is not in the database? It is ok, IMHO, if you say that this is beyond the scope of the homework or that false input can be silently ignored or whatever - it is just a fundamental truth that any input users provide should be validated somehow and you dhould have an answer to this question. If it is the correct data type (strings, digits, ...), if it is "well-formed" in some regard (for instance an IP address looks always the same - 4 numbers, 0-255, divided by 3 dots, etc.), and so on. If your program makes some assumption about which data it will be presented then check if indeed this data is what you have before you use it.

2. The sed-script
There are three basic forms of sed commands:

Code:
s/<expr1>/<expr2>/

/<expr>/ {
       command1
       command2
       ....
     }


/<expr1>/,/<expr2>/ {
       command1
       command2
       ....
     }

The first one is the most simple one: it applies the single command to replace <expr1> with <expr2> to every line. It might be that <expr1> is not part the line and the command will do nothing in this case, but still it is applied to the line (this is necessary to understand how sed works).

The second one is a more complex form: it will successively apply command1, then command2, etc. to lines which contain <expr1>. Of course the "list of commands" could also consist of only one command, in which case you can omit the curly braces and write:

Code:
/expr/command

An example: The following script will change only the lines that start with "abc". The change will be to first replace the "c" in "abc" to "<command1>", the second change will be to replace the line end with "<command2>", which effectively appends this to the end of the line.

Code:
sed '/^abc/ {
          s/c/<command1>/
          s/$/<command2>/
     }' in > out

file in:
========
abc will be changed
abd will not be changed
xxx next line will be changed, but this will not
abc

file out:
========
ab<command1> will be changed<command2>
abd will not be changed
xxx next line will be changed, but this will not
ab<command1><command2>

The third and last form ist the most complex one: it will apply a (list of) command(s) to a range of lines. It will start applying the list of commands when a line containing <expr1> is encountered and will do so on any subsequent lines untl <expr2> is found in a line. Then the commands are not applied to any line until <expr1> is found again, etc.. Similar to the second variant you can replace the list of commands by a single command where you can omit the curly braces:

Code:
/expr1/expr2/ommand

See if you can solve your problem now.

I hope this helps.

bakunin
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replacing part of a text file with user input.

Ok, I am brand new to UNIX and I am trying to learn a cross between basic script and database use. I had got some ideas off the net on simple ideas for learning UNIX. I am working on creating a simple phone book program that allows myself to enter our employees from work into a phone book text... (0 Replies)
Discussion started by: georgefurbee
0 Replies

2. Shell Programming and Scripting

find & replace with user input

Trying to create a script/executable to replace "abc" text string in "myfile.htm" with input from a pop-up field. For example, launch this thing and a prompt is popped up asking the user to input what "abc" should be replaced with, then it inserts what the user inputs in place of abc in the... (3 Replies)
Discussion started by: mike909
3 Replies

3. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

4. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

5. Shell Programming and Scripting

Search & Replacing text within a file

Hi all! I'm a newbie and I'm writing a script which will ask a user for data to search. I will then search for this data using grep and displaying this data back to the screen for the user to see. The user will then enter new data to replace the data searched. Now how do I replace this data... (4 Replies)
Discussion started by: macastor
4 Replies

6. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

7. Shell Programming and Scripting

Awk replacing file with user input

this section of the awk code i have here takes file to work with from the user. the user specifies the file name from the command line and the file name is assigned to the variable $FLIST awk 'BEGIN { while((getline < "'${FLIST}'")>0) S FS="\n"; RS="}\n" } now, i dont want... (5 Replies)
Discussion started by: SkySmart
5 Replies

8. Shell Programming and Scripting

Finding a text in files & replacing it with unique strings

Hallo Everyone. I have to admit I'm shell scripting illiterate . I need to find certain strings in several text files and replace each of the string by unique & corresponding text. I prepared a csv file with 3 columns: <filename>;<old_pattern>;<new_pattern> ... (5 Replies)
Discussion started by: gordom
5 Replies

9. Shell Programming and Scripting

How-To Check & Filter user input

Hi, On my Java webpage which invokes the shell script has two checkboxes viz ... apache and weblogic apache require one parameter i.e apache home from the user while Weblogic requires three or five params from the user vi.z weblogic_home or <jdk_home, config_home & pid>, username and... (4 Replies)
Discussion started by: mohtashims
4 Replies

10. Programming

find & Replace text using two non-unique delimiters.

I can find and replace text when the delimiters are unique. What I cannot do is replace text using two NON-unique delimiters: Ex., "This html code <text blah >contains <garbage blah blah >. All tags must go,<text > but some must be replaced with <garbage blah blah > without erasing other... (5 Replies)
Discussion started by: bedtime
5 Replies
All times are GMT -4. The time now is 10:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy