Keep lines with specific words up in an order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Keep lines with specific words up in an order
# 1  
Old 11-19-2009
Keep lines with specific words up in an order

I hava a file with following data:

number|CREDIT_ID|NULL
date|SYS_CREATION_DATE|NULL
varchar2|GGS_COMMIT_CHAR|NULL
varchar2|GGS_OP_TYPE|NULL
number|GGS_SCN|NULL|
number|GGS_LOG_SEQ|NULL
number|GGS_LOG_POS|NULL
number|GGS_ORACREC_SCN|NULL
varchar2|BATCH_ID|NULL
char|GGS_IMAGE_TYPE|NULL

Is it possible to keep lines up in an order, those are having GGS_LOG_SEQ, GGS_ORACREC_SCN, GGS_LOG_POS and GGS_COMMIT_CHAR
in their second field?

Expected o/p :

number|GGS_LOG_SEQ|NULL
number|GGS_ORACREC_SCN|NULL
number|GGS_LOG_POS|NULL
varchar2|GGS_COMMIT_CHAR|NULL
number|CREDIT_ID|NULL
date|SYS_CREATION_DATE|NULL
varchar2|GGS_OP_TYPE|NULL
number|GGS_SCN|NULL|
varchar2|BATCH_ID|NULL
char|GGS_IMAGE_TYPE|NULL


Any help in this regard is highly appreciated.

Thanks.
# 2  
Old 11-19-2009
Java Quick and dirty solution

Well Some quick and dirty solution ... more of dirty solution

Code:
CHKTHESE="GGS_LOG_SEQ  GGS_ORACREC_SCN GGS_LOG_POS GGS_COMMIT_CHAR"
rm -rf $CHKTHESE final.txt tmp result.txt
cat file >final.txt
for lookfor in $CHKTHESE
do
grep $lookfor file > $lookfor
grep -v $lookfor final.txt > tmp
cat tmp>final.txt
done
cat $CHKTHESE final.txt > result.txt
rm -rf $CHKTHESE final.txt tmp
echo "result.txt will have all data"
cat result.txt

Let us know if it worked
# 3  
Old 11-19-2009
A solution with awk :
Code:
awk -v order="GGS_LOG_SEQ,GGS_ORACREC_SCN,GGS_LOG_POS,GGS_COMMIT_CHAR" '
BEGIN {
   FS = "|";
   OtherKey = SUBSEP;

   Keys_cnt = split(order, Key, /,/);
   Key[++Keys_cnt] = OtherKey;

   for (i=1; i<=Keys_cnt; i++) Key_lines[Key[i]] = "";
}
{
   k = ($2 in Key_lines ? $2 : OtherKey);
   Key_lines[k] = Key_lines[k] $0 ORS;
}
END {
   for (i=1; i<=Keys_cnt; i++) {
      k = Key[i]
      lines = Key_lines[k]
      if (lines) printf "%s",lines;
   }
}
'input_file

Input_file :
Code:
number|CREDIT_ID|NULL
date|SYS_CREATION_DATE|NULL
varchar2|GGS_COMMIT_CHAR|NULL
varchar2|GGS_OP_TYPE|NULL
number|GGS_SCN|NULL|
number|GGS_LOG_SEQ|NULL
number|GGS_LOG_POS|NULL
number|GGS_ORACREC_SCN|NULL
varchar2|BATCH_ID|NULL
char|GGS_IMAGE_TYPE|NULL

Output:
Code:
number|GGS_LOG_SEQ|NULL
number|GGS_ORACREC_SCN|NULL
number|GGS_LOG_POS|NULL
varchar2|GGS_COMMIT_CHAR|NULL
number|CREDIT_ID|NULL
date|SYS_CREATION_DATE|NULL
varchar2|GGS_OP_TYPE|NULL
number|GGS_SCN|NULL|
varchar2|BATCH_ID|NULL
char|GGS_IMAGE_TYPE|NULL
$

Jean-Pierre.
# 4  
Old 11-19-2009
Chakrapani,

You were very close, this is what got after I ran a script.

number|GGS_LOG_SEQ|NULL
number|GGS_ORACREC_SCN|NULL
number|GGS_LOG_POS|NULL
varchar2|GGS_COMMIT_CHAR|NULL
number|CREDIT_ID|NULL
date|SYS_CREATION_DATE|NULL
varchar2|GGS_OP_TYPE|NULL
number|GGS_SCN|NULL|
number|GGS_LOG_SEQ|NULL
number|GGS_LOG_POS|NULL
number|GGS_ORACREC_SCN|NULL
varchar2|BATCH_ID|NULL
char|GGS_IMAGE_TYPE|NULL


Very close, only thing that it has not deleted following lines in middle:

number|GGS_LOG_SEQ|NULL
number|GGS_LOG_POS|NULL
number|GGS_ORACREC_SCN|NULL
# 5  
Old 11-19-2009
On my AIX box with KSH, the result is as required (as shown in my post)
The lines with GGS_LOG_SEQ... are not duplicated.

Verify your script, and perhaps try nawk.

Jean-Pierre.
# 6  
Old 11-19-2009
Quote:
Originally Posted by kolesunil
Very close, only thing that it has not deleted following lines in middle:

number|GGS_LOG_SEQ|NULL
number|GGS_LOG_POS|NULL
number|GGS_ORACREC_SCN|NULL
Hmm... does grep -v works on your system ? .. check man grep ...

I have two things done in the loop.
1. get all the GGS_.. in to files starting with same name
2. have another file called final.txt and remove whatever we found

After loop:
3. Now append all the GGS_ files and last final.txt
4. Just put all in result file.

Can you let me know your OS and grep version ... else we have use sed
# 7  
Old 11-19-2009
I tried using Jean-Pierre's solution and it worked for me. Thanks everyone.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace specific words with nothing

Hi I have a file like that contains infomation about genes exons introns made as a single string. i am just planning to get the gene name alone with out any extra information. intergenic_Nedd4_exon_0_F Gapvd1_intron_24_R Gapvd1_exon_25_Rmy output file should be intergenic_Nedd4 Gapvd1... (13 Replies)
Discussion started by: raj_k
13 Replies

2. Shell Programming and Scripting

Sort by specific order?

Hello all I was wondering if someone has an idea how to sort by a specific order, let's say by a specific alphabet containing only 4 letters like (d,s,a,p) instead of (a,b,c....z) ?? Cheers! (6 Replies)
Discussion started by: cabrao
6 Replies

3. Shell Programming and Scripting

how to print specific lines or words

Hi, Please have a look on below records. STG_HCM_STATE_DIS_TAX_TBL.1207.Xfm: The value of the row is: EMPLID = 220677 COMPANY = 919 BALANCE_ID = 0 BALANCE_YEAR = 2012 STG_HCM_STATE_DIS_TAX_TBL.1207.Xfm: ORA-00001: unique constraint (SYSADM.PS_TAX_BALANCE) violated ... (4 Replies)
Discussion started by: Sachin Lakka
4 Replies

4. UNIX for Dummies Questions & Answers

Extract lines with specific words with addition 2 lines before and after

Dear all, Greetings. I would like to ask for your help to extract lines with specific words in addition 2 lines before and after these lines by using awk or sed. For example, the input file is: 1 ak1 abc1.0 1 ak2 abc1.0 1 ak3 abc1.0 1 ak4 abc1.0 1 ak5 abc1.1 1 ak6 abc1.1 1 ak7... (7 Replies)
Discussion started by: Amanda Low
7 Replies

5. UNIX Desktop Questions & Answers

Display a specific words from a multiple lines

well, i am so not familiar with this kind of things but i am gonna explain extactly what i am looking for so hopfully someone can figure it out :) i have a command that shows memory usage besides the process name, for example(the command output): 500 kb process_1 600 kb process_2 700 kb... (4 Replies)
Discussion started by: Portabello
4 Replies

6. UNIX for Dummies Questions & Answers

Searching for multiple words on a line in any order issue

Hi again I have figured out how to be able to sort through lines in a file with multiple words in any order and display them using this command: cat file | grep -i $OPTION1 | grep -i $OPTION2 | grep -i $OPTION3 OPTION1 is 2008, OPTION2 is Mar, OPTION 3 is Tue Result: Tue Mar 25... (4 Replies)
Discussion started by: semaj
4 Replies

7. Shell Programming and Scripting

awk script to get words which are not in a particula order

Hi all, I need a big help. I've a requirment which should be done in awk scripting only.. My requirment is: In below file, there are some lines started with 'master replica:'. i want to get the exact word in that line which should be before '@', and i've to create a file with these names... (5 Replies)
Discussion started by: raghu.iv85
5 Replies

8. UNIX for Dummies Questions & Answers

sort by column or specific order

Hi, I need to sort it by column or need it in a specific order... input is ===== uid=shashi country= india region =0 ph=0 uid= jon region= asia ph= 12345 country=0 uid = man country= india ph=2222 region=0 uid= neera region= asia ph= 125 country=0 output should be uid=shashi ... (1 Reply)
Discussion started by: hegdeshashi
1 Replies

9. Shell Programming and Scripting

compare strings, words in different order

Hi, Would anyone know how to compare two strings, and only throw an error if there were different words, not that the same words were in a different order? e.g "A B C" vs "B C A" ->OK "A B C" vs "A D C" -> BAD Thanks! (2 Replies)
Discussion started by: rebelbuttmunch
2 Replies

10. Shell Programming and Scripting

Ignore some lines with specific words from file comparison

Hi all, I need help in doing this scenario. I have two files with multiple lines. I want to compare these two files but ignoring the lines which have words like Tran, Loc, Addr, Charge. Also if i have a word Credit in line, i want to tokenize (i.e string after character " ... (2 Replies)
Discussion started by: jakSun8
2 Replies
Login or Register to Ask a Question

Featured Tech Videos