Register
It is currently Thu Jul 31, 2014 9:31 am

How to merge two greps storing only the first ocurrence?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Thu Feb 27, 2014 2:43 pm   

Joined: Thu Feb 27, 2014 2:40 pm
Posts: 1
I have the following script:

Code:
#!/bin/bash

echo
echo "INPUT"
echo
   
#grep 1
grep -v '.*SYSTSIN' input.txt | grep -oPh '(?!.*?\.tmp)[^|]+\|\s+\K[^|]+(?=\|\s+OK.*?\[INPUT)' |  uniq -i |
while read file; do
    echo "$file"
done

echo
echo "OUTPUT"
echo
   
# grep 2
grep -Poh '(?!.*?\.tmp)[^|]+\|\s+\K[^|]+(?=\|\s+OK.*?\[OUTPUT)' input.txt | uniq -i |
while read file; do
    echo "$file"
done



With this is content in input.txt file:

Code:
234234 |  robert.txt| OK|  INFO| [INPUT]
  54656 |   maria.txt| OK|  INFO| [OUTPUT]
078978 |   maria.txt| OK|  INFO| [INPUT]
     8 |  robert.txt| OK|  INFO| [OUTPUT]
    57 |   steve.txt| OK|  INFO| [INPUT]
   567 |   steve.txt| OK|  INFO| [OUTPUT]
  09832 |   marta.txt| OK|  INFO| [OUTPUT]
    99 |   marta.txt| OK|  INFO| [INPUT]
   ASD |    nick.txt| OK|  INFO| [INPUT]
   RFE |   jesus.txt| OK|  INFO| [OUTPUT]


Te result printed is:

Code:
INPUT

robert.txt
maria.txt
steve.txt
marta.txt
nick.txt

OUTPUT

maria.txt
robert.txt
steve.txt
marta.txt
jesus.txt


Now I'm trying to go is get only the first occurrence of each filename and save it in the corresponding array.
I mean: if a match is saved in one of the two arrays, all the matches of this filename which are below, will be ignored.

With these conditions, the result that I'm looking for is:

Code:
INPUT

robert.txt
steve.txt
nick.txt

OUTPUT

maria.txt
marta.txt
jesus.txt


Could I do it with `awk` command? Do you have another idea to do it? May be reading it line by line?


Top
 Profile  
 PostPosted: Fri Feb 28, 2014 2:38 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 533
hi,

i'd begin to standardize the input file by replacing blanks and vertical bars with a semicolon.
it'd be easier then to treat what is now a csv formated file using awk.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP