The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
SED: how to remove newline after pattern? nico.ben Shell Programming and Scripting 1 01-02-2009 10:59 PM
sed/awk help to match list of patterns and remove from org file rajan_san Shell Programming and Scripting 1 12-07-2008 02:56 AM
Delete lines between two patterns without deleting the second pattern Ilja Shell Programming and Scripting 1 11-14-2008 09:53 AM
comment/delete a particular pattern starting from second line of the matching pattern imas Shell Programming and Scripting 4 10-13-2008 02:37 AM
sed remove everything up to the pattern katrvu Shell Programming and Scripting 4 04-08-2008 09:35 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 04-07-2009
ppat7046 ppat7046 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 24
delete two patterns and remove one pattern

Friends,

I have .txt file with following format.
START
ABC|Prashant1|Patel1
ABC|Prashant2|Patel2
ABC|Prashant1|Patel1
ABC|Prashant2|Patel2
END


I would like to do:
1) Delete line with START
2) Delete line with END
3) Remove ABC|
4) Delete duplicate records

The following command works fine which deletes line with START and END
sed -e /^START/d -e /^END/d Filename.txt

How do I incorporate task 3 and 4?
NOTE: The file will have more than 500,000 thousand rows.

Thanks in advance for suggestion,
Prashant
  #2 (permalink)  
Old 04-07-2009
pinnacle pinnacle is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 182
Quote:
Originally Posted by ppat7046 View Post
Friends,

I have .txt file with following format.
START
ABC|Prashant1|Patel1
ABC|Prashant2|Patel2
ABC|Prashant1|Patel1
ABC|Prashant2|Patel2
END



I would like to do:
1) Delete line with START
2) Delete line with END
3) Remove ABC|
4) Delete duplicate records

The following command works fine which deletes line with START and END
sed -e /^START/d -e /^END/d Filename.txt

How do I incorporate task 3 and 4?
NOTE: The file will have more than 500,000 thousand rows.

Thanks in advance for suggestion,
Prashant

Code:
nawk -F'|'  'NF==3{print $2,$3}' patel
  #3 (permalink)  
Old 04-07-2009
tostay2003 tostay2003 is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 23
Quote:
Originally Posted by zenith View Post
Code:
nawk -F'|'  'NF==3{print $2,$3}' patel
This code is not doing uniq + removes pipe delimiter

use this

Code:
grep "|" test | sed 's/^ABC|//g' | sort -u
  #4 (permalink)  
Old 04-07-2009
pinnacle pinnacle is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 182
Quote:
Originally Posted by tostay2003 View Post
This code is not doing uniq + removes pipe delimiter

use this

Code:
grep "|" test | sed 's/^ABC|//g' | sort -u

Tostay2003:
What if the data doesnt start with ABC your logic fails

use this
nawk -F'|' 'NF==3{print $2,$3}' patel | sort -u
  #5 (permalink)  
Old 04-07-2009
tostay2003 tostay2003 is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 23
Quote:
Originally Posted by zenith View Post
Tostay2003:
What if the data doesnt start with ABC your logic fails

use this
nawk -F'|' 'NF==3{print $2,$3}' patel | sort -u
I assumed from the authors description that the first field remains common in the file

Quote:
START
ABC|Prashant1|Patel1
ABC|Prashant2|Patel2
ABC|Prashant1|Patel1
ABC|Prashant2|Patel2
END
...

3) Remove ABC|
Use this if you didnt mean that the first field woudl be same.
Code:
grep "|" test | cut -d'|' -f2,3 | sort -u
or with slight amendment to code written by zenith i.e. by adding OFS
  #6 (permalink)  
Old 04-07-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Code:
nawk -F'|' 'NF==3 && !a[$2,$3]++ {print $2,$3}' patel
  #7 (permalink)  
Old 04-08-2009
ppat7046 ppat7046 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 24
Thank you all for your reply.

Code:
nawk -F'|' 'NF==3 && !a[$2,$3]++ {print $2,$3}' patel
I had made small change in print statment because it was not printing | symbol.
Code:
nawk -F'|' 'NF==3 && !a[$2,$3]++ {print $2,"|",$3}' patel
However, it prints the space after and before | symbol.

Thanks,
Prashant
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 11:35 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