Deleting a line from a file based on one specific string instance?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting a line from a file based on one specific string instance?
# 1  
Old 01-27-2010
Data Deleting a line from a file based on one specific string instance?

Hello!

I need to delete one line in a file which matches one very precise instance of a string only. When searching the forum I unfortunately only found a solution which would delete each line on which a particular string occurs.

Let's assume I have a file composed of thousands of lines and let's call the file chap-secrets. Let's take the following sample entries:

Code:
#USERNAME      SERVER         PASSWORD         IP
pp          pptpd             blahblah         *
cpp              pptpd             ppbbbbpp         *
hopp           pptpd             ggmmsasm         *
flopps    pptpd     01234567890abcdef    *

The usernames are unique, naturally.

I would now like to delete the line containing the username "pp" only. The following suggestion has been made in a related thread:

Code:
awk 'match($0,"pp") == 0 {print $0}' FILENAME > NEWFILENAME

However, this doesn't work here, for it would delete (or rather not store) all these lines, because all lines with the usernames "cpp", "hopp" and "flopps" are valid matches, all lines with the server name "pptpd" are valid matches and all lines where the password contains a "pp" (such as "ppbbbbpp") are valid matches, too, and these lines all would be deleted (or rather not stored) as well.

Note that the number of spaces between the fields differ as well all the time, so I can't search for a pattern like

Code:
"pp          pptpd"

either.

I suppose this problem is very simple for some of you, but for me it's not, thus every help would be very much appreciated. Smilie

Thank you!
# 2  
Old 01-27-2010
Code:
grep -w "pp" chap-master

# 3  
Old 01-27-2010
Code:
$sed  '/^pp */d' chap-secrets

or

$sed -i '/^pp */d' chap-secrets

# 4  
Old 01-27-2010
MySQL

Try this :

awk -F " " '{ if( $1!="pattern" ) print $0 }' filename
# 5  
Old 01-27-2010
One more:
Code:
awk '$1!="pp"' infile

These would work too for your example, but not if there is a USERNAME that starts with pp:
Code:
awk '!/^pp/' infile

Code:
sed /^pp/d infile

Code:
grep -v ^pp infile


Last edited by Scrutinizer; 01-27-2010 at 03:46 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print specific line in file based on criteria

In the file below I am trying to extract a specific instance of path, if the adjacent plugin": "/rundb/api/v1/plugin/49/. Thank you :). file "path": "/results/analysis/output/Home/Auto_user_S5-00580-4-Medexome_65_028/plugin_out/FileExporter_out.52", "plugin": "/rundb/api/v1/plugin/49/",... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Shell Programming and Scripting

Add specific string to last field of each line in perl based on value

I am trying to add a condition to the below perl that will capture the GTtag and place a specific string in the last field of each line. The problem is that the GT value used is not right after the tag rather it is a few fields away. The values should always be 0/1 or 1/2 and are in bold in the... (12 Replies)
Discussion started by: cmccabe
12 Replies

3. Shell Programming and Scripting

Issue deleting all lines (having a specific string) in a file

I'm trying to create a script. There are 2 files - fileA.log & fileB.log fileA.log has the below data : aaaa cccc eeee fileB.log has the below data : cjahdskjah aaaa xyz jhaskjdhas bbbb abc ajdhjkh cccc abc cjahdskjah ... (7 Replies)
Discussion started by: Pandee
7 Replies

4. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

HI Can any one guide me how to achieve this task. I have 2 files env.txt #Configuration.Properties values identity_server_url = http://identity.test-hit.com:9783/identity/service/user/register randon_password_length = 6 attachment_file_path = /pass/temp/attachments/... (1 Reply)
Discussion started by: nikilbr86
1 Replies

5. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

6. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

Hi I am not the best scripter in the world and have run into a issue which you might be able to guide me on... I have two files. File1 : A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB File2: C345,... (5 Replies)
Discussion started by: luckycharm
5 Replies

7. UNIX for Dummies Questions & Answers

Deleting lines that contain a specific string from a space delimited text file?

Hi, I have a space delimited text file that looks like the following: 250 rs10000056 0.04 0.0888 4 189321617 250 rs10000062 0.05 0.0435 4 5254744 250 rs10000064 0.02 0.2403 4 127809621 250 rs10000068 0.01 NA 250 rs1000007 0.00 0.9531 2 237752054 250 rs10000081 0.03 0.1400 4 17348363... (5 Replies)
Discussion started by: evelibertine
5 Replies

8. Shell Programming and Scripting

Deleting files that don't contain particular text strings / more than one instance of a string

Hi all, I have a directory containing many subdirectories each named like KOG#### where # represents any digit 0-9. There are several files in each KOG#### folder but the one I care about is named like KOG####_final.fasta. I am trying to write a script to copy all of the KOG####_final.fasta... (3 Replies)
Discussion started by: kmkocot
3 Replies

9. UNIX for Dummies Questions & Answers

(cont) Retrieve line from a file based on a value in specific column

HI, Your help was great: awk -F":" '$5 ~ /^P/{print }' file I would like to know what changes need to be done to this line code, so that I can put it in a shell script and call it as the example below. example: countries that start with chacater 'P' > country P Result: ... (0 Replies)
Discussion started by: efernandes
0 Replies

10. UNIX for Dummies Questions & Answers

Retrieve line from a file based on a value in specific column

Hi, I have a file that has several values seperated by ":" 2006:John:Student:Football:Portugal:Cinema 2006:James:Engineer:Basket:Poland:Theatre 2007:Lucy:Diver:Gymnastic:England:Music 2007:Smith:Plumber:Basket:Spain:Poker I need make a filter based on the 5th field to find countries that... (1 Reply)
Discussion started by: efernandes
1 Replies
Login or Register to Ask a Question