The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find string, then get the next 3 lines in a file lyoncc Shell Programming and Scripting 8 11-07-2007 06:30 AM
need to help to find and copy to a file pujars1 UNIX for Dummies Questions & Answers 2 07-12-2007 09:56 AM
find string in .jar file rakeshou UNIX for Dummies Questions & Answers 8 07-06-2007 03:42 PM
Copy / string. Paulw0t Shell Programming and Scripting 6 03-10-2007 11:26 PM
Looking for command(s)/ script to find a text string within a file wrwelden Shell Programming and Scripting 5 11-23-2006 12:53 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-23-2004
vascobrito vascobrito is offline
Registered User
  
 

Join Date: Oct 2003
Posts: 33
find and copy string in a file

Hello there
I need to find a string in an file, and then copy to a new file from the previous 6 lines to 41 lines after the string.
So, what i need to do , and just don't know how, is to find the string and copy 48 lines where the string would be in the 7th line.
I don't know if i can do it with "sed", but i've readed the man in my hp-ux, and all the threads i could find about manipulating files, and got a lot confused.
Thanks for any help.
  #2 (permalink)  
Old 02-23-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Try using awk
Code:
awk '
   /search string/ {c=42; for(x=NR-6;x<NR;x++) print a[x%6]}
   c>0 {print $0;c--}
   {a[NR%6]=$0}
' file1 > file2
  #3 (permalink)  
Old 02-23-2004
vascobrito vascobrito is offline
Registered User
  
 

Join Date: Oct 2003
Posts: 33
Thanks Ygor
It worked fine. I wich i could understand the script, but i never used awk. I bet it isn't easy to explain.
There's one thing i missed in my question, i'l try to explain why i need this.
I have a file that i send to another place, where there is someone that will print that file. Sometimes, the printer has some problems and he misses parts of the file. The ideia is to create an interface that he can use to repeat those parts that he misses when printing. The reason i need this, is because many times i have to come to work just to make a new file with the parts that he needs, specialy on weekends.(he doesn't have direct access to the Unix system).
This file has 48 lines for each client, so this string (the client number) must be located on the 7th line every 48 lines.
So, when i start reading the file, the first place to search the string will be the 7th line, and the second will be 7+48, and third will be 7+48+48.
I don't want you to make all the work for me, but i don't have any ideia how to get this. I'm trying to get a solution for more then one year.
Many,Many Thanks
  #4 (permalink)  
Old 02-24-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Here is a shell script which you can adapt to do what you want. The awk program loads 48 lines into an array then checks for a match on the 7th entry...
Code:
#!/usr/bin/ksh

printf "Enter Client ID: "
read ClientID
if [ $ClientID ]
then
  awk -v cid="$ClientID" '
  {
    arr[++cnt]=$0
    if (cnt==48) {
      if (arr[7] ~ cid) {
        for (idx=1;idx<=48;idx++) {
          print arr[idx]
        }
      }
      cnt=0
    }
  }' file1 > file2
  #print file2
fi
  #5 (permalink)  
Old 02-24-2004
vascobrito vascobrito is offline
Registered User
  
 

Join Date: Oct 2003
Posts: 33
Nothing i could say would realy express the way i thankyou for your help and your patiance with this. I've looked arround in many threads and you have been helping many like me, that are starting to learn about scripting. so, thanks a lot.
by the way, it works great for me. It's exactly what i need.
thanks again
  #6 (permalink)  
Old 02-24-2004
vascobrito vascobrito is offline
Registered User
  
 

Join Date: Oct 2003
Posts: 33
I don't mean to get abusive, but how can i print those 48 lines where we looked for the match, plus the next 96 lines?
I've tryed some things with the script, but i think i have to learn much more to understand all the script. I understand the sintaxe, but i don't know how to change this.
thanks , once again
  #7 (permalink)  
Old 02-25-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Change the awk program like this...
Code:
  awk -v cid="$ClientID" '
  {
    arr[++cnt]=$0
    if (cnt==48) {
      if (arr[7] ~ cid) {
        for (idx=1;idx<=48;idx++) {
          print arr[idx]
        }
        for (idx=1;idx<=96;idx++) {
          getline
          print $0
        }
      }
      cnt=0
    }
  }' file1 > file2
Hope this means you don't have to work weekends.
Sponsored Links
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 08:30 PM.


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