prompt to delete each record when pattern is found
Hello!. I am working on a very simple program and I have been trying different things. This is so far what I have done and there is one small detail that still does not work. It finds all the records in a phonebook per say:
The problem is that it only prompts to remove the last record and skips the first record. BUT if I type "y" then it will remove both records from the phonebook file, if I type "n" then it removes none. Here is my program so far.
Does anybody would be so kind to tell me what is wrong with my program? I appreciate any help. Thank you, thank you.
Last edited by Scott; 11-27-2010 at 05:37 AM..
Reason: Code tags
Thank you very much for your help Mr. Scrutinizer. Your second comment was very helpful, I made the change and that works perfect. I would like to explain my idea behind the line that you mentioned.
if [ $ans != n ] the variable $ans is the answer from the user (y/n) it suppose to enter the loop and prompt for every pattern found. Which means that IF the user types 'y' THEN it will proceed to remove the record #1, BUT, IF $ans = n THEN it will go to the line prinf "Record will not be removed" BUT still the program will go to the second pattern found IF any, and prompt me to remove the record going through the same process. I still cannot make it do that. Please that is the only detail. Thanks.
---------- Post updated at 05:59 PM ---------- Previous update was at 04:59 PM ----------
Here I wanted to put it this way for simplicity.
rnames=$(grep Susan phonebook | awk '{print $1" " $2}')
for i in "$rnames"; do echo "$i remove? (y/n)"; done
Susan1 banana
Susan2 apple remove? (y/n)
Stills puts the remove? question only at the end. What do i need to do to prompt for the first record?
Last edited by bartsimpsong; 11-27-2010 at 09:06 PM..
The variable i is being specified as the variable in the for loop, but what is used inside the loop in the grep -v statement the variable name .
Further, you need to use for i in $names without the double quotes (otherwise it will loop only once for the whole string). But you would need to change the IFS variable in this case, since you are using multiple words for matching and you want to only use the newline character as the field separator.
Another option would be to use:
I would use:
so that only the letter y will lead to the deletion and not any character but n.
To allow case independence and multiple possible answers you could use this:
One more difficulty is that you are using:
names=$(grep Susan phonebook | awk '{print $1" " $2}')
So that there is only a single space between $1 and $2, even if there were for instance two spaces in the input file. If you then later would like to delete the line based on these two variables, grep will no longer find them
For example:
Last edited by Scrutinizer; 11-28-2010 at 02:40 AM..
Hi,
i have another problem. I have been trying to solve it by myself but failed.
inputfile
;;
ID T08578
NAME T08578
SBASE 30696
EBASE 32083
TYPE P
func just test
func chronology
func cholesterol
func null
INT 30765-37333
INT 37154-37318
Link 5546
Link 8142 (4 Replies)
I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :)
I have the output of an ldapsearch that looks like this:
dn: cn=sam,ou=company,o=com
uidNumber: 7174
gidNumber: 49563
homeDirectory: /home/sam
loginshell: /bin/bash
uid: sam... (2 Replies)
I have two files
File1
====
1|2000-00-00|2010-02-02||
2| 00:00:00|2012-02-24||
3|2000-00-00|2011-02-02||
File2
====
2000-00-00
00:00:00
I want the delete the patterns which are found in file 2 from file 1,
Expected output:
File1
==== (5 Replies)
Hi,
I have a input file with the following entries:
1one
2two
3three
1four
2five
3six
1seven
1eight
1nine
2ten
The output should be
1one
2two
3three
1four
2five
3six (2 Replies)
I thought that this was going to be quit simple using sed but i wasn't able to find a way to delete the second line of a text file if my pattern was not found in the line
With awk i am completly useless :rolleyes:
Any ideas? (2 Replies)
I am trying to do some thing like this ..
In a file , if pattern found insert new pattern at the begining of the line containing the pattern.
example:
in a file I have this.
gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin
if i find feedthru_pin want to insert !! at the... (7 Replies)
Hi,
I have file 1.txt with following entries as shown:
0152364|134444|10.20.30.40|015236433
0233654|122555|10.20.30.50|023365433
**
**
**
In file 2.txt I have the following entries as shown:
0152364|134444|10.20.30.40|015236433
0233654|122555|10.20.30.50|023365433... (4 Replies)
Hi,
I have two files viz,
rak1:
$ cat rak1
rak2:
$ cat rak2
sdiff rak1 rak2 returns:
I want the lines that got modified, changed, or deleted preceding with the section they are in.
I have done this so far: (1 Reply)
I am working with the Oracle 10.2.0.3 job scheduler on Solaris 10, and unfortunately, the scheduler executes scripts in such a way that several default shell environment variables are not defined. For example, $HOME, $USER, and $LOGNAME are missing.
How can I parse the appropriate record in... (7 Replies)
I have a file which contains blocks of text - each block is a multi-lines text delimited by blank lines eg.
<blank line>
several lines of text
...
pattern found on this line
several more lines of text
...
<blank line>
How do you delete the block of text (including the blank lines) when... (17 Replies)