help with finding text and deleting line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with finding text and deleting line
# 1  
Old 07-15-2010
help with finding text and deleting line

HI All, I need to search for a particular pattern input by the user in order to delete the line. My username.txt has
Code:
username@email.com:John:149.0.3.4:1
username1@email.com:Harry:149.0.3.4:1
username1@email.net:Alex:149.0.3.4:1
username1@email.edu:Nemo:149.0.3.4:1

The program i written
Code:
#!/bin/bash

echo " Insert email"
read a
echo "Insert name"
read b

if i input username@email.com and John, the output should be
Code:
username1@email.com:Harry:149.0.3.4:1
username1@email.net:Alex:149.0.3.4:1
username1@email.edu:Nemo:149.0.3.4:1

I tried sed, but it only reads from a defined variable. Any help would be appreciated, thanks
# 2  
Old 07-15-2010
Hi,
Try this,
Code:
#!/bin/sh

echo -n "Enter email:-"
read a
echo -n "Enter name:-"
read b

c="$a:$b"

grep -v "$c" username.txt

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 07-15-2010
Or:
Code:
awk -F: 'BEGIN{
  printf("Enter the email and username :")
  getline ans < "-"
  split(ans,a," ")
}
!(a[1]==$1 && a[2]==$2)
' username.txt

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 07-15-2010
Hi, That worked, thanks!

I have 3 questions
Question 1 how can i set up such a way that when i input a,b, it only prints out that line ( kinda like search function)

e.g
username.txt
Code:
username@email.com:John:149.0.3.4:1
username1@email.com:Harry:149.0.3.4:1
username1@email.net:Alex:149.0.3.4:1
username1@email.edu:Nemo:149.0.3.4:1

if i input username@email.com as a, John as b
Output
Code:
username@email.com:John:149.0.3.4:1

Question 2 How do i only modify the value specified by the user?
e.g
Code:
#!/bin/bash


echo -n "Please enter email u which to modify:"
read a
echo -n "Please enter his name:-"
read b
echo -n "Enter new email:-"
read c
echo -n "Enter new name:-"
read d
echo -n "Enter new IP address:-"
read e
etc.. etc..

if a =username@email.com, b = John, c = Hello@yahoo.com, d = Lala, e =127.0.0.1
Output
Code:
Hello@yahoo.com:Lala:127.0.0.1:1
username1@email.com:Harry:149.0.3.4:1
username1@email.net:Alex:149.0.3.4:1
username1@email.edu:Nemo:149.0.3.4:1

Advanced Feature , how can i set up in such a way that if i input non characters(like numerals or weird symbols like !@#) for the input a and b, it returns a error message?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting all the N lines after the line in which text is found.

Hi! I want to delete N (say 10) lines after the line which text is found in a file "A".Also to delete the line in which the text is found. Only one occurrence of the search string in the file "A" The text to be deleted is in another text file "B". All the search texts in the file "B" are in... (3 Replies)
Discussion started by: shahid1632
3 Replies

2. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

3. Shell Programming and Scripting

Deleting text start at 2nd line....

How can I delete the first '2' characters in the 2nd line and every fourth line thereafter (Means line no.s 2, 6, 10...so on). For example if the following is my Input treieieiei trrpepepepep tyeueueue tyeueueeu tyeyeyeye tryryry tyeyey tyrtyty tytyty tututu tututututu Output... (8 Replies)
Discussion started by: cs_novice
8 Replies

4. Shell Programming and Scripting

Finding a match and deleting the line above to it..

Hi All, I have a file with the data: Sun is Hot Moon is cool ; -- Mon Sep 10 08:54:10 CDT 2012 -- Mon Sep 11 08:54:10 CDT 2012 -- Mon Sep 12 08:54:10 CDT 2012 revoke connect from SREE; delete from = 'SREE'; grant connect to SREE with 'fastcar8'; I want to remove the line above... (8 Replies)
Discussion started by: raosr020
8 Replies

5. Shell Programming and Scripting

Reformatting single column text file starting new line when finding particular string

Hi, I have a single colum file and I need to reformat the file so that it creates a new line every time it come to an IP address and the following lines are corresponding rows until it comes to the next IP address. I want to turn this 172.xx.xx.xx gwpusprdrp02_pv seinwnprd03... (7 Replies)
Discussion started by: kieranfoley
7 Replies

6. Shell Programming and Scripting

Finding a string in a text file and posting part of the line

What would be the most succinct way of doing this (preferably in 1 line, maybe 2): searching the first 10 characters of every line in a text file for a specific string, and if it was found, print out characters 11-20 of the line on which the string was found. In this case, it's known that there... (13 Replies)
Discussion started by: busdude
13 Replies

7. Shell Programming and Scripting

Finding a flatfile & deleting first line

I have a small script where I want to see if a file exists & then delete the first line from it. I have code to help me find if the file exists, but I am unsure as to how to then take in the answer and remove the first line from the flatfile: This is what I have so far just to output if the... (3 Replies)
Discussion started by: fatalxkiss
3 Replies

8. Shell Programming and Scripting

adding new line after finding specific text

hello i need some help here are the contents of my file. test.txt this is filename 1.mp3 http://www.url.com/filenamehashed filename 2.mp3 http://www.url.com/fileamehashed something_else.zip http://www.url.com/filenamehashed so this file has 100 of these lines filename url I would... (9 Replies)
Discussion started by: mscice
9 Replies

9. Shell Programming and Scripting

Finding pattern & prepending a line with text

Hello Dudes, I have a task to make a unix shell script that should search for a specific TEXT in a file.If that TEXT is found, shell script should add a comment statement before that TEXT line. Ex : LINE 1 xxxxx LINE 2 xxxx CALL xxxx LINE 3 xxxx PERFORM UNTIL if i... (1 Reply)
Discussion started by: kirrushna
1 Replies

10. Shell Programming and Scripting

sed.. Add new line after finding text

I know that this my be really simple, but I'm having a hard time accomplishing it. I am trying to add a new line of text after finding a particular string of text in a file. Here's what I'm getting: sed: command garbled: N/search_string/\\new_text/ I was using "N" to add a line after the... (3 Replies)
Discussion started by: douknownam
3 Replies
Login or Register to Ask a Question