extracting KEY from a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting KEY from a line
# 1  
Old 12-01-2011
extracting KEY from a line

consider a line from a file is stored in a variable called $var
i.e.
Code:
 
var="  KEY `controllingZoneId` (`controllingZoneId`),"

or it can be (i.e without comma at the end)
Code:
var="KEY `requestingUserId` (`requestingUserId`)"


here i need to extract KEY and 1st and 2nd word
i want to know how to extract first word second word and third word fromt the above line independently... in there separate commands (and store them in 3 diffrent variable)...
the above lines may vary with different words.. only KEY will be there as first word..
# 2  
Old 12-01-2011
Code:
~/ $ echo $var |awk -F'( |,)' '{print $1 " - " $2 " - " $3}'
KEY - `requestingUserId` - (`requestingUserId`)

You can other elements to the field separation regex as required
This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 12-01-2011
so does this give
var1=$(echo $var |awk -F'(=| |,)' '{print $1}')
var2=$(echo $var |awk -F'(=| |,)' '{print $2}')
var3=$(echo $var |awk -F'(=| |,)' '{print $3}')
KEY
requestingUserId
requestingUserId

respectively????
# 4  
Old 12-01-2011
Code:
# echo '  KEY `controllingZoneId` (`controllingZoneId`),' | awk '{print $1}' | sed "s/[ \`\(\), ]//g"
KEY
# echo '  KEY `controllingZoneId` (`controllingZoneId`),' | awk '{print $2}' | sed "s/[ \`\(\), ]//g"
controllingZoneId
# echo '  KEY `controllingZoneId` (`controllingZoneId`),' | awk '{print $2}' | sed "s/[ \`\(\), ]//g"
controllingZoneId

---------- Post updated at 05:52 PM ---------- Previous update was at 05:11 PM ----------

The Perl way...
Code:
# perl -le '@x=split/\s+/,"  KEY `controllingZoneId` (`controllingZoneId`),";shift@x;for(@x){s/[`\(\),]//g;print $_}'
KEY
controllingZoneId
controllingZoneId

This User Gave Thanks to balajesuri For This Post:
# 5  
Old 12-01-2011
Code:
$ echo $var
KEY `controllingZoneId` (`controllingZoneId`),
$
$ echo $var | tr -d '`(),' | tr ' ' '\n'
KEY
controllingZoneId
controllingZoneId

This User Gave Thanks to jayan_jay For This Post:
# 6  
Old 12-01-2011
i want to store them in individual variables....!
# 7  
Old 12-01-2011
@jayan_jay: If there are 2 spaces before KEY (" KEY") as mentioned by vivek_d_r, then the output differs:
Code:
# echo '  KEY `controllingZoneId` (`controllingZoneId`),' | tr -d '`(),' | tr ' ' '\n'


KEY
controllingZoneId
controllingZoneId

@vivek_d_r: You can catch the output in each variable like this
Code:
# var1=$(echo '  KEY `controllingZoneId` (`controllingZoneId`),' | awk '{print $1}' | sed "s/[ \`\(\), ]//g")
# echo $var1
KEY

This User Gave Thanks to balajesuri For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Delete lines according to a key words in that line

HI, I have a file A like this: c 1 length 14432 width 3434 temp 34 c 2 length 3343 width 0923 height 9383 hm 902 temp34 c 3 length 938 height 982 hm 9292 temp 23 ... (2 Replies)
Discussion started by: the_simpsons
2 Replies

2. Shell Programming and Scripting

Extracting key/value pairs in awk

I am extracting a number of key/value pairs in awk using following: awk ' /xyz_session_id/ { n=index($0,"xyz_session_id"); id=substr($0,n+15,25); a=$4; } END{ for (ix in a) { print a } }' I don't like this Index + substr with manually calculated... (5 Replies)
Discussion started by: migurus
5 Replies

3. Shell Programming and Scripting

grep - Extracting multiple key words from stdout

Hello. From command line, the command zypper info nxclient return a bloc of data : linux local # zypper info nxclient Loading repository data... Reading installed packages... Information for package nxclient: Repository: zypper_local Name: nxclient Version: 3.5.0-7 Arch: x86_64... (7 Replies)
Discussion started by: jcdole
7 Replies

4. Shell Programming and Scripting

how to find the shortest line which containing a key string?

hi all, suppose a key string: M0271857 and to find all lines containing this key string in a text file which returns multiple lines but i only want the shortest one is there a way to do that? thanks so much! (4 Replies)
Discussion started by: sunnydanniel
4 Replies

5. Shell Programming and Scripting

Extracting Max date for multiple same key

Can any one help me to sort and extract the max date row from multiple same key ? example of input file kEY DATE(YYY-MM-DD) 10 2011-08-01 20 2011-09-02 20 2011-10-01 20 2011-08-02 30 2010-01-20 30 2011-01-20 Out put : 10 2011-08-01co 20 2011-10-01 30 2011-01-20 ... (5 Replies)
Discussion started by: jambesh
5 Replies

6. UNIX for Dummies Questions & Answers

Extracting a Private key from a keystore?

Hi everyone! I know you can extract public keys from a keystore using the keytool command. But what is the process to extract a private key from a jks keystore and import into another jks keystore using keytool? Any guidance would be greatly appreciated! I can't seem to find anything, I do... (0 Replies)
Discussion started by: Keepcase
0 Replies

7. Shell Programming and Scripting

Copying/Extracting from line A to line B

Can any one please help me to copy file content between particualr line numbers. (3 Replies)
Discussion started by: engineer
3 Replies

8. Shell Programming and Scripting

extracting a line based on line number

i want to cut all the entries from the /etc/passwd file in which the uid is> 500 for this i was writing this ,m quiet new to all this.. scripting but on the 6th n 8th line ,, i hav to specify a line number .. to get the commnd working .. but i want to use variable i instead of that ,,... (2 Replies)
Discussion started by: narendra.pant
2 Replies

9. Shell Programming and Scripting

getting the line number by extracting a line

grep "KeyNotFoundException" weblogic.log After running this command the output is abcxyzexceptions.KeyNotFoundException: Person not found How to know ..what is the line number of the above string in the log file. (2 Replies)
Discussion started by: bishweshwar
2 Replies

10. Shell Programming and Scripting

Read line with a single key press...

I would really like to have a script that will accept the key press from the user with out having to press the enter key afterwards. i.e. echo "Press Y to print \c" read YesNo At this point the user has to press the enter key to continue. Is there a way to accept the key press from the... (3 Replies)
Discussion started by: jagannatha
3 Replies
Login or Register to Ask a Question