The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to read record by record from a file in unix raoscb UNIX for Dummies Questions & Answers 1 05-16-2008 07:30 AM
Script to search a bad record in a file then put the record in the bad file shilendrajadon Shell Programming and Scripting 2 12-28-2007 10:02 AM
Script to search a bad record in a file then put the record in the bad file shilendrajadon UNIX for Advanced & Expert Users 1 12-28-2007 10:00 AM
splitting a record and adding a record to a file rsolap Shell Programming and Scripting 1 08-13-2007 02:58 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-03-2007
xtravel xtravel is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 7
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 (permalink)  
Old 07-03-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398

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

  #3 (permalink)  
Old 07-03-2007
xtravel xtravel is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 7
the record file is very big, so i get error "cannot be longer than 3,000 bytes" when using awk
  #4 (permalink)  
Old 07-03-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,928

Code:
{ rm infile && sed -n '
/P1/ b block
H
$ b block
b
:block
x
/4,4545:/w temp
/4,4545:/!p'>infile;}<infile

  #5 (permalink)  
Old 07-03-2007
xtravel xtravel is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 7
still does not work... i m using HP Unix
  #6 (permalink)  
Old 07-03-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695

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

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0