Seach for Record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Seach for Record
# 1  
Old 07-03-2007
Seach for Record

Below is an example of a file with some records... each record start with P1 till the next P1.

If I search for something like "4,4545:" then I want all the records with "4,4545:" to be removed from original file and copied to a temp file.

Code:
P1
1,123455654444
2,7126817361873
3,2134128282828
4,4545:812987123
P2
5,123:123213123
6,98:123123123
P3
7,9123123123
8,908890909
9,99919239123
P1
1,123455654444
2,7126817361873
3,2134128282828
4,4545:812987123
P2
5,123:123213123
6,98:123123123
P3
7,9123123123
8,908890909
9,99919239123
P1
1,123455654444
2,7126817361873
3,2134128282828
4,4545:812987123
P2
5,123:123213123
6,98:123123123
P3
7,9123123123
8,908890909
9,99919239123

# 2  
Old 07-03-2007
Code:
sed "s/P1/\\
&/" file | awk -v RS="" ' { if( $0 ~ /4,4545:/ ) { print > "newfile" } else print > "temp" } '
mv temp > file

# 3  
Old 07-03-2007
the record file is very big, so i get error "cannot be longer than 3,000 bytes" when using awk
# 4  
Old 07-03-2007
Code:
{ rm infile && sed -n '
/P1/ b block
H
$ b block
b
:block
x
/4,4545:/w temp
/4,4545:/!p'>infile;}<infile

# 5  
Old 07-03-2007
still does not work... i m using HP Unix
# 6  
Old 07-03-2007
Code:
Temp_Pattern="with_pat.txt"
Temp_No_Pattern="no_pat.txt"
rm -f $Temp_Pattern
rm -f $Temp_No_Pattern
mFlag="1"
while read mLine
do
  if [ "$mLine" = "P1" ]; then
    if [ "$mFlag" = "Y" ]; then
      cat $$Temp >> $Temp_Pattern
    else
      if [ "$mFlag" = "N" ]; then     ### If it is not first time
        cat $$Temp >> $Temp_No_Pattern
      fi
    fi
    mFlag="N"
    rm -f $$Temp
  fi
  echo $mLine >> $$Temp
  mCnt=`echo $mLine | egrep -c '4,4545'`
  if [ $mCnt -ne 0 ]; then
    mFlag="Y"
  fi
done < input_file
if [ "$mFlag" = "Y" ]; then
 cat $$Temp >> $Temp_Pattern
fi
rm -f $$Temp

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk seach and printing a particular pattern

Hi i need a help for this. the output of a command is like below 1 /tmp/x 2.2K /tmp/y 3.2k /tmp/z 1G /tmp/a/b 2.2G /tmp/c 3.4k /tmp/d Now i need to grep for the paths which are in GB..like below 1G /tmp/a/b 2.2G /tmp/c pls suggest me, how can i... (12 Replies)
Discussion started by: kumar85shiv
12 Replies

2. Shell Programming and Scripting

file seach from pulseaudio module list

Hi Guys,I admit my knowledge in this area is awful, :( I'm trying to get the alsa card number from within the text returned by running pactl list cards Alsa card numbers from running aplay -l and above do not match. I'm attempting to get a match on the PA card list with the type of sound... (2 Replies)
Discussion started by: g8jvm
2 Replies

3. Shell Programming and Scripting

Find files and seach limit with grep

Hi, I'm testing nginx-cache-purge and notice that grep searches all file content, and since the cache key is in the second line I would like to limit grep searching. This is the script I'm using: github.com/perusio/nginx-cache-purge The line I would like to change and limit grep search... (5 Replies)
Discussion started by: nfn
5 Replies

4. UNIX for Dummies Questions & Answers

Seach and Replace

Our email address is changing and I need to update 4000 htm pages. I was told I command was something like this Eg) find docs/700_799/ -name "*.htm" -type f | xargs perl -pi -e 's/\/company-information\/legal-and-regulatory/\/legal-and-regulatory/g' Is there away of not changing the date... (1 Reply)
Discussion started by: mchelle_99
1 Replies

5. UNIX for Dummies Questions & Answers

Seach for part of string and replace whole word

I am trying to find words in a text with a certain ending with sed and replace them with themselves but wrapped in tabs ex.: The fish swims in the water. -> searching for -ms ending The fish <tab>swims<tab>in the water. I've been trying all sorts of commands and get either an error... (5 Replies)
Discussion started by: stinnes
5 Replies

6. UNIX for Dummies Questions & Answers

Shell to seach all .mov files in a drive

I want a shell script which does: -> search all .mov files in a drive (say e: drive) which are above 800 mb of size -> Delete the searched .mov files which are above 800mb Thanks, Manoj (4 Replies)
Discussion started by: manoj.b
4 Replies

7. UNIX for Dummies Questions & Answers

How to seach in specific directories?

Hi, How can I perform a search on parent directories and not child? I need to search and gzip files that are in /Home/Parent, but there are subdirectories under Parent ( /Home/Parent/Today). I don't want to gzip anything in the "Today" folder, just stop at the parent. There are about 50... (2 Replies)
Discussion started by: bbbngowc
2 Replies

8. Shell Programming and Scripting

how to seach junk or special char in vi edit

Hi, I have text file which is genrated by some external system(not sure of what is the edirot used) I copied to my Solaris machine and did vi found some strange text like below in one line. Ren\351 Erich I wanted to find all places that contain \351, in vi I am unable search unsing... (2 Replies)
Discussion started by: McLan
2 Replies

9. Shell Programming and Scripting

AWK seach for exact word in certain column

Can anyone help me how I will extract all lines in a file where the word "worker" or "co-worker" in 2nd column exist. There are also word in 2nd column like "workers" or "worker2" but I don't want to display those lines. Appreciate any help in advance! Thank you! (5 Replies)
Discussion started by: Orbix
5 Replies

10. Shell Programming and Scripting

using sed seach replace

Need help on using sed .. search for pattern then delete that occurance globally .. ex. <ad-number>1002877553-01</ad-number> <start-date>01/07/2009</start-date> <end-date>01/07/2009</end-date> <eps-file>c:\adpay\100298452.eps </eps-file> Want to delete this expression ---> ... (2 Replies)
Discussion started by: aveitas
2 Replies
Login or Register to Ask a Question