Grepping and replacing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping and replacing
# 1  
Old 10-01-2012
Grepping and replacing

Hey Friends,
Need your help again.

I have input.temp file as follows

Code:
$cat input.temp

Lakme|Beauty Products|Lipstick
L'Oreal|Hair Care|Conditioner
Lakme|Beauty Products|Lip gloss
L'Oreal|Hair Care|Mild Shampoo
Gala|Beauty Products|Mehndi Cones
Lakme|Beauty Products|Eye Shadow
Lakme|Beauty Products|Lip liner
Gala|Beauty Products|Bridal dots
Lakme|Beauty Products|Eye liner
L'Oreal|Hair Care|Special T Shampoo
Lakme|Beauty Products|Mascara
Lakme|Beauty Products|Kajal
Gala|Beauty Products|Temp Tattoos

What we are trying to do is as follows.
I want to grep everything except "L'Oreal" and want to replace manufacture's name and respective product type i.e. Hair care, Beauty products etc with word "Expired"
output would look like follows

$cat output.temp

Code:
Expired|Expired|Lipstick
L'Oreal|Hair Care|Conditioner
Expired|Expired|Lip gloss
L'Oreal|Hair Care|Mild Shampoo
Gala|Beauty Products|Mehndi Cones
Expired|Expired|Eye Shadow
Expired|Expired|Lip liner
Gala|Beauty Products|Bridal dots
Expired|Expired|Eye liner
L'Oreal|Hair Care|Special T Shampoo
Expired|Expired|Mascara
Expired|Expired|Kajal
Gala|Beauty Products|Temp Tattoos

Kindly help.
Anu.

Last edited by Scott; 10-01-2012 at 09:14 AM.. Reason: Code tags
# 2  
Old 10-01-2012
Quote:
Originally Posted by anushree.a
output would look like follows

$cat output.temp

Expired|Expired|Lipstick
L'Oreal|Hair Care|Conditioner
Expired|Expired|Lip gloss
L'Oreal|Hair Care|Mild Shampoo
Gala|Beauty Products|Mehndi Cones
Expired|Expired|Eye Shadow
Expired|Expired|Lip liner
Gala|Beauty Products|Bridal dots
Expired|Expired|Eye liner
L'Oreal|Hair Care|Special T Shampoo
Expired|Expired|Mascara
Expired|Expired|Kajal
Gala|Beauty Products|Temp Tattoos
Why are the highlighted lines not changed in the output?
# 3  
Old 10-01-2012
as per --
Quote:
Originally Posted by anushree.a
I want to grep everything except "L'Oreal" and want to replace manufacture's name and respective product type i.e. Hair care, Beauty products etc with word "Expired"
try this..
Code:
awk -F "|" '$1 !~ /Oreal$/{$1="Expired";$2=$1}1' OFS="|" file

as per your expected output.. try this..
Code:
awk -F "|" '$1 !~ /Oreal$|^Gala/{$1="Expired";$2=$1}1' OFS="|" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grepping using -w and dashes (-)

I have a script to sort a list of arbitrary hosts and determine if they are supported by grepping them into a master supported list. I cut all the suffixes of the hosts in the arbitrary list, leaving the "short" hostname if you will, then grep -w them into the master list. For example: ... (1 Reply)
Discussion started by: MaindotC
1 Replies

2. Shell Programming and Scripting

grepping by digit

Hi all, Need your help here. I have a file with thousand of lines, as shown in example below KDKJAA 98324 OIDSAJ 324 KJAJAK 100 KJKAJK 89 JOIJOI 21 JDKDJL 12 UOIUOD 10 UDUYDS 8 UIUHKK 6 I would like to grep using... (5 Replies)
Discussion started by: masterpiece
5 Replies

3. UNIX for Dummies Questions & Answers

grepping a variable

I need to pass a parameter that will then be grepped. I need it to grep /paramater and then have a space so if 123 was passed my grep would be grep '/123 ' sample.log this works fine from the command line but when i try and set it searchThis="/$2 " and then run grep $searchThis... (6 Replies)
Discussion started by: magnia
6 Replies

4. Shell Programming and Scripting

Please help on grepping

Hi All, I have a log file and I want to parse the logfile with a script.A sample text is shown below: I would grep on "return code " on this file. Any idea how the lines above and below the grep patterns could also be extracted. Thanks! nua7 The runLoggingInstall return code is 0... (3 Replies)
Discussion started by: nua7
3 Replies

5. Shell Programming and Scripting

Grepping issue..

I found another problem with my disk-adding script today. When looking for disks, I use grep. When I grep for the following disk sizes: 5242880 I also pick up these as well: 524288000 How do I specifically pick out one or the other, using grep, without resorting to the -v option? ... (9 Replies)
Discussion started by: LinuxRacr
9 Replies

6. Shell Programming and Scripting

grepping around

Using shell scripts, I use grep to find the word “error” in a log file: grep error this.log. How can I print or get the line 3 lines below the line that word “error” is located? Thanks in advance for your response. (9 Replies)
Discussion started by: cbeauty
9 Replies

7. UNIX for Dummies Questions & Answers

Grepping for strings

Hello. I have a dir of 1500+ dir. In these dirs is a file host, with a tag <x_tag>. I need to : 1. grep for all dir that contain this host file that contain <x_tag> 2. print a list of these host files containing <x_tag> is this better to egrep this? (5 Replies)
Discussion started by: t4st33@mac.com
5 Replies

8. UNIX for Dummies Questions & Answers

grepping for numbers in between

Ok here's the situation I have a log like the following: (S0) Time: 124937.326033 - SendingTime: '20041108-12:49:29' (S0) Time: 124937.389946 - SendingTime: '20041108-12:49:29' (S0) Time: 124937.462494 - SendingTime: '20041108-12:49:29' (S0) Time: 124937.551056 - ... (1 Reply)
Discussion started by: fusion99
1 Replies

9. UNIX for Dummies Questions & Answers

grepping for a sentence

Can you grep for a sentence. I have to search logs everyday at work and I was wondering if I could search for a string of words instead of just one. for example, if I had to find this sentence: "Received HTTP message type" How would I grep it (2 Replies)
Discussion started by: eloquent99
2 Replies

10. UNIX for Dummies Questions & Answers

grepping

Is there a way to grep for something and then print out 10 lines after it. for example if I want to grep for a word, then output the following 10 or whatever number of lines after the word. (5 Replies)
Discussion started by: eloquent99
5 Replies
Login or Register to Ask a Question