Extract values only for certain tags


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract values only for certain tags
# 15  
Old 12-10-2014
Quote:
Originally Posted by junior-helper
It should work if you use {print $2} instead of {print} (print only field 2 instead of whole line), but there will be a leading space.
If you don't want the preceding space, try this:
{sub(/^ /,"",$2); print $2} it means remove a single preceding space from the field 2 + print field 2. In fact it means substitute the preceding space with nothing ("").
If just the values associated with the keywords were wanted (instead of keyword and value), it would be simpler to use the default awk field delimiters and look for the keywords with the colons added:
Code:
awk '$1 == "MSISDN:" || $1 == "IMSI:" {print $2}' Input_file

or:
Code:
awk '$1 ~ "^(MSISDN|IMSI):" {print $2}' Input_file

or if there might be lines where there is no whitespace or could be multiple whitespace characters after the colon, you could use:
Code:
awk -F':[[:blank:]]*' '$1 ~ "^(MSISDN|IMSI)$" {print $2}' Input_file

or:
Code:
awk -F':[[:blank:]]*' '$1 == "MSISDN" || $1 == "IMSI" {print $2}' Input_file


Last edited by Don Cragun; 12-11-2014 at 06:28 AM.. Reason: Remove extraneous }' from middle two awk commands.
This User Gave Thanks to Don Cragun For This Post:
# 16  
Old 12-11-2014
Thank you, Don Smilie
Those are very useful tips/hints and I will try to acquire them Smilie

PS: I think there are two typos in the 2 awk commands in the middle (}'}')
This User Gave Thanks to junior-helper For This Post:
# 17  
Old 12-11-2014
Quote:
Originally Posted by junior-helper
Thank you, Don Smilie
Those are very useful tips/hints and I will try to acquire them Smilie

PS: I think there are two typos in the 2 awk commands in the middle (}'}')
You're correct. I'll fix those two typos in post #15 in this thread.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract multiple values into corresponding variables

Here is my input # MANIFEST.MF Manifest-Version: 1.0 Build-Jdk: 1.6.0 Built-By: CM_TEAM Build_SvnRev: 662789 Build_Number: 13.0.0.0-JDK8 Build_Date: Wed 04/05/2017-20:48:19.17 Archiver-Version: Plexus Archiver Created-By: Apache Maven 3.1.0 Here is the expected output:... (4 Replies)
Discussion started by: kchinnam
4 Replies

2. Shell Programming and Scripting

Extract values in a line using awk

hello all, I need your help in extracting values of some parameter within a line using awk. for example: i have the below line available in a file and i want to extract the values of only CustomerId, s_PackageId and s_HZINumbers in order the result to be as ... (13 Replies)
Discussion started by: nael_najib
13 Replies

3. Shell Programming and Scripting

Extract values

hi I have a line as given below. I need to match "COLUMN_NAME" and get the every third value ie words in between quotes completely (' ') Sample Input - COLUMN_NAME Like '%value%' Or COLUMN_NAME Like '%value%' Or COLUMN_NAME Like '%value value%' Or COLUMN_NAME Like '%value%' OR... (5 Replies)
Discussion started by: Prashanth B
5 Replies

4. Shell Programming and Scripting

Is it possible to extract these values from the output

Hi, I would like to search based of "java" and "-Dplatform.home=" and store these two values in bash variables. ps -xef | grep <pid> wlsuser 15160 15144 0 Feb 20 ? 17:27 /app1/jdk150_07/bin/IA64N/java -server -Xms1536m -Dplatform.home=/app1/bea/weblogic92... (13 Replies)
Discussion started by: mohtashims
13 Replies

5. Shell Programming and Scripting

extract key values

I am parsing a log with key values spread all over in the following fashion: TEST 1 SCHEME 12 SET EMPTY VARLEN SET TEST 1201 PARAM1 EMTY PARAM2 SET SCHEME 12 REFRESH TEST 8 I need to extract test number, my result should be 1 1201 8 I use awk for processing this log and use... (4 Replies)
Discussion started by: migurus
4 Replies

6. Shell Programming and Scripting

to extract specific values twice in a file

Hi Friends, I have a file with the following values.. xyz.txt,12345.xml abc.txt,04567.xml cde.txt,12134.xml I would like to extract all the 2nd column values twice as shown in the example like 12345,12345.xml 04567,04567.xml 12134,12134.xml Please advice!! In the formus one of... (7 Replies)
Discussion started by: techmoris
7 Replies

7. Shell Programming and Scripting

Extract Values from CSV

Hi, I need to extract values from a CSV file based on some conditions as explained below: File format details: 1. each set starts with AAA only 2. number of columns is fixed 3. number of rows per set may vary (as they are having different CCC rows) Now, i need to extract 3rd column of... (3 Replies)
Discussion started by: prvnrk
3 Replies

8. Shell Programming and Scripting

Extract XML Element Values

I have a rather large file with XML-style content. Each line contains one full XML entry. For example: 1:<Message><DNIS>1234</DNIS><UCID>3456</UCID><TransferGroup>XYZXYZ</TransferGroup></Message> 2:<Message><DNIS>9999</DNIS><UCID>2584</UCID><TransferGroup>ABCABC</TransferGroup></Message>... (1 Reply)
Discussion started by: sharpi03
1 Replies

9. Shell Programming and Scripting

how to extract values b/w two delimiters

Hi, Please help me to extrat values b/w two delimiters. $ echo $abc i want to extract the value 12345 b/w %. (5 Replies)
Discussion started by: tsaravanan
5 Replies

10. Shell Programming and Scripting

Extract values from log file

I would like to write a shell script that will parse through a file similar to the sample below. The data in the file is redirected from rsync into a log file. I would like to call a shell script to parse through and pick out the number beside the percent sign inside the parentheses in the last... (5 Replies)
Discussion started by: wdympcf
5 Replies
Login or Register to Ask a Question