Get grep to output only 1 no from a file ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get grep to output only 1 no from a file ?
# 1  
Old 03-01-2015
Get grep to output only 1 no from a file ?

I want to finde a no in a fine

My line look like this
Code:
 “SWD_BUILD ?= 207 # can be 0-999- Build nr”

and I only want 1 output. the no 207
My command rigtht now is
Code:
 grep -A0 “SWD_BUILD” Makefile | grep -m1 -Eoe “[0-9][0-9][0-9]|[0-9][0-9]|[0-9]”

But I getSmilie
207
0
999

How to get only the 207 out ??Smilie

Moderator's Comments:
Mod Comment Please use code tags for your code and data, next time

Last edited by vbe; 03-01-2015 at 03:52 PM..
# 2  
Old 03-01-2015
It is quite simple: you use the wrong tool you get the wrong result.

Quote:
Originally Posted by Cillo
Code:
 grep -A0 “SWD_BUILD” Makefile | grep -m1 -Eoe “[0-9][0-9][0-9]|[0-9][0-9]|[0-9]”

But I getSmilie
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:

Code:
sed -n '/^SWD_BUILD \?=/
                 {
                     s/^[^=]*= //
                     s/^\([0-9][0-9]*\).*/\1/p
                 }' /path/to/input.file

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.

I hope this helps.

bakunin
# 3  
Old 03-01-2015
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:
Code:
 grep -A0 "SWD_BUILD" Makefile | grep -m1 -Eoe "[0-9][0-9][0-9]|[0-9][0-9]|[0-9]"|head -n 1

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:
Code:
awk '/SWD_BUILD/{match($0, /[[:digit:]]{1,3}/);print substr($0, RSTART, RLENGTH)}' Makefile

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:
Code:
awk '/SWD_BUILD/{if(match($0, /[[:digit:]]{1,3}/)) print substr($0, RSTART, RLENGTH)}' Makefile

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:
# 4  
Old 03-02-2015
Thanks :)

Thanks Don Cragun, iw works fine SmilieSmilieSmilie
# 5  
Old 03-02-2015
Note1: nawk on Solaris knows neither [[:digit:]] nor {1,3}, use /usr/xpg4/bin/awk instead

Note2: when using GNU awk version < 4 , use awk --re-interval or --posix to make {1,3} work

Note3: mawk also does not know {1,3}

Last edited by Scrutinizer; 03-02-2015 at 08:51 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 03-02-2015
Yes it dos watt I want Smilie
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.

Who to ask ??
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep output to file result not as expected

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)
Discussion started by: green_k
2 Replies

2. Shell Programming and Scripting

Redirect script output to file after grep

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)
Discussion started by: before4
2 Replies

3. Shell Programming and Scripting

How to grep the desired output and output to a file?

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)
Discussion started by: jediwannabe
10 Replies

4. Shell Programming and Scripting

Store grep output file

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)
Discussion started by: radicaled
3 Replies

5. Shell Programming and Scripting

grep words from output file

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)
Discussion started by: darling
5 Replies

6. Shell Programming and Scripting

Grep a pattern given in one file at other file and display its corresponding contents as output.

***************************************** 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)
Discussion started by: abinash
7 Replies

7. Shell Programming and Scripting

grep output of a text file

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)
Discussion started by: buechler66
6 Replies

8. UNIX and Linux Applications

How to redirect grep command output to same file

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)
Discussion started by: picheswa
5 Replies

9. Shell Programming and Scripting

Reading from a text file then /grep/output

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)
Discussion started by: s3rro
7 Replies

10. Shell Programming and Scripting

Redirect grep output into file !!!!!

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)
Discussion started by: hanu_oracle
2 Replies
Login or Register to Ask a Question