It is quite simple: you use the wrong tool you get the wrong result.
Quote:
Originally Posted by Cillo
But I get
207
0
999
grep is a tool for extracting LINES out a file, not for extracting certain numbers or other parts of a line. So the first grep has a single line as a result (the "-A0" is superfluous), the second grep has the same line as a result and the "-o" (which i suppose is a GNU-extension and therefore better avoided anyway) matches 1,2 or 3 consecutive numerical digits. Look at your sample line, there are 3 places which match this: the three numbers which the output consists of.
Btw.: the extended regexp "[0-9][0-9][0-9]|[0-9][0-9]|[0-9]" is much more simpler written as a standard regexp: "[0-9][0-9]*[0-9]*".
So, after this lengthy explanation of why that didn't work: what you need is sed:
This looks for lines starting with "SWD_BUILD ?=", all the other lines are silently ignored (the "-n" switch). On these lines the following is done: first, cut off everything from the beginning to the first "="-character and a following blank. The number you are interested in is now at the beginning of the line. In the next step everything not numerical after this initial number is cut off too so that the number remains. This is finally printed.
If we fix the quotes in your sample code and add another element to your pipeline to just take the 1st line of output, you can get what you want with:
But, a three element pipeline for this seems like overkill, and if there is more than one line containing the string SWD_BUILD, it will only show you the results from the 1st line containing that string.
A more efficient alternative, using awk, is:
which will print up to three characters from the 1st string of digits found on every line containing the string SWD_BUILD.
However, if there are lines in Makefile containing SWD_BUILD that do not contain any digits, the above awk command will print an empty line for each of those input lines. If this is a problem, the following version will ignore those lines:
If you want to try either of these on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
Last edited by Don Cragun; 03-01-2015 at 06:56 PM..
Reason: Clean up the English...
This User Gave Thanks to Don Cragun For This Post:
Yes it dos watt I want
But now I face watt I thought was a simple thing. I know this is not the right forum, but maybe you can tel me where to ask then.
Watt I need is to set a Jenkins Environment Variable to this value.
I have running the script from Jenkins and start a Cygwin shell.
I can see that in the cygwin shell the env is set but when I return to the Jenkins shell it is gone.
I have also try "Gawk for Windows" but dit not succeed.
Hi Gurus,
I run command grep ABC file1 > file2 against below file. I got all ABC_xxx in one line in file2. I expect to get multiple lines in file2. If I print result in screen, the result is expected.
thanks in advance
My os is SunOS 5.10 Generic_150400-64 sun4v sparc sun4v
ABC_123
XXXXX... (2 Replies)
i have simple program that generate log file 1 line every sec, i need to do grep for specific record then redirect to another file.
#!/bin/bash
for i in `seq 1 20`;
do
echo $i
sleep 1
done
./test.sh |egrep "5|10|15"
5
10
15
r
./test.sh... (2 Replies)
currently I have process from a raw file to this stage
ALTER TABLE "EXCEL_ADMIN"."TC_TXN_VOID" ADD CONSTRAINT "PK_TC_TXN_VOID" PRIMARY KEY ("TC_TXN_IID")
ALTER TABLE "EXCEL_ADMIN"."TC_TXN_AMT" ADD CONSTRAINT "PK_TC_TXN_AMT" PRIMARY KEY ("TC_TXN_AMT_IID")
ALTER TABLE... (10 Replies)
Hi, I'm trying to store the output from a grep, I just want the file name.
But couldn't find how to do it.
Basically, I just want to grep <etc> * and I want to store the file name.
There is only one file with the what I'm grepping, so storing in a variable o an array its the same.
If someone... (3 Replies)
Hi,
By using shell scripit i have save output in one file. I want to grep two words named CLUSTER and CLUSQMGR from that output file. How to grep that. output file would be having below words
TYPE(QCLUSTER) ALTDATE(2010-05-17)
CLUSTER(QS.CL.MFT1) ... (5 Replies)
*****************************************
Right now i have this current system.
I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg.
"this is the line1"
"this is the line2"
The yyy.txt with lot of lines. eg:
"This is a test message which... (7 Replies)
Hi. I have a unix script 'if' condition that greps through a file and searches for the strings ERROR and WARNING.
if egrep -q 'ERROR|WARNING' myfile.txt; then
When this 'if' condition is true then I want to be able to capture 10 lines before the string and 10 lines after the string. So... (6 Replies)
Hi Everyone,
Can anyone please tell me, how can I redirect the grep command output to same file. I am trying with below command but my original file contains no data after executing the command.
$grep pattern file1 > file1
Kind Regards,
Eswar (5 Replies)
I have a list of words that I want to grep in many files to see which ones have it and which ones dont.
in the text file I have all the words listed line by line, ex:
list.txt:
check
try this
word1
word2
open space
list ..
I want to grep each line one by one.
like I want it to... (7 Replies)
Hi,
I am writing the following code in command prompt it is working fine.
grep ',222,' SAPPCO_20080306.CSV_old > SAPPCO_20080306.CSV_new
But the command is not working in the Shell Script...
##########################################
#!/bin/sh
#... (2 Replies)