Extract only first occurance

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Extract only first occurance
# 1  
Old 04-06-2018
Extract only first occurance

Hi All,

From the below file. I need to get only the first occurrence and print. I tried to do it in separate grep not coming as expected

Original file

Code:
11001;1213;304;;;;;;;111020677.64;;;;;;;;;;;;;;;;;;;;;;;;;;
11001;1214;304;;;;;;;102376462.96;;;;;;;;;;;;;;;;;;;;;;;;;;
11001;1215;304;;;;;;;12348678.19;;;;;;;;;;;;;;;;;;;;;;;;;;
11001;1216;304;;;;;;;88340.57;;;;;;;;;;;;;;;;;;;;;;;;;;
11001;1217;304;;;;;;;127834.48;;;;;;;;;;;;;;;;;;;;;;;;;;
11001;1218;304;;;;;;;105220.65;;;;;;;;;;;;;;;;;;;;;;;;;;
11002;1213;304;;;;;;;244189.32;;;;;;;;;;;;;;;;;;;;;;;;;;
11002;1214;304;;;;;;;220748.79;;;;;;;;;;;;;;;;;;;;;;;;;;
11003;1215;304;;;;;;;64361.46;;;;;;;;;;;;;;;;;;;;;;;;;;
11003;1216;304;;;;;;;113.89;;;;;;;;;;;;;;;;;;;;;;;;;;
11003;1218;304;;;;;;;21.16;;;;;;;;;;;;;;;;;;;;;;;;;;


Expected Result :
Code:
11001;1213;304;;;;;;;111020677.64;;;;;;;;;;;;;;;;;;;;;;;;;;
11002;1213;304;;;;;;;244189.32;;;;;;;;;;;;;;;;;;;;;;;;;;
11003;1215;304;;;;;;;64361.46;;;;;;;;;;;;;;;;;;;;;;;;;;

# 2  
Old 04-06-2018
Any attempts / ideas / thoughts from your side?

First occurrence of what?
# 3  
Old 04-06-2018
Quote:
Originally Posted by RudiC
Any attempts / ideas / thoughts from your side?

First occurrence of what?

First occurance of first field. For example 11001 having many rows. I want to get only first row
# 4  
Old 04-06-2018
What have you tried @arunkumar_mca?
# 5  
Old 04-06-2018
Quote:
Originally Posted by Scrutinizer
What have you tried @arunkumar_mca?
I need to have only 1 and 10 the column so I have cut the record and tried to do sort on key. It dont worked.


Code:
 cut -d ";" -f1,10 file| sort -u  -k1,1

I got
Code:
11001;102376462.96
11001;105220.65
11001;111020677.64
11001;12348678.19
11001;127834.48
11001;88340.57
11002;220748.79
11002;244189.32
11003;113.89
11003;21.16
11003;64361.46

But I need

Code:
11001;102376462.96
11002;220748.79
11003;64361.46

# 6  
Old 04-06-2018
For sort to work you also have to specify the field separator. You can try:
Code:
sort -ut\; -k1,1 file

However, this is not standard (POSIX) behavior for sort and cannot be relied upon and some sorts will render very different results.

A better option would be:
Code:
awk '!A[$1]++' FS=\; file

Moderator's Comments:
Mod Comment You beat me (again) by the split second!


---
If you only want to print those two fields, try:
Code:
cut -d ";" -f1,10 file | sort -ut\;  -k1,1

or the more reliable:
Code:
awk '!A[$1]++{print $1, $10}' FS=\; OFS=\; file


Last edited by RudiC; 04-06-2018 at 02:52 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

2. Shell Programming and Scripting

Extract multiple occurance of strings between 2 patterns

I need to extract multiple occurance strings between 2 different patterns in given line. For e.g. in below as input ------------------------------------------------------------------------------------- mike(hussey) AND mike(donald) AND mike(ryan) AND mike(johnson)... (8 Replies)
Discussion started by: sameermohite
8 Replies

3. Shell Programming and Scripting

Multiple occurance of file

Hi all, I have file structure as file.log 84t-rw-r--r-- 1 emily04 us_cms 24492717 Oct 5 13:29 vgtree_84_1_K3L.root 85t-rw-r--r-- 1 emily04 us_cms 50410380 Oct 5 16:06 vgtree_85_1_uZv.root 85t-rw-r--r-- 1 emily04 us_cms 50567380 Oct 5 16:06 vgtree_85_1_hjv.root 86t-rw-r--r-- 1 emily04... (4 Replies)
Discussion started by: emily
4 Replies

4. Shell Programming and Scripting

replace every second occurance of a string

I want to replace every 2nd occurance of a string/character from a line. ababacbsbddbsbbcbdbssb i want to replace every s2nd b with @ like below should be like aba@acbs@ddbs@bc@dbss@ (3 Replies)
Discussion started by: ratheeshjulk
3 Replies

5. Shell Programming and Scripting

Trying to grep for '--' occurance

Hello Im trying to grep for a string in grub.conf . I've used the -F option since its a long string, but when i execute, i run into errors. Script and output below. GRUBPASSWD="password --md5 xyz" if grep -Fxq $GRUBPASSWD /etc/grub.conf then . . output: grep: unrecognized option... (5 Replies)
Discussion started by: bludhemn
5 Replies

6. Shell Programming and Scripting

extract till occurance of a string

hi , i have an xml that comes in a single, the entire xml file is read as a single line when i open in edit plus or unix. i need to amend the contents of this xml file. below is the extract from the file <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"... (5 Replies)
Discussion started by: sais
5 Replies

7. Shell Programming and Scripting

First occurance

A PERL script that prints just the first occurrence of a string in a file and immediately exits (the string and the filename are the first and the second command line arguments; I used filehandle to open an input file). (1 Reply)
Discussion started by: aadi_uni
1 Replies

8. Shell Programming and Scripting

Split on last occurance

I want to call a script like this: ./somescript some-thing.knows.what.ending Inside the script it needs to split at last .(period) so I can: a=some-thing.knows.what b=ending I know I can do it in perl but im still learing awk and sed. Thanks (3 Replies)
Discussion started by: Ikon
3 Replies

9. Shell Programming and Scripting

How to insert values in 1st occurance out of two occurance in a file

Hi I have a file which contains the following two lines which are same But I would like to insert the value=8.8.8.8 in the 1st occurance line and value=9.9.9.9 in the 2nd occurance line. <parameter name="TestIp1" value=""> <parameter name="TestIp1" value=""> Please suggest (1 Reply)
Discussion started by: madhusmita
1 Replies

10. Shell Programming and Scripting

awk: last occurance

Hi All, I need to extract the last occurance of a pattern match. So far I've got the code below which extracts the first occurance. Any ideas how I can modify it so that it extracts the last? BEGIN {} { if (data++ == 0) ... (17 Replies)
Discussion started by: pondlife
17 Replies
Login or Register to Ask a Question