delete records from a file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

delete records from a file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-09-2007
dsravan dsravan is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 10 August 2012, 6:18 AM EDT
Posts: 245
Thanks: 1
Thanked 0 Times in 0 Posts
delete records from a file

I have a big file with "|" delimiter. I want to delete all the records that have 'abc' in the 2nd field. How can i do that? I am not abe to open it in VI that is why i need to do it from outside. Please suggest
Sponsored Links
    #2  
Old 09-10-2007
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
 
Join Date: Apr 2002
Last Activity: 20 December 2009, 10:56 AM EST
Location: Italy
Posts: 370
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
awk -F'|' '$2 !~ /^abc$/' input_file.txt > output_file.txt

Sponsored Links
    #3  
Old 09-10-2007
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
 
Join Date: Feb 2006
Last Activity: 24 October 2014, 6:58 PM EDT
Location: Almerķa, Spain
Posts: 777
Thanks: 24
Thanked 110 Times in 105 Posts
You can also use sed:


Code:
sed -e "/^[^\|]*\|abc\|/d" input_file.txt > output_file.txt

    #4  
Old 09-10-2007
dsravan dsravan is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 10 August 2012, 6:18 AM EDT
Posts: 245
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks Klashxx

Can you explain what this means.

awk -F'|' '$2 !~ /^abc$/' input_file.txt > output_file.txt

does !~ means equal to? i thought its not equal to?
Sponsored Links
    #5  
Old 09-10-2007
ajcannon ajcannon is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 19 November 2008, 10:48 AM EST
Location: Binfield, Berkshire. UK
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
what does it mean

I think this means 'contains'

the '^abs' means is there the chars 'abc' at the beginning of $2 which is the field delimited by the character given in the '-F' flag to awk
Sponsored Links
    #6  
Old 09-10-2007
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
 
Join Date: Apr 2002
Last Activity: 20 December 2009, 10:56 AM EST
Location: Italy
Posts: 370
Thanks: 0
Thanked 0 Times in 0 Posts
Yes it's almost right. The operator "!~" means "does not match regexp".
The given regular expression matches exactly the word "abc" in the second field, so all the lines that does match this criteria are discarded.
Sponsored Links
    #7  
Old 09-10-2007
DILEEP410 DILEEP410 is offline
Registered User
 
Join Date: Dec 2006
Last Activity: 29 August 2013, 9:16 PM EDT
Posts: 153
Thanks: 0
Thanked 0 Times in 0 Posts
Java

But it won't match for a field value 'comabcpvt'.For that just use /abc/ in the regex.

With regards
Dileep Pattayath
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need unix commands to delete records from one file if the same record present in another file... msathees Shell Programming and Scripting 6 05-08-2012 12:46 AM
How can you delete records in a file matching a pattern? mode09 UNIX for Dummies Questions & Answers 2 07-15-2008 01:53 PM
Use records from one file to delete records in another file kenneth.mcbride UNIX for Dummies Questions & Answers 5 06-18-2008 02:36 PM
Delete Duplicate records from a tilde delimited file irshadm Shell Programming and Scripting 5 12-06-2007 04:36 AM
Hai delete the records readycpbala Shell Programming and Scripting 1 02-16-2007 06:49 AM



All times are GMT -4. The time now is 11:59 AM.