Search the word to be deleted and delete lines above this word starting from P1 to P3


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search the word to be deleted and delete lines above this word starting from P1 to P3
# 1  
Old 05-23-2011
Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi,
I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this:

Records
Code:
P1
10,23423432
,77:1
,234:2
P2
10,9089004
,77:1
,234:2
,87:123
,9898:2
P3
456456
P1
:123,456456546
P2
abc:324234
,89237494
P3
,98:234
.
.
.

and so on.

The file contains thousands of records.

Now I have to search for a particular word for eg I am searching 9089004. Then the records in which 9089004 will lie I mean from P1 till P3 will be deleted from the file and rest of the records will exist in the file. The problem is that the word can exist anywhere between P1 to P2 or from P2 to P3 and then I have to delete that record i.e. from P1 to P3.

Please help
# 2  
Old 05-23-2011
See if this works for you:
Code:
#!/usr/bin/ksh
rm -f temp_file
rm -f out_file
mP1="N"
while read mLine; do
  mTag=$(echo ${mLine} | cut -c1-2)
  if [[ "${mTag}" = "P1" ]]; then
    mP1="Y"
  fi
  if [[ "${mP1}" = "Y" ]]; then
    echo ${mLine} >> temp_file
  else
    echo ${mLine} >> out_file
  fi
  if [[ "${mTag}" = "P3" ]]; then
    mStr=$(egrep '9089004' temp_file)
    if [[ "${mStr}" = "" ]]; then
      cat temp_file >> out_file
    fi
    rm -f temp_file
    mP1="N"
  fi
done < inp_file

# 3  
Old 05-24-2011
Question

Thanks for your reply ...... shell_life .....

It is working fine , but I am getting P3 records above P1

For eg the input file is:

If I am searching the number 9881119347
Code:
P1
123
P2
9881119347,87
P3
9881119347
P1
998811347
P2
34534
P3
98811147
P1
78686
P2
902384930284
P3
2904898


Then the output generating is:
Code:
9881119347
P1
998811347
P2
34534
P3
98811147
P1
78686
P2
902384930284
P3
2904898


But it should be like below:
Code:
P1
998811347
P2
34534
P3
98811147
P1
78686
P2
902384930284
P3
2904898

I am searching only till P3..but I have to search before the next P1 occurs...Please check the output that is required.


Kindly advice

Last edited by Franklin52; 05-24-2011 at 12:43 PM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

3. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word "description" excluding weird characters like $&lmp and without html tags in the new file output.txt. Help me. Thanx in advance. I have attached the input... (4 Replies)
Discussion started by: sachit adhikari
4 Replies

4. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script?

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word description excluding weird characters like $$#$#@$#@***$# and without html tags in the new file output.txt. Help me. Thanx in advance. My final goal is to... (11 Replies)
Discussion started by: sachit adhikari
11 Replies

5. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

6. UNIX for Dummies Questions & Answers

Script to search for a particular word in files and print the word and path name

Hi, i am new to unix shell scripting and i need a script which would search for a particular word in all the files present in a directory. The output should have the word and file path name. For example: "word" "path name". Thanks for the reply in adv,:) (3 Replies)
Discussion started by: virtual_45
3 Replies

7. Shell Programming and Scripting

How to delete all lines before a specific word?

Let's say we have a file containing: alllllsadfsdasdf qwdDDDaassss ccxxcxc#2222 dssSSSSddDDDD D1Sqn2NYOHgTI Hello Alex ssS@3 Ok, and let's say we want to delete all words from D1Sqn2NYOHgTI and back, this means to delete the words (and the lines of them) : alllllsadfsdasdf... (2 Replies)
Discussion started by: hakermania
2 Replies

8. Shell Programming and Scripting

search a word and delete consecutive lines below it

Hi all coders, I need a help to process some data. I have this file, 3 09/21/08 03:32:07 started undef mino Oracle nmx004.wwdc.numonyx.co m Message Text : The Oracle session with the PID 1103 has a CPU time consuming of 999.00... (3 Replies)
Discussion started by: vikas027
3 Replies

9. Shell Programming and Scripting

delete lines containing a specific word in afile

Hi, Please suggest how to write a shell script which delets all the lines containing the word unix in the files supplied as argument in the shell. (4 Replies)
Discussion started by: sireesha9
4 Replies

10. Shell Programming and Scripting

need a cmd to search starting word

example - shsk mss-sdsd-asd i need a command which will search for staring word not others it should search only -shsk cat filename | grep '-' will search whole '-' in the file but i need to search only staring '-' thank u revenna (1 Reply)
Discussion started by: revenna
1 Replies
Login or Register to Ask a Question