How to get a part of the line(need help in using grep)
Hi, Suppose, This is a part of a file <filename> . I Need to get the value of SERVICE_NAME from this line… The field SERVICE_NAME can be anywhere in that line..it may not be in the same order… I tried like this… o/p: project_db1 but It should not depend on the field number as 11th or 10th …as It may differ in some cases… So it should depend on the field name “SERVICE_NAME” only.
So, I tried like this... But its showing the correct o/p only if the SERVICE_NAME filed is the last one. But there is a chance of changing the order of the fields so that the SERVICE_NAME field will be in 3rd place in the order.
Can u please help me in this…
Moderator's Comments:
Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. Also please refrain from overly use of font formattings. So please remove the formattings or at least add code tags.
Last edited by Dpu; 04-06-2011 at 06:32 AM..
Reason: code tags
First off, "grep" isn't the right tool: "grep" will always return whole lines, but not parts of it. You need "sed" (or "awk" or some other text filter) to do what you want.
First, lets describe your task: You want to find a line starting with "DBconnection", then extract the "word" following the expression "SERVICE_NAME=". So you have to find out what could terminate such a "word". It seems that there must follow a closing bracket, preceeded by maybe whitespace.
First try, only find the right line:
"sed" will not display any read lines save for lines explicitly asked to display. We have one rule, which applies to every line starting with "DBconnection" (notice that this is case sensitive) and which simply prints the line (the "p" command). So far "sed" is doing the same as "grep", but we have made sure we "hit" the right line with our command.
Second try, extract the word:
You see we have just added to rules to what should be done with lines found before printing them: first we delete everything from the start of the line up to "SERVICE_NAME=" (to be precise: we [s]ubstitute nothing for it), then we delete any (optional) whitespace followed by a closing bracket, followed by any other character(s) to the end of the line. The only thing remaining is the word after "SERVICE_NAME", which we print.
(Replace <spc> and <tab> with literal spaces/tab characters.)
Hi,
I have a file with thousands of lines as below
INSERT INTO T_DIM_CLNT(CLNT_KY,CLNT_OBJ_ID,ISI_CLNT_ID,OPERN_ID,CLNT_NM,PRMRY_SIC_CD,PRMRY_SIC_DSC,RET_AGE_NBR,REC_CRT_TS,REC_DATA_EXTRC_TS,ETL_LOG_KY)
VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)... (5 Replies)
I need some help. I would like to read in a text file.
Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01.
Is this possible? any help is appreciated. (2 Replies)
Hi,
I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this:
Name =A
xxxxxx
yyyyyy
zzzzzz
aaaaaa
bbbbbb
Value = 57
This is necessary because in a file there are written more lines which... (6 Replies)
Hi I just started on GNU Grep with regex and am finding it very challenging and need to ask for help already...
here is the problem, I have a page (MYFILE) which consists of the following....
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden"... (2 Replies)
Hello All. I have an output file which contains the phrase "Total DFT Energy =" and then a number. This occurs many times in the output file, and what I want is to pipe the numbers (which are all different) to a file so I can plot them. How do I grep "Total DFT Energy =" and then get the numbers... (3 Replies)
How do I print only the values of the variables from set or env.
For example I have the lines
USER=USER
XYZ='text'
SHELL=/bin/bash
How do I print only the part after or starting from =
Let's say I have those lines in a list X
If I run
Y = echo $X | grep -o "=*$"
Y won't contain... (3 Replies)
Hi,
In a file FILE, there is a line like this one
MOLECULE C2 H2
I want to extract "C2 H2". I can do it in two step in a script :
VARIABLE="`grep MOLECULE FILE`" # Assign "MOLECULE C2 H2"
VARIABLE=`echo ${VARIABLE/"MOLECULE "}` # Remove "MOLECULE ".
Then,
$echo $VARIABLE
gives
C2... (6 Replies)
Hi how do i grep only a part of the line from a file from all the lines.
file:
asdfgh 543212376 nag
lkjh abhilash 543757858
How do i grep and print only 543212376 and 543757858
Can i grep something like 543* and print only that. (3 Replies)