Visit Our UNIX and Linux User Community


Retrieve string from each line in a file based on a pattern in Unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Retrieve string from each line in a file based on a pattern in Unix
# 1  
Old 11-25-2009
Retrieve string from each line in a file based on a pattern in Unix

I have a file which contains several lines. Sample content of the file is as below.

Code:
OK    testmessage    email<test@123>
NOK    receivemessage    email<123@test>
NOK    receivemessage    email(123@test123)
NOK    receivemessage    email<abc@test>

i would like to know by scripting will it be possible to retrive just the mail addresses based on string provided, value is 'OK' or 'NOK'.

If i provide search string as 'NOK', i need the output as,

Code:
123@test
123@test123
abc@test

please note, email addresses are wrapped in either () or <> brackets.

Please let me know your comments.

p.s: i m new to scripting...

Last edited by zaxxon; 11-25-2009 at 10:29 AM.. Reason: code tags
# 2  
Old 11-25-2009
Code:
sed -n "/^NOK/s/.*[(<]\(.*\)[)>]/\1/p" infile
123@test
123@test123
abc@test

Or if your awk supports multi-character file-separators:
Code:
awk -F"[()<>]" '/^NOK/ { print $2 }' infile
123@test
123@test123
abc@test

# 3  
Old 11-25-2009
With sed:

Code:
sed -n 's/^NOK.*[<(]\([^>)]*\)[)>].*/\1/p' infile
123@test
123@test123
abc@test

# 4  
Old 11-25-2009
bash
Code:
read -p "Enter search string: " search
while read -r a b c
do
    case "$a" in
       "$search")
            c=${c/email/}
            echo ${c//[(<)>]/};;
    esac
done <"file"

output
Code:
$ ./shell.sh
Enter search string: NOK
123@test
123@test123
abc@test

# 5  
Old 11-26-2009
I am trying with the below content & it is not working.

Code:
2008-06-17 15:32 delivery 12473185: failure: NOK:_<test123@abc.com>/Thanks/
2008-06-17 15:32 delivery 12473115: success: OK:_<test121@abc.com>/Thanks/
2008-06-17 15:32 delivery 12473125: failure: NOK:_<test124@abc.com>/Thanks/
2008-06-17 15:32 delivery 12473135: failure: NOK:_<test125@abc.com>/Thanks/
2008-06-17 15:32 delivery 12473145: failure: NOK:_(test126@abc.com)/Thanks/
2008-06-17 15:32 delivery 12473155: success: OK:_<test127@abc.com>/Thanks/
2008-06-17 15:32 delivery 12473165: failure: NOK:_<test128@abc.com>/Thanks/

can you please check this?
# 6  
Old 11-26-2009
Try:
for searching OK
Code:
sed -n '/ OK/ { s!.*OK:_[<(]\(.*@.*\)[>)]/Thanks/!\1!p; }' file

for searching NOK
Quote:
sed -n '/ NOK/ { s!.*OK:_[<(]\(.*@.*\)[>)]/Thanks/!\1!p; }' file

Last edited by dennis.jacob; 11-26-2009 at 07:02 AM..
# 7  
Old 11-26-2009
Code:
$ awk -F"[()<>]" '/ OK/ {print $(NF-1)}' urfile
test121@abc.com
test127@abc.com

$ awk -F"[()<>]" '/ NOK/ {print $(NF-1)}' urfile
test123@abc.com
test124@abc.com
test125@abc.com
test126@abc.com
test128@abc.com


Previous Thread | Next Thread
Test Your Knowledge in Computers #861
Difficulty: Medium
Vue.js was created by Evan Ewe.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

awk to insert missing string based on pattern in file

Using the file below, which will always have the first indicated by the digit after the - and last id in it, indicated by the digit after the -, I am trying to use awk to print the missing line or lines in file following the pattern of the previous line. For example, in the file below the next... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

awk - To retrieve an expression from the last line containing a pattern

Hi All, I'm new on this forum, and i'm trying since several days to find out a way to retrieve a expression from the last line containing a pattern. Could you please help me with this ? E.g. The file is containing the following lines 08/05 17:33:47 STAT1 Response(22) is... (4 Replies)
Discussion started by: Antonio Fargas
4 Replies

4. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

7. Shell Programming and Scripting

Split File Based on Line Number Pattern

Hello all. Sorry, I know this question is similar to many others, but I just can seem to put together exactly what I need. My file is tab delimitted and contains approximately 1 million rows. I would like to send lines 1,4,& 7 to a file. Lines 2, 5, & 8 to a second file. Lines 3, 6, & 9 to... (11 Replies)
Discussion started by: shankster
11 Replies

8. Shell Programming and Scripting

how do we retrieve a line from a file in unix

we need to capture a record from a file in to a variable and do modifications to it .. so capturing line by line in a file in to some variable (2 Replies)
Discussion started by: lmadhuri
2 Replies

9. UNIX for Dummies Questions & Answers

(cont) Retrieve line from a file based on a value in specific column

HI, Your help was great: awk -F":" '$5 ~ /^P/{print }' file I would like to know what changes need to be done to this line code, so that I can put it in a shell script and call it as the example below. example: countries that start with chacater 'P' > country P Result: ... (0 Replies)
Discussion started by: efernandes
0 Replies

10. UNIX for Dummies Questions & Answers

Retrieve line from a file based on a value in specific column

Hi, I have a file that has several values seperated by ":" 2006:John:Student:Football:Portugal:Cinema 2006:James:Engineer:Basket:Poland:Theatre 2007:Lucy:Diver:Gymnastic:England:Music 2007:Smith:Plumber:Basket:Spain:Poker I need make a filter based on the 5th field to find countries that... (1 Reply)
Discussion started by: efernandes
1 Replies

Featured Tech Videos