need help in writing a script for extracting fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help in writing a script for extracting fields
# 1  
Old 10-11-2011
Question need help in writing a script for extracting fields

Hi,
I need to extract last character of the field retrieved from the database using select command.
eg:
select event,text from event_data;
o/p: Event1,text1

But I need to extract only '1' from the fields...similarly '2' from Event2,text2 and '3' from Event3,text3 etc., and need to pass these extracted digits to a command.
it will be like :

EventRun -r '1' ( '1' from Event1 of select query) -p 1 ('1' from text1 of select query)
i.e.,
EventRun -r 1 -p 1
Smilie
Can anyone please help me out in this
# 2  
Old 10-11-2011
With expr,
Code:
$ var=Event1,text1
$ expr $var : '.*\([a-zA-Z0-9]\)'
1
$

# 3  
Old 10-11-2011
Data

Quote:
Originally Posted by anchal_khare
With expr,
Code:
$ var=Event1,text1
$ expr $var : '.*\([a-zA-Z0-9]\)'
1
$


I should pass the extracted digit from the fields as input to a command..Event1 and Text1 here are two fields extracted from database.
But I need to pass only the numeric value ( 1 for event1 and 1 for text1) at the end of each filed as input to the command.

EventRun -r 1 -p 1 Smilie
# 4  
Old 10-11-2011
try,

Code:
v=${var##${var%%?}}
EventRun -r $v -p $v

# 5  
Old 10-11-2011
Quote:
Originally Posted by anchal_khare
try,

Code:
v=${var##${var%%?}}
EventRun -r $v -p $v


The part I need to write should be like..
EventRun -r -i 'select $ awk '{print "select event,text from event_data}'

similar to the above I should be able to pass last digits of the extracted data to the above query.Smilie
-r and -i are to be passed dynamically from the query output.
# 6  
Old 10-11-2011
I didn't understand.
The solution can be fit to your requirement. you can off course adjust/re-use it to match with.

If you are sure the last character would always be a digit and want to pass sequentially,
Code:
$ a=T3,B2
$ echo $a | sed 's/.*\([0-9]\),.*\([0-9]\)/\1 \2/'
3 2
$

You can later use it this way

Code:
$ set -- $(echo $a | sed 's/.*\([0-9]\),.*\([0-9]\)/\1 \2/')
$ EventRun -r $1 -p $2

# would actually result
$ EventRun -r 3 -p 2

This User Gave Thanks to clx For This Post:
# 7  
Old 10-11-2011
Quote:
Originally Posted by anchal_khare
I didn't understand.
The solution can be fit to your requirement. you can off course adjust/re-use it to match with.

If you are sure the last character would always be a digit and want to pass sequentially,
Code:
$ a=T3,B2
$ echo $a | sed 's/.*\([0-9]\),.*\([0-9]\)/\1 \2/'
3 2
$

You can later use it this way

Code:
$ set -- $(echo $a | sed 's/.*\([0-9]\),.*\([0-9]\)/\1 \2/')
$ EventRun -r $1 -p $2
 
# would actually result
$ EventRun -r 3 -p 2


Thanks for the Solution :-)
I hope it works..will check it now ..ty again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extracting specific fields from an XML file

Hello All, I have a requirement to split the input.xml file different files and i have tried using earlier examples(where i have posted in the forum), but still no luck Here is my input.xml <jms-system-resource> <name>UMSJMSSystemResource</name> ... (4 Replies)
Discussion started by: Siv51427882
4 Replies

2. Shell Programming and Scripting

UNIX extracting fields

I have one file A.txt which is comma separated and I want to extract first 4 field's in a file and also I want to add one more column in output A.txt in output for all records. A.txt should not be hard coded since I do not filename it may be any file. (1 Reply)
Discussion started by: vamsi.valiveti
1 Replies

3. Shell Programming and Scripting

Pattern Matching and extracting the required fields in Perl

Hi All, I am writing the following Perl Scrip and need your help in Pattern matching : I have the following Shell Script that would read line by line from the file (file_svn) and would inturn calls the Perl Script: #!/bin/bash perl_path="/home/dev/filter"... (2 Replies)
Discussion started by: filter
2 Replies

4. Shell Programming and Scripting

Help in extracting fields from a file

I have an input file with contents like: 203969 OrdAcctCycChg USAGE_DAEMON1 203970 OrdAcctCycChg USAGE_DAEMON2 203971 OrdAcctCycChg USAGE_DAEMON3 203972 OrdAcctCycChg USAGE_DAEMON4 I need to extract variables in first column... (51 Replies)
Discussion started by: Rajesh Putnala
51 Replies

5. Shell Programming and Scripting

Extracting fields from file

I am need to extract a number of values from a file, put have now clue how to do this. The file looks like this: # My file Dest=87;CompatibleSystemSoftwareVersion=2.5300-; Dest=87;ImageVersion=000061f3;SystemSoftwareVersion=2.5300;CDN=http://my.backup.com/download.txt;CDN_Timeout=30; I... (3 Replies)
Discussion started by: MagicDude4Eva
3 Replies

6. Shell Programming and Scripting

Removing LF and extracting two fields

I need some assistance, I am writing a script in bash. I want to do two things: 1/. I want to replace the LF at the end of the RFH  Ø  ¸MQSTR ¸ so I can process the file record by record using a while loop. 2/. I want to extract two fields from each record, they are identified with... (1 Reply)
Discussion started by: gugs
1 Replies

7. Shell Programming and Scripting

Extracting records with unique fields from a fixed width txt file

Greetings, I would like to extract records from a fixed width text file that have unique field elements. Data is structured like this: John A Smith NY Mary C Jones WA Adam J Clark PA Mary Jones WA Fieldname / start-end position Firstname 1-10... (8 Replies)
Discussion started by: sitney
8 Replies

8. Shell Programming and Scripting

extracting fields

Hi, i have a line with several fields (indefinite number of - count varies) separated by colon. Now, i need to pick each field (except the first one) and have it assigned to variable within a loop. In other words, in the first iteration of the loop, the variable must be assigned with 2nd... (2 Replies)
Discussion started by: prvnrk
2 Replies

9. UNIX for Dummies Questions & Answers

Extracting information from text fields.

Dear friends, I'm a novice Unix user and I'm trying to learn the ropes. I have a big task I have to accomplish and I'm convinced Unix can get the job done, I just haven't figured out how. I recently posted on the topic of cutting text between unique text patterns and somebody helped me a great... (24 Replies)
Discussion started by: spindoctor
24 Replies

10. Shell Programming and Scripting

Extracting fields from an output 8-)

I am getting a variable as x=2006/01/18 now I have to extract each field from it. Like x1=2006, x2=01 and x3=18. Any idea how? Thanks a lot for help. Thanks CSaha (6 Replies)
Discussion started by: csaha
6 Replies
Login or Register to Ask a Question