find and copy string in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users find and copy string in a file
# 8  
Old 02-25-2004
Eureka
It mean's that i don't have to work on weekends and that i don't have to wakeup on 3.00AM to come to work on weekdays.
It's frequent the problems with that printer, and this will able them to repeat what they need without help.
Thank you very much once again, Ygor.
# 9  
Old 03-03-2004
Looking for Ygor

Hi there
i'm using this script from Ygor ,where i have made a small change to adapt it to my needs.
It just happens that i'm trying (not very successfuly) to change it a bit.
Right now i have a file , that the script loads 48 lines each time into an array and look for a match on the 6th entry, then prints those 48 lines where the match is found and print the next "xx" lines i indicate to the script.

here it is:
Code:
#!/usr/bin/ksh
echo ""
echo ""
printf "file :"
read sFile
echo ""
echo ""
printf "Client :"
read clientID
echo ""
echo ""
printf "How many more?"
read Qta
qt=$(($Qta * 48))
if [ $clientID ]
then
 awk -v cid="$clientID" -v xx="$qt" '
 {
   arr[++cnt]=$0
   if (cnt==48) {
     if (arr[6] ~ cid) {
       for (idx=1;idx<=48;idx++) {
	 print arr[idx]
       }
       for (idx=1;idx<=xx;idx++) {
	 getline
	 print $0
       }
     }
     cnt=0
   }
 }' /usr/imp/$sFile > /usr/imp/rep.txt
fi

this works wonderfuly.
My problem now, is that some times the user don't know how many more pages in the file he has to print. What he allways know is the first page and last page to repeat.
So, i need to change the script indicating not HOW MANY PAGES MORE, but a second match to be found in the file and then print from the 48 lines where the first match is found to the 48 lines where the second match is found.

Thanks for any help

added code tags for readability --oombera

Last edited by oombera; 03-03-2004 at 11:37 PM..
# 10  
Old 03-03-2004
Try this...
Code:
#!/usr/bin/ksh

printf "\n\nEnter file name : "
read sFile
printf "\n\nStart at Client : "
read clientID
printf "\n\n Stop at Client : "
read endingID

[ "$sFile" ] && [ "$clientID" ] && [ "$endingID" ] || exit

awk -v cid="$clientID" -v eid="$endingID" '
{
  if (NR%48==6 && $0~cid) {
    for(x=NR-5;x<NR;x++)
      print a[x%5]
    cnt=1000000
  }
  if (NR%48==6 && $0~eid)
    cnt=40
  a[NR%5]=$0
}
cnt-->0' /usr/imp/$sFile > /usr/imp/rep.txt

Enjoy!
# 11  
Old 03-03-2004
Once again you have been most kind.
It works perfectly with just one minor adjustment.

#!/usr/bin/ksh

printf "\n\nEnter file name : "
read sFile
printf "\n\nStart at Client : "
read clientID
printf "\n\n Stop at Client : "
read endingID

[ "$sFile" ] && [ "$clientID" ] && [ "$endingID" ] || exit

awk -v cid="$clientID" -v eid="$endingID" '
{
if (NR%48==6 && $0~cid) {
for(x=NR-5;x<NR;x++)
print a[x%5]
cnt=1000000
}
if (NR%48==6 && $0~eid)
cnt=40 --------------------- this should be 43 in my understanding, otherwise it will make the last page 3 lines short
a[NR%5]=$0
}
cnt-->0' /usr/imp/$sFile > /usr/imp/rep.txt

thanks Ygor, i've been learning a lot with you. Thanks for that
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy a string to another file

OS version: RHEL 6.7 Shell : Bash I have a file like below. It has 500K lines. I want to extract TAG_IDs shown in single quote at the end to copied to another file. As if I had copied the TAG_IDs using block select (Column Select) in modern text editor $ cat file.txt UPDATE TAGREF SET... (9 Replies)
Discussion started by: John K
9 Replies

2. Shell Programming and Scripting

Find string in file and find the all records by string

Hello I would like to get know how to do this: I got a big file (about 1GB) and I need to find a string (for instance by grep ) and then find all records in this file based on a string. Thanks for advice. Martin (12 Replies)
Discussion started by: mape
12 Replies

3. Shell Programming and Scripting

Find and copy files based on todays date and search for a particular string

Hi All, I am new to shell srcipting. Problem : I need to write a script which copy the log files from /prod/logs directory based on todays date like (Jul 17) and place it to /home/hzjnr0 directory and then search the copied logfiles for the string "@ending successfully on Thu Jul 17". If... (2 Replies)
Discussion started by: mail.chiranjit
2 Replies

4. Shell Programming and Scripting

Find string in XML file, copy contents of section

I am new, really new to bash scripts. I want to search an XML file for a certain string, say "1234567890" Once found, I want to copy the entire contents from the previous instance of the string "Entity" to the next instance of "/Entity" to a txt file. And then continue searching for the... (4 Replies)
Discussion started by: jrfiol
4 Replies

5. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

6. Shell Programming and Scripting

input a string and copy lines from a file with that string on it

i have a file1 with many lines. i have a script that will let me input a string. for example, APPLE. what i need to do is to copy all lines from file1 where i can find APPLE or any string that i specify and paste in on file 2 thanks in advance! (4 Replies)
Discussion started by: engr.jay
4 Replies

7. Shell Programming and Scripting

find a string and copy the string after that

Hi! just want to seek help on this: i have a file wherein i want to find a string and copy the string after that and paste that other string to a new file. ex: TOTAL 123456 find "TOTAL" and copy "123456" and paste "123456" to a new file NOTE: there are many "TOTAL" strings on that... (12 Replies)
Discussion started by: kingpeejay
12 Replies

8. Shell Programming and Scripting

Copy string from files into new file

I'm trying to copy a string (myame@yahoo.com) from multiple files and save them to a new file. This is what's I've gathered so far: sed 's/string/g' file.txt > output.txt Not sure how to run this on multiple files and extract just the email address found in each file. Any help would be... (2 Replies)
Discussion started by: rdell
2 Replies

9. UNIX for Advanced & Expert Users

How to copy a string to a text file

I am using the following command to email a tex file as an attachment- cat mailtext.txt | elm -s "Subject" emailAddr where content of mailtext.txt is - "Body of email" This will attach foo.txt with the email. My problem is that the file foo.txt is ceated dynamically everytime with a... (5 Replies)
Discussion started by: hpuxlxboy
5 Replies

10. UNIX for Dummies Questions & Answers

need to help to find and copy to a file

I am trying to search for files and copy them into a text file. Can anybody help me how to do that. find /test/sds/data -name "*.*" -mtime -365 -exec ls -altr {} \ this is my find command and want to copy the result to a file. (2 Replies)
Discussion started by: pujars1
2 Replies
Login or Register to Ask a Question