Visit Our UNIX and Linux User Community


Specific search using sed!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Specific search using sed!
# 1  
Old 08-21-2013
Specific search using sed!

Hi Guys,
I need to grep a specific string between a pattern. I have used sed command in the past in the following fashion:
Code:
set x = ROI_2_AND_mod_AND_name
echo $x | sed 's/.*ROI_\(.*\)_AND.*/\1/'
Returns:
2_AND_mod
I want:
2

But I want just the number "2". All I want is anything between "ROI_ and "_AND" (That is the first AND it sees). Can anybody help?
Thanks in advance.
# 2  
Old 08-21-2013
I don't know if sed supports a non-greedy quantifier, but if Perl is an option, then:

Code:
$
$ echo $x
ROI_2_AND_mod_AND_name
$
$ echo $x | perl -plne 's/.*ROI_(.*?)_AND.*/$1/'
2
$
$

Alternatively, if the characters between "ROI_" and "_AND" always comprise of digit(s), then you could take advantage of that in your sed command.
# 3  
Old 08-21-2013
or awk...
Code:
echo $x|awk -F\_ '/ROI_/ {print $2}'

test:
Code:
ant2:/regies/test $ echo $look4
ROI_2_AND_mod_AND_name
ant2:/regies/test $ echo $look4|awk -F\_ '/ROI_/ {print $2}'
2


Last edited by vbe; 08-21-2013 at 12:38 PM.. Reason: add test result
# 4  
Old 08-21-2013
Code:
echo $x
ROI_2_AND_mod_AND_name

echo $x | sed 's|^ROI_\(.*\)_AND_mod_.*|\1|'
2

# 5  
Old 08-21-2013
try also:
Code:
IFS=_ ; set -A ARR $(echo ROI_2_AND_mod_AND_name) ;  echo ${ARR[1]}

# 6  
Old 08-21-2013
Code:
echo $x | sed 's/.*ROI_\([^_]\).*/\1/'

--ahamed
# 7  
Old 08-21-2013
Quote:
Originally Posted by durden_tyler
I don't know if sed supports a non-greedy quantifier, but if Perl is an option, then:

Code:
$
$ echo $x
ROI_2_AND_mod_AND_name
$
$ echo $x | perl -plne 's/.*ROI_(.*?)_AND.*/$1/'
2
$
$

Alternatively, if the characters between "ROI_" and "_AND" always comprise of digit(s), then you could take advantage of that in your sed command.
Quote:
I was thinking of getting the solution using sed only, I could have used "AND_mod" in my sed statement but that won't be generic one. You are correct that between ROI and AND there will be digits , but it could be "ROI_2_3_AND_mod_AND_name", in that case I would want 2_3 as an answer. Thanks for your effort anyways.
---------- Post updated at 10:50 AM ---------- Previous update was at 10:49 AM ----------

Quote:
Originally Posted by in2nix4life
Code:
echo $x
ROI_2_AND_mod_AND_name

echo $x | sed 's|^ROI_\(.*\)_AND_mod_.*|\1|'
2

Quote:
I thought of this solution but it is not generic enough for me. Infact I did this to get temporary solution.
Thanks for your effort though.
---------- Post updated at 10:54 AM ---------- Previous update was at 10:50 AM ----------

Quote:
Originally Posted by ahamed101
Code:
echo $x | sed 's/.*ROI_\([^_]\).*/\1/'

--ahamed
Quote:
I guess I didn't explain in detail. It could be "ROI_2_3_4_AND_mod_AND_name". I think your solution would give me "4" but instead I want:2_3_4 , that is anything between ROI_ and _AND (The first AND).
Thanks for your effort.

Previous Thread | Next Thread
Test Your Knowledge in Computers #416
Difficulty: Medium
JavaScript supports event-driven, functional, and imperative (including object-oriented and prototype-based) programming styles.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

Search and replace specific positions of specific lines

Hi, I have a file with hundreds of lines. I want to search for particular lines starting with 4000, search and replace the 137-139 position characters; which will be '000', with '036'. Can all of this be done without opening a temp file and then moving that temp file to the original file name. ... (7 Replies)
Discussion started by: dsid
7 Replies

3. UNIX for Dummies Questions & Answers

Search specific string logfile specific date range

Hi, I have logfile like this.. === 2014-02-09 15:46:59,936 INFO RequestContext - URL: '/eyisp/sc/skins/EY/images/pickers/comboBoxPicker_Over.png', User-Agent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko': Unsupported with Accept-Encoding header === 2015-02-09... (8 Replies)
Discussion started by: kishk
8 Replies

4. UNIX for Dummies Questions & Answers

Search for a specific String in a log file for a specific date range

Hi, I have log file which rolls out every second which is as this. HttpGenRequest - -<!--OXi dbPublish--> <created="2014-03-24 23:45:37" lastMsgId="" requestTime="0.0333"> <response request="getOutcomeDetails" code="114" message="Request found no matching data" debug="" provider="undefined"/>... (3 Replies)
Discussion started by: karthikprakash
3 Replies

5. Shell Programming and Scripting

Search specific name in a file and fetch specific entries

Hi all, I have 2 files, One file contain data like this FHIT CS CHRM1 PDE3A PDE3B HSP90AA1 PTK2 HTR1A ESR1 PARP1 PLA2G1B These names are mentioned in the second file(Please see attached second file) as (7 Replies)
Discussion started by: manigrover
7 Replies

6. Shell Programming and Scripting

Urgent request to consider:Search specific name in a file and fetch specific entries

Hi all, I have 2 files, One file contain data like this FHIT CS CHRM1 PDE3A PDE3B HSP90AA1 PTK2 HTR1A ESR1 PARP1 PLA2G1B These names are mentioned in the second file(Please see attached second file) as # Drug_Target_X_Gene_Name:(Where X can be any number (1-1000) (1 Reply)
Discussion started by: manigrover
1 Replies

7. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

8. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

9. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

10. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies

Featured Tech Videos