The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to read record by record from a file in unix raoscb UNIX for Dummies Questions & Answers 1 05-16-2008 07:30 AM
Remove First and Last Record from a file ravikuc UNIX for Dummies Questions & Answers 1 10-11-2007 04:35 AM
splitting a record and adding a record to a file rsolap Shell Programming and Scripting 1 08-13-2007 02:58 PM
command to remove last record on file mheinen UNIX for Dummies Questions & Answers 4 01-09-2007 04:39 PM
remove duplicated xml record in a file under unix happyv Shell Programming and Scripting 8 09-20-2006 02:36 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-22-2008
kanu_pathak kanu_pathak is offline
Registered User
  
 

Join Date: Jan 2008
Location: India
Posts: 34
How to remove a particular record from a file?

Please tell me the command(s) to remove a particular record from the file and placing the rest of the record in a seperate file.
  #2 (permalink)  
Old 07-22-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,441
Post a sample of your input file in the original format and the exact output you want and please place them within code tags (select the text and click on the # symbol above the edit window).
  #3 (permalink)  
Old 07-22-2008
kanu_pathak kanu_pathak is offline
Registered User
  
 

Join Date: Jan 2008
Location: India
Posts: 34
Please find the attached file in text format.

say if i have to remove the NA2 record and NPD record. (this is a portion of file which contains thousands of records). i believe sed pattern matching will work.
Attached Files
File Type: txt file.txt (1.2 KB, 51 views)
  #4 (permalink)  
Old 07-22-2008
kanu_pathak kanu_pathak is offline
Registered User
  
 

Join Date: Jan 2008
Location: India
Posts: 34
file type:


Code:
NMT000010000100001ENVL,CSP,28#,9X12,KFT,1C                                                        00001
NA20000105500000003081547100100008000000000024.19         000000000000001DZ  000000000024.19  000000000000000  00002
NPD                                                                                                                                            TOP63120                      TOP63120
NP2                                                                                                                                                                                                                                                                                                                                                                                             00000000000000 00000000000000                                                             000                                                                                                                                                                           00000000000000                               00000000000001 00000000000000                                               00000000000000
NMT000010000800001PAD,LGL RL,PRISM,LTR,BE

  #5 (permalink)  
Old 07-22-2008
kanu_pathak kanu_pathak is offline
Registered User
  
 

Join Date: Jan 2008
Location: India
Posts: 34
grep is not doing anything.
new_file is still with the same records.
anything with the sed?
  #6 (permalink)  
Old 07-22-2008
BMDan BMDan is offline
Registered User
  
 

Join Date: Jul 2008
Location: BlackMesh Managed Hosting
Posts: 66

Code:
grep -vE '^(NPD|NA2)' file > file2

While it's trivial to do this in sed, if there's a way to remove newlines in sed, I've never found it. You could always try piping it into Perl:

Code:
cat file | perl -ne 'print unless /^(NPD|NA2)/;'

  #7 (permalink)  
Old 07-22-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,441
Quote:
Originally Posted by kanu_pathak View Post
grep is not doing anything. new_file is still with the same records.
Work's for me

Code:
$ cat file
NMT000010000100001ENVL,CSP,28#,9X12,KFT,1C                                                        00001
NA20000105500000003081547100100008000000000024.19         000000000000001DZ  000000000024.19  000000000000000  00002
NPD                                                                                                                                            TOP63120
TOP63120
NP2                                                                                                                                                                                                                                                                                                                                                               
00000000000000 00000000000000                                                             000                                                                                    
00000000000000                               00000000000001 00000000000000                                               00000000000000
NMT000010000800001PAD,LGL RL,PRISM,LTR,BE
$ grep -v '^NA2\|^NPD' file > new_file
$ cat new_file
NMT000010000100001ENVL,CSP,28#,9X12,KFT,1C                                                        00001
TOP63120
NP2                                                                                                                                                                                                                                                                                                                                                               
00000000000000 00000000000000                                                             000                                                                                    
00000000000000                               00000000000001 00000000000000                                               00000000000000
NMT000010000800001PAD,LGL RL,PRISM,LTR,BE
$ wc -l file
       8 file
$ wc -l new_file
       6 new_file

Or sed
Code:
$ sed '/^NA2/d;/^NPD/d;' file > new_file2
$ wc -l new_file2
       6 new_file2

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0