Pattern Matching and replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern Matching and replacement
# 1  
Old 08-19-2014
Pattern Matching and replacement

Hello Everybody,

I need a help in the below pattern matching and replacement issue
I have a file : emp.txt
Code:
21356 suresh 12/12/2012
23511 ramesh 11/06/2011
31456 biswajit 09/08/2013
53134 archan  06/02/2009

first field:- employee id, 2nd field is name and third field is date of joining

HR gives a list of employees who have been moved out of the company.
Let's suppose i get a text file from hr:-
Code:
ramesh
archan

I need to compare it with second column of the original employee list and which ever matches , employee list will be updated

output file

Code:
21356 suresh 12/12/2012
23511 ramesh_terminated  11/06/2011
31456 biswajit 09/08/2013
53134 archan_terminated  06/02/2009

Thanks
# 2  
Old 08-19-2014
Is this a homework assignment?

What have you tried?

What OS and shell are you using?
# 3  
Old 08-19-2014
No this is not a home work assignment. I am not able to figure it out at all.
I am using bash shell and linux operating system
# 4  
Old 08-19-2014
Please show us what you have tried so far.
# 5  
Old 08-19-2014
Use it & let me know if it is working fine for you:

Code:
file="tmp.txt"   ### this is your input file with all the records
pat_file="term.txt"   ### this is your pattern file.

while read pat_line
do
        sed -i "s/$pat_line/${pat_line}_teminated/g" $file

done < "$pat_file"


Last edited by Don Cragun; 08-19-2014 at 04:14 AM.. Reason: Change COLOR tags to CODE tags.
# 6  
Old 08-19-2014
Check & let me know

Use it & let me know if it is working fine for you:

Code:
file="tmp.txt" ### this is your input file with all the records
pat_file="term.txt" ### this is your pattern file.

while read pat_line
do
sed -i "s/$pat_line/${pat_line}_teminated/g" $file

done < "$pat_file"

# 7  
Old 08-19-2014
Code:
kibou@laptop:~$ cat text.txt 
21356 suresh 12/12/2012
23511 ramesh 11/06/2011
31456 biswajit 09/08/2013
53134 archan  06/02/2009
kibou@laptop:~$ sed 's;\(ramesh\);\1_terminated;' text.txt 
21356 suresh 12/12/2012
23511 ramesh_terminated 11/06/2011
31456 biswajit 09/08/2013
53134 archan  06/02/2009

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

2. Shell Programming and Scripting

Help with Pattern Matching and replacement in Gz files

Hi Techies, I need a help in finding junk characters and remove them from a Datafile. we have a file and it had crores of records like below SGSN_MCC_MNC=01150 but sometime due to the issue with sending server we are getting some junk characters in the middle of data like below ... (6 Replies)
Discussion started by: mahi_mayu069
6 Replies

3. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. Shell Programming and Scripting

Sed for selective pattern replacement

Hi I am having a code snippet grant permission to all user sts|ln|uSe|PSG sajncht|se|Use|PPSPSG psg|ln|use|TSPSG sts_user.Me revoke I need to change all occurance of use (uSe,Use,use) with USE. I am using the following sed command for this sed 's//USE/g' s_sample.txt Output: (7 Replies)
Discussion started by: sudeep.id
7 Replies

6. UNIX for Dummies Questions & Answers

awk pattern replacement

Hi I'm a newbie in unix and I'm having trouble in creating a script. I want to search for a pattern '_good' and insert new lines that contains '_bad', '_med', '_fail' while also ensure that the line contains _good is removed here some of the data UPDATE SCHOOL SET GRADE =... (1 Reply)
Discussion started by: sexyTrojan
1 Replies

7. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

8. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

9. Linux

matching pattern and replacement

Hi I am trying to look for a view name in create view statement and then replace this view name with VW_ in grants line in my ddl file . cat dim_provider.sql | grep -i "create view" | while read f1 f2 f3 f4 f5 f6 f7 f8 f9 do new_vw=` echo "$f3" | cut -d "." -f2... (32 Replies)
Discussion started by: capri_drm
32 Replies

10. UNIX for Dummies Questions & Answers

Pattern Replacement

There is a requirement that i need to replaced a pattern by another pattern in all the files in my entire file system. there are 1000s of file in the system. let the pattern is "calcuta". i have to replace this pattern by "kolkata" in all those files which contain "calcuta". I am only able to... (12 Replies)
Discussion started by: palash2k
12 Replies
Login or Register to Ask a Question