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
# 1  
Old 01-23-2014
Grep for a Number in a String

Hi,

I have variable called fname that has the value M201401151630_P008949.csv.txt

I need to grep and store this in a variable called prcid for the number before ".csv" and the letter "P" excluding all zeros in the beginning.

Desired output 8949

Likewise for M201401151630_P108949.csv.txt

Desired ouput is 108949

Code:
uname -a 
SunOS mymach 5.10 Generic_148888-04 sun4v sparc SUNW,SPARC-Enterprise-T5220

# 2  
Old 01-23-2014
Try

Use nawk on sunos

Quote:
Originally Posted by mohtashims
Hi,

I have variable called fname that has the value M201401151630_P008949.csv.txt

I need to grep and store this in a variable called prcid for the number before ".csv" and the letter "P" excluding all zeros in the beginning.

Desired output 8949

Likewise for M201401151630_P108949.csv.txt

Desired ouput is 108949

Code:
uname -a 
SunOS mymach 5.10 Generic_148888-04 sun4v sparc SUNW,SPARC-Enterprise-T5220

Use this
Code:
$ variable="M201401151630_P08949.csv.txt"

$ awk 'gsub(/.*_P|.*_P[0]+|\.csv.*/,x)' <<<$variable
8949


Last edited by Akshay Hegde; 01-23-2014 at 07:37 AM..
# 3  
Old 01-23-2014
Hello,

Following may help also.

Code:
echo "M201401151630_P008949.csv.txt" | sed 's/\(.*\_P\)\(.*\)\(\.csv.*\)/\2/g'
 
echo "M201401151630_P108949.csv.txt" | sed 's/\(.*\_P\)\(.*\)\(\.csv.*\)/\2/g'


EDIT:

1 more approach with awk as follows.

Code:
echo "M201401151630_P108949.csv.txt" | awk 'gsub(/.*\_P/,X) gsub(/[[:alpha:]]/,Y) gsub(/[[:punct:]]/,Z) 1'


Thanks,
R. Singh

Last edited by RavinderSingh13; 01-23-2014 at 07:14 AM.. Reason: added one more solution
# 4  
Old 01-23-2014
You can also try:
Code:
awk -F"_P" '{print int($2)}' file

This User Gave Thanks to Franklin52 For This Post:
# 5  
Old 01-23-2014
Quote:
Originally Posted by Franklin52
You can also try:
Code:
awk -F"_P" '{print int($2)}' file

Elegant Solution! Franklin52 Smilie
This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 01-23-2014
Quote:
Originally Posted by Franklin52
You can also try:
Code:
awk -F"_P" '{print int($2)}' file

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

# 7  
Old 01-23-2014
This is the output I get:
Code:
$ cat file
M201401151630_P008949.csv.txt
$
$ awk -F"_P" '{print int($2)}' file
8949
$

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