grep middle word between two patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep middle word between two patterns
# 1  
Old 02-08-2010
grep middle word between two patterns

Hi,
I'm currently working on a shell script to automate a backup check on oracle database. My requirement is to grep the words between two delimiters and pass on to a variable..

for ex I have following values in my log file...
Code:
(DB_NAME), (163.24 25), (16/02/10 23:40), (COMPLETED),

I want to grep only the values between opening and closing paranthesis and pass that value to shell variables.
Code:
A='DB_NAME'
B='163.24 25'
C='16/02/10 23:40'
D='COMPLETED'

The values between the delimiters are bound to change based on my backup check, so the check has to be performed based on delimiters - ().

Can someone please help!!

Last edited by Scott; 02-09-2010 at 03:56 AM.. Reason: Please use code tags
# 2  
Old 02-08-2010
Do you want to grep particular line or do you want to process all lines?
# 3  
Old 02-08-2010
How about ...

Code:
[house@leonov] INPUT="(DB_NAME), (163.24 25), (16/02/10 23:40), (COMPLETED),"; \
[............] A=$( echo $INPUT | awk -F "[()]" '{print $2}' ); \
[............] B=$( echo $INPUT | awk -F "[()]" '{print $4}' ); \
[............] C=$( echo $INPUT | awk -F "[()]" '{print $6}' ); \
[............] D=$( echo $INPUT | awk -F "[()]" '{print $8}' ); \
[............] echo "A='$A', B='$B', C='$C', D='$D'"
A='DB_NAME', B='163.24 25', C='16/02/10 23:40', D='COMPLETED'

# 4  
Old 02-08-2010
Code:
#! /bin/ksh

INPUT="(DB_NAME), (163.24 25), (16/02/10 23:40), (COMPLETED), "

echo $INPUT | nawk -F",|, " ' {
        for( i = 65; i <= 68; i++ )
                printf("\"%c\"%s\n", i,"="$(i - 64))
        }' | tr "()" "'"

Output: -

Code:
"A"='DB_NAME'
"B"='163.24 25'
"C"='16/02/10 23:40'
"D"='COMPLETED'



---------- Post updated at 11:35 PM ---------- Previous update was at 08:37 PM ----------

Actually the delimiter is the , not ().

If you want to do the assignement to the variables form the script you could try this: -

Code:
#! /bin/ksh
 
INPUT="(DB_NAME), (163.24 25), (16/02/10 23:40), (COMPLETED), "
 
echo $INPUT | nawk -F",|, " ' {
        for( i = 65; i <= 68; i++ )
                printf("export %c%s\n", i,"="$(i - 64))
        }' | tr "()" "'" > tmp_file
 
. tmp_file 2>/dev/null || echo "Error in export file"
 
echo "A=$A"
echo "B=$B"
echo "C=$C"
echo "D=$D"
 
rm -f tmp_file

Output: -

Code:
A=DB_NAME
B=163.24 25
C=16/02/10 23:40
D=COMPLETED

# 5  
Old 02-09-2010
Computer

Hi All,
Thanks so much for all the replies. Both the suggestions worked for me,
Thats the output I was looking for ..

Thanks!
# 6  
Old 02-09-2010
Actually I have just realised the test of the tmp_file for errors does not work, it needs to be executed in a subshell like: -

Code:
if ! tmp_file >/dev/null 2>&1

Then take any remedial action within the if block.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help needed in excluding certain word patterns

Hi, I need help with following. I need to exclude words that match following patterns a. more than length 4 (example SBRAP) b. contains mixture uppercase and lower case regardless of the length (example GSpD) File contains COFpC MCHX SP SNFCA GEH SBRAP DGICA JPMpE WFCpP GSpD AXL... (5 Replies)
Discussion started by: jakSun8
5 Replies

2. Shell Programming and Scripting

Grep patterns

Hi Experts, I have a log file like this.I need to filter the Index name and elapsed time(only created ). 06:36:39 SQL> create index XYZ_F75 on XYZ 06:36:39 2 ("GRP_ID", "_ID") parallel 64 nologging 06:36:39 3 tablespace XARGS_IDX 06:36:39 4 ; Index created. Elapsed:... (2 Replies)
Discussion started by: navsan420
2 Replies

3. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

4. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

5. Shell Programming and Scripting

Find zipcode with Grep with optional space in middle

Hello everyone. I am new to unix, nice to meet you all. I have a small problem with a question. I have to write a command that finds all the postal codes in a txt file. All postal codes will begin on its own line in the txt file. This is the formal of postal code: "A#A#A#" or "A#A #A#" A... (3 Replies)
Discussion started by: leonmerc
3 Replies

6. UNIX for Dummies Questions & Answers

printing only the middle word between two patterns

How would I print the word "and" between the words "FOO" and BAR" using sed? My file has three words in it FOO and BAR. I only want the word "and". Thanks every one. (7 Replies)
Discussion started by: tigta09
7 Replies

7. UNIX for Dummies Questions & Answers

Is it possible to grep with a newline in the middle of an expression?

I'm hoping that there is a way to do this. I'm sure I won't be able to get the answer I need on the deadline I need it...but at least I'll learn how to solve this problem. I have a file that looks like this: (00:14:25\$ head -27 QNHDSPACEDVR Name: PollDhctAVFSInfo 00:0F:21:4B:00:6A Name:... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. Shell Programming and Scripting

add a word in the middle

I have a file where in I need to add gctunit1/gtdivcompebb1/ after the = sign for example: gtfix31/gctunit_gtdivcompebb1/csclkswcompbypassstepgnnnh = gctunit1/gtdivcompebb1/csclkswcompbypassstepgnnnh (3 Replies)
Discussion started by: pitagi
3 Replies

9. Shell Programming and Scripting

Grep with wildcard in middle of word

How can grep G.*schema give me the result: ${Gacntg_dt}""'"' doesn't G.*schema say give me an unlimited number of characters between G and schema? :confused: (3 Replies)
Discussion started by: danmauer
3 Replies

10. Shell Programming and Scripting

grep multiple patterns + the whole word only

Guys, i used egrep "pattern1|pattern2". But the whole word is searched. But i want the output if only the exact word is matched. i.e the output is got evenif a part of the pattern is matched. I tried the -w opion but its showing usage error. Please help me out on this one. please sent me... (2 Replies)
Discussion started by: meheretoknow
2 Replies
Login or Register to Ask a Question