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
Delete repeated rows from a file tonet Shell Programming and Scripting 8 04-08-2008 10:42 AM
Repeated execution of Autosys Jobs Data469 Shell Programming and Scripting 2 02-28-2008 04:24 PM
matching repeated character robsonde Shell Programming and Scripting 5 12-13-2007 05:43 PM
how to find a word repeated in a file gurukottur UNIX for Dummies Questions & Answers 4 08-24-2006 05:53 AM
Repeated printF causes annoyance with do C|[anti-trust] High Level Programming 8 04-26-2005 03:08 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 10-03-2005
jijibabawu jijibabawu is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 2
Get rid of repeated entries.

I have the following problem. The file contains many lines already sorted according to their first arguments. Some of these first arguments are repetitive. For each first argument value, I need to keep the first and the last line that contain it. For example,
...
1 234
1 348
...
...
5 483
...
...
7 132
7 154
7 168
7 244
...

will become

...
1 234
1 348
...
...
5 483
...
...
7 132
7 244
...

I am really pulling my hair . Could any expert help me here ?
  #2 (permalink)  
Old 10-03-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Assuming your file is sorted as you say - numerically by first field then by second....

input file - jiji.txt

Code:
1 234
1 348
3 123
3 124
3 125
5 483
7 132
7 154
7 168
7 244
9 123
10 123
10 124
10 125
10 126
10 194

Script - jiji.sh

Code:
#!/bin/sh

input_file="jiji.txt"

cut -d' ' -f1 $input_file | uniq |\
  while read val; do
  values=`sed -n "/^${val} / p" $input_file`
  if [ `echo "$values" | wc -l` -eq 1 ]; then
     echo "$values"
  else
     first=`echo "$values" | sed -n '1 p'`
     last=`echo "$values" | sed -n '$ p'`
     echo "$first"
     echo "$last"
  fi
done

exit 0

Output

Code:
1 234
1 348
3 123
3 125
5 483
7 132
7 244
9 123
10 123
10 194

Cheers
ZB
  #3 (permalink)  
Old 10-03-2005
jijibabawu jijibabawu is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 2
Cool !!! Thanks a lot, ZB!!!
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 09:57 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