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.

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

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

A solution with awk :

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] = "";
}
{
   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
      lines = Key_lines[k]
      if (lines) printf "%s",lines;
   }
}
'input_file

Input_file :

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:

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.

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

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.

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

I tried using Jean-Pierre's solution and it worked for me. Thanks everyone.