Grep for a Number in a String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep for a Number in a String
# 8  
Old 01-23-2014
Quote:
Originally Posted by mohtashims
Does not seem to yield the right output for fname=M201401151630_P008949.csv.txt.

Code:
pid=$(awk -F"_P" '{print int($2)}' $fname)
        echo "PID is : $pid"
Output: PID is 0

Frank's solution works, let me confirm first $fname is what in your program ? variable containing string ? or variable containing filename ? please do check don't blame

Code:
pid=$(awk -F"_P" '{print int($2)}' <<<$fname)

Right usage :
Code:
$ fname=M201401151630_P008949.csv.txt.

$ pid=$(awk -F"_P" '{print int($2)}' <<<$fname)

$ echo $pid
8949

This User Gave Thanks to Akshay Hegde For This Post:
# 9  
Old 01-23-2014
Quote:
Originally Posted by Akshay Hegde
Frank's solution works, let me confirm first $fname is what in your program ? variable containing string ? or variable containing filename ? please do check don't blame

Code:
pid=$(awk -F"_P" '{print int($2)}' <<<$fname)

Right usage :
Code:
$ fname=M201401151630_P008949.csv.txt.

$ pid=$(awk -F"_P" '{print int($2)}' <<<$fname)

$ echo $pid
8949

fname is a variable containing String not a filename.

Please suggest !!

Last edited by mohtashims; 01-23-2014 at 12:03 PM..
# 10  
Old 01-23-2014
Quote:
Originally Posted by mohtashims
fname is a variable containing String not a filename.

Please suggest !!


Either
Code:
pid=$(awk -F"_P" '{print int($2)}' <<<$fname)

Or
Code:
$ pid=$(echo $fname | awk -F"_P" '{print int($2)}')

# 11  
Old 01-23-2014
A sed command that may help as well

Sort of clumsy but may help:

Code:
echo M201401151630_P008949.csv.txt |sed -e 's/\(.*\)_P0*//g' -e 's/.csv.txt//g'


Code:
8949

Code:
echo M201401151630_P118949.csv.txt |sed 's/\(.*\)_P0*//g' |sed 's/.csv.txt//g'

Code:
118949

# 12  
Old 01-23-2014
Quote:
Originally Posted by Akshay Hegde
Either
Code:
pid=$(awk -F"_P" '{print int($2)}' <<<$fname)

Or
Code:
$ pid=$(echo $fname | awk -F"_P" '{print int($2)}')

Does not work
Code:
Imagebash-3.2$ echo M201401120252_P004640.csv | awk -F"_P" '{print int($2)}'
0

Code:
bash-3.2$ awk -F"_P" '{print int($2)}' <<<M201401120252_P004640.csv
0

Image
# 13  
Old 01-23-2014
Quote:
Originally Posted by mohtashims
Does not work
Code:
Imagebash-3.2$ echo M201401120252_P004640.csv | awk -F"_P" '{print int($2)}'
0

Code:
bash-3.2$ awk -F"_P" '{print int($2)}' <<<M201401120252_P004640.csv
0

Image
2nd one is wrong usage again, you are on solaris I said in #2 post that you have to use nawk

change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk

--edit---

Code:
echo M201401120252_P004640.csv | /usr/xpg4/bin/awk -F"_P" '{print int($2)}'

Code:
echo M201401120252_P004640.csv | /usr/xpg6/bin/awk -F"_P" '{print int($2)}'

Code:
echo M201401120252_P004640.csv | nawk -F"_P" '{print int($2)}'


Last edited by Akshay Hegde; 01-23-2014 at 12:59 PM..
This User Gave Thanks to Akshay Hegde For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

Grep lines for number greater than given number

Hello, I am newbie to bash scripting. Could someone help me with the following. I have log file with output as shown below **************************LOG************************* 11/20/2013 9:11:23.64 Pinging xx.xx.xx.xx with 32 bytes of data: 11/20/2013 9:11:23.64 Reply from xx.xx.xx.xx:... (4 Replies)
Discussion started by: meena_2013
4 Replies

3. Shell Programming and Scripting

Grep string in files and list file names that contain the string

Hi, I have a list of zipped files. I want to grep for a string in all files and get a list of file names that contain the string. But without unzipping them before that, more like using something like gzcat. My OS is: SunOS test 5.10 Generic_142900-13 sun4u sparc SUNW,SPARC-Enterprise (8 Replies)
Discussion started by: apenkov
8 Replies

4. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

5. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

6. Shell Programming and Scripting

Grep for a string and then grep using a string from that result

Hello, Thanks in advance for the query. There is a log file abcd.log which has multible line like this. "hello1" , "hello2", "hello3" , "hello4" , "hello5" I want to grep for the lines which has "hello4" & "hello5" and use "hello2" to grep the same log file again. All these should... (8 Replies)
Discussion started by: kzenthil
8 Replies

7. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

8. UNIX for Dummies Questions & Answers

| help | unix | grep - Can I use grep to return a string with exactly n matches?

Hello, I looking to use grep to return a string with exactly n matches. I'm building off this: ls -aLl /bin | grep '^.\{9\}x' | tr -s ' ' -rwxr-xr-x 1 root root 632816 Nov 25 2008 vi -rwxr-xr-x 1 root root 632816 Nov 25 2008 view -rwxr-xr-x 1 root root 16008 May 25 2008... (7 Replies)
Discussion started by: MykC
7 Replies

9. Shell Programming and Scripting

find out line number of matching string using grep

Hi all, I want to display line number for matching string in a file. can anyone please help me. I used grep -n "ABC" file so it displays 6 ABC. But i only want to have line number,i don't want that it should prefix matching context with line number. Actually my original... (10 Replies)
Discussion started by: sarbjit
10 Replies

10. Shell Programming and Scripting

grep the string with the line number

Dear Masters, Here i have some doubts can anyone clarify?. Is it possible to grep the lines by specifying the line numbers. I know the line number which i want to grep. example: grep 40th line filename grep 50th line filename Need ur comments. (4 Replies)
Discussion started by: salaathi
4 Replies
Login or Register to Ask a Question