Search between 2 variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search between 2 variables
# 1  
Old 09-28-2010
Search between 2 variables

Hi Guys... can you tell me how to search between 2 variables ?... Im looking for a script that will move the data between varying words to separate files. I dont have my data on which I need to work right now... but it looks something like this..

start name1
data(a)
data(b)
stop name1
-------------------
start name2
data(c)
data(d)
stop name2
-----------------......... the list continues....what Im trying to do here is to extract the data between 'start name 1' and 'stop name1' and put it to a file and call it name1..Hope you guys can help me with this soon.....many thanks!!!
# 2  
Old 09-28-2010
Try this following:

Code:
flag=0; 
while read line;
do  

    if [[ $flag -eq 1 ]]; 
    then    
        echo $line | grep -qv "stop" 
    	if [[ $? -eq 0 ]]; 
   	then         
   	     echo "$line" >> ${FILENAME}
   	fi;     
    fi; 
    echo $line | grep -q "start"; 
    if [[ $? -eq 0 ]]; 
    then     
        flag=1; 
        FILENAME=$(echo $line | awk '{ print $2; }')
        touch ${FILENAME}
    fi; 
    echo $line | grep -q "stop"; 
    if [[ $? -eq 0 ]]; 
    then     
        flag=0; 
    fi; 
    


done < file.log

May not be efficient but it works Smilie
# 3  
Old 09-28-2010
With filelist for viewing and further use
bash code:
  1. #!/bin/bash
  2. i=0   #### You can remove if you don't want the file list
  3. while read -a L
  4. do
  5.    &#91; "${L[0]}" = start ] && { OUT=${L[1]}; FILES[$i]=$OUT; ((i++)); :>$OUT; continue; }   #### Remove FILES[$i]=$OUT; ((i++)); if you don't want filelist.
  6.    &#91; "${L[0]}" = stop ] && { OUT=""; continue; }
  7.    &#91; -n "$OUT" ] && echo "${L[*]}" >> $OUT
  8. done < infile
  9. ##### You can remove all the following if you don't want the file list
  10. for ((i=0; i<${#FILES[@]}; i++))
  11. do
  12.    echo "------------------"
  13.    echo "File: ${FILES[$i]}"
  14.    cat ${FILES[$i]}
  15. done
Without filelist
bash code:
  1. #!/bin/bash
  2. while read -a L
  3. do
  4.    &#91; "${L[0]}" = start ] && { OUT=${L[1]}; :>$OUT; continue; }
  5.    &#91; "${L[0]}" = stop ] && { OUT=""; continue; }
  6.    &#91; -n "$OUT" ] && echo "${L[*]}" >> $OUT
  7. done < infile
# 4  
Old 09-28-2010
Code:
awk '/^stop/{n=x}n{print>n}/^start/{n=$2}' infile

# 5  
Old 09-28-2010
Quote:
Originally Posted by R!C
extract the data between 'start name 1' and 'stop name1' and put it to a file and call it name1
Code:
awk '/^stop/{f=0}/^start/{FILE=$NF;f=1;next}f{print > FILE }' file



---------- Post updated at 03:55 PM ---------- Previous update was at 03:51 PM ----------

Smilie Scrutinizer Smilie
# 6  
Old 09-28-2010
Code:
$ ruby -00 -ne '$_.scan(/start (.[^\n]*)(.*?)stop/m).each{|x| File.open(x[0],"w"){|f| f<<x[1]} }' file

# 7  
Old 10-04-2010
Many thanks to all for your help!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

2. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

3. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

4. Shell Programming and Scripting

awk - ignore metacharacters, search shell variables

Can I use awk to search for a string, passed from the shell, that might include metacharacters? File1 entries: Bob Marley Jammin (Bonus Track).mp3 File2 entries: Bob Marley Jammin (Bonus Track).mp3 32000 /Music/Bob Marley/ Jammin (Bonus Track).mp3 So far, I have this; $ sed -e... (9 Replies)
Discussion started by: DSommers
9 Replies

5. UNIX for Advanced & Expert Users

use of variables in awk to search for pattern from a file

Hi, I need to extract all the content between two strings stored in two variables "startstring" and "endstring" startstring=hello enstring=world #notworking awk '/$startstring/, $NF ~ /$endstring/ ' file > file2 The above code is not working with variables. It works when actual string... (2 Replies)
Discussion started by: jeanjkj
2 Replies

6. HP-UX

Search environment variables for paths

Hi, I am using the HP machine at the moment and by default I have been setup with the kron shell i.e. my home profile is .kshrc I would like to access a program anywhere on the system so I have added a path and created an environment variable like this: export myvarpath=/a/abc/def/ghij... (3 Replies)
Discussion started by: cyberfrog
3 Replies

7. Shell Programming and Scripting

search and replace using awk with variables

Hi, I have been trying to use awk with variables that needs to search for a pattern and replace it with another pattern, the patterns are supplied in a variable. I have tried several different ways without success and hope that someone can help me. Here are the details echo $UPC 07007457809... (2 Replies)
Discussion started by: jerardfjay
2 Replies

8. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

9. Shell Programming and Scripting

File search variables

I have a remote server that generates files with name format: Daily_Generated_File_11-14-07.txt. The file name remains the same regardless, but the date portion at the end of the name will change (mm-dd-yy). Can someone supply a script that will search for files with the latest date based on... (19 Replies)
Discussion started by: bbbngowc
19 Replies

10. Shell Programming and Scripting

Ksh script - Design ? - Search file and set variables

Hi - I'm trying to think of a clever way to write a shell script (trying to stay w/ ksh as that's what I know the best...) that will resolve the following problem: Problem - On a daily basis I have to email folks who are on-call to remind them. I was hoping to script this out so I could have... (9 Replies)
Discussion started by: littlefrog
9 Replies
Login or Register to Ask a Question