Computing.Net > Forums > Unix > Need help on shell script

Need help on shell script

Reply to Message Icon

Original Message
Name: kate01
Date: March 8, 2004 at 11:56:45 Pacific
Subject: Need help on shell script
OS: UNIX
CPU/Ram: unsure
Comment:

In need to be able to identify and delete certain lines in a file. An example of some lines in my file:

123|405|A|0000|9999
123|456|A|0000|9999
123|456|1|0000|1999

Where there are multiple lines with the same data in Field 1 and Field 2, I want to delete the line that has an A in Field 3.

However, I have lines that do not have multiple occurences of Field 1 and Field 2 that are also followed by an A in Field 3. I do NOT want to delete those lines.

So, in my example above, I would only want to delete the middle line:
123|456|A|0000|9999

How can I do this? I am a complete novice at this so any help is appreciated.
Thx.



Report Offensive Message For Removal

Response Number 1
Name: aigles
Date: March 8, 2004 at 12:55:32 Pacific
Subject: Need help on shell script
Reply: (edit)

Try the following script :


awk -v FS='|' '
function PrintRec( i) {
  for (i=1; i<=cnt; i++) {
    if (cnt == 1 || Fld3[i] != "A")
      print Rec[i];
    delete Rec[i];
  }
  cnt = 0;
}

function MemoRec() {
  cnt++;
  Rec[cnt] = $0;
  Fld3[cnt] = $3;
  PrvFld1 = $1;
  PrvFld2 = $2;
}

$1 != PrvFld1 || $2 != PrvFld2 { PrintRec() }
{ MemoRec() }
END { PrintRec() }
' input_file

Jean-Pierre.


Report Offensive Follow Up For Removal

Response Number 2
Name: kate01
Date: March 8, 2004 at 13:40:46 Pacific
Subject: Need help on shell script
Reply: (edit)

Thanks - I'll try that.


Report Offensive Follow Up For Removal

Response Number 3
Name: emalik05
Date: March 30, 2004 at 10:11:28 Pacific
Subject: Need help on shell script
Reply: (edit)

Can someone help me with deleting few lines from the end of the file. I use

sed '$d' $filename > $filename2

to delete a single line but confuss on multiple lines. Please help. Thanks


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Need help on shell script

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software