![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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. |
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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 |
|
|||||
|
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
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|