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
# 1  
Old 02-23-2004
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  
Old 02-23-2004
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  
Old 02-23-2004
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  
Old 02-24-2004
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  
Old 02-24-2004
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  
Old 02-24-2004
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  
Old 02-25-2004
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.
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