awk search and replace in a targeted field instead of $0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk search and replace in a targeted field instead of $0
# 1  
Old 11-14-2012
awk search and replace in a targeted field instead of $0

Hi

I would like to apply this gawk command:
Code:
gawk '{$0=gensub(/\y[a-z_\-]+\y[ |\,\;\:\.]/,"","g"); print}' file

not to the whole $0 but just to the part of $0 that is between:
(a number)"> and </mrk>

Is it possible?

thanks for your help.
# 2  
Old 11-14-2012
Possible.

You could use match for the required regexp and then get the required substring by substr using values of RSTART and RLENGTH and invoke gensub on this substring.

Please post a sample of the input and the desired output for better suggestions.

Last edited by elixir_sinari; 11-14-2012 at 08:28 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 11-14-2012
thanks elixir_sinari
here is an example:
from this:
Code:
<trans-unit id="pt1"><mrk mtype="seg" mid="1">YouuŽ Men’s Sight Sleeved Crew Neck is a baselayer with HoPamive™ technology, offering warmth and moisture transport in warmer conditions.</mrk> <mrk mtype="seg" mid="2">source" /></sdl:seg-defs></trans-unit>

I'd like to obtain this:
Code:
<trans-unit id="pt1"><mrk mtype="seg" mid="1">YouuŽ  Men’s Sight Sleeved Crew Neck</mrk>  <mrk mtype="seg" mid="2">source"  /></sdl:seg-defs></trans-unit>

The idea is to only keep uppercase words.

thanks

---------- Post updated at 07:50 AM ---------- Previous update was at 07:40 AM ----------

this would almost do it:
Code:
{
    match($0,/[[:digit:]]">(.*)<\/mrk/);
    new=gensub(/\y[a-z_\-]+\y[ |\,\;\:\.]/,"","g",substr($0,RSTART,RLENGTH)); 
    print new;
}

but I don't know how to reintegrate the whole line...

---------- Post updated at 07:54 AM ---------- Previous update was at 07:50 AM ----------

almost there:
Code:
#!/usr/bin/awk -f
{
    match($0,/[[:digit:]]">(.*)<\/mrk/);
    new=gensub(/\y[a-z_\-]+\y[ |\,\;\:\.]/,"","g",substr($0,RSTART,RLENGTH)); 
    print substr($0,0,RSTART-1) new substr($0,RLENGTH,length($0));
}

---------- Post updated at 08:01 AM ---------- Previous update was at 07:54 AM ----------

got it!!! sorry for the multiple edits ;-)

Code:
#!/usr/bin/awk -f
{
    match($0,/[[:digit:]]">(.*)<\/mrk/);
    new=gensub(/\y[a-z_\-]+\y[ |\,\;\:\.]/,"","g",substr($0,RSTART,RLENGTH)); 
    new=gensub(/’/,"’s ","g",new);    
    print substr($0,0,RSTART-1) new substr($0,RSTART+RLENGTH,length($0));
}

# 4  
Old 11-14-2012
Without suggesting many changes:
Code:
{
    if(match($0,/[[:digit:]]">(.*)<\/mrk/)) {
     new=gensub(/\y[a-z_\-]+\y[ |\,\;\:\.]/,"","g",substr($0,RSTART,RLENGTH)); 
     print substr($0,1,RSTART-1) new substr($0,RSTART+RLENGTH+1)
    }
}

# 5  
Old 11-14-2012
arggg no, it doesn't work...
The
Code:
rk m

in
Code:
mrk mtype=

is removed though it shouldn't...
any idea why?

and also, I would need to apply the gensub also on strings between (a number)"> and </g
That would do somthing like:
Code:
match(substr($0,offset),/[[:digit:]]">(.*)<\/[g|m]/)

thanks

---------- Post updated at 08:25 AM ---------- Previous update was at 08:08 AM ----------

to sum up I'd like to match any segment between
(a number)"> and </g
or
(a number)"> and </mrk

and apply the changes

is it possible?

thanks

Last edited by louisJ; 11-14-2012 at 09:15 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search and replace the last field

Hi All, Seeking for your assistance on how to search and replace the last field/column. please see sample below: inputfile1.csv ="8923523434",="543623534"="afd23535623",="100"="200" ="8923523431",="543623536"="afd23535626",="101"="201"... (3 Replies)
Discussion started by: poginiks
3 Replies

2. Shell Programming and Scripting

Search for a value and replace other field in the same set

Hello friends, I have huge file with many sets where each "set" has few lines and each set always begins with "Set" in Sq brackets as shown above. # cat file1 (2 Replies)
Discussion started by: magnus29
2 Replies

3. Shell Programming and Scripting

Search field in text file and replace value

Hi there, First of all this is my first post here. Thank you in advance for your help. What I am trying to do is the following. I have a text file where each field of each row is separated by a tabulator. Looks like this: ATOM 1 N HSE A 26 3.033 -10.429 -2.262 1.00 17.07 ... (8 Replies)
Discussion started by: doom4
8 Replies

4. UNIX for Dummies Questions & Answers

Shell script for search and replace by field

Hi, I have an input file with below data and rules file to apply search and replace by each field in the input based on exact value or pattern. Could you please help me with unix script to read input file and rules file and then create the output and reject files based on the rules file. Input... (13 Replies)
Discussion started by: chandrath
13 Replies

5. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

6. Shell Programming and Scripting

Search and replace field?

I have 2 files A.txt and B.txt A.txt 3 fields and separate by a comma some,thing,florida any1,thing1,california some2,thing2,dallas just,fun,kansas B.txt has 8 fields and separate by a comma what,ever,florida-state,,,,,, some,one,dallas_state,,,,,, You will see 3rd fields are the... (5 Replies)
Discussion started by: sabercats
5 Replies

7. Shell Programming and Scripting

Search duplicate field and replace one of them with new value

Dear All, I have file with 4 columns: 1 AA 0 21 2 BB 0 31 3 AA 0 21 4 CC 0 41 I would like to find the duplicate record based on column 2 and replace the 4th column of the duplicate by a new value. So, the output will be: 1 AA 0 21 2 BB 0 31 3 AA 0 -21 4 CC 0 41 Any suggestions... (3 Replies)
Discussion started by: ezhil01
3 Replies

8. Shell Programming and Scripting

Awk Search text string in field, not all in field.

Hello, I am using awk to match text in a tab separated field and am able to do so when matching the exact word. My problem is that I would like to match any sequence of text in the tab-separated field without having to match it all. Any help will be appreciated. Please see the code below. awk... (3 Replies)
Discussion started by: rocket_dog
3 Replies

9. Shell Programming and Scripting

Perl - search and replace a particular field

Hi, I have a file having around 30 records. Each record has 5 fields delimited by PIPE. Few records in the file having Junk characters in the field2 and field4. I found the junk charcter and I tested it and replace the junk with space with the command below perl -i -p -e "s/\x00/ /g"... (1 Reply)
Discussion started by: ramkrix
1 Replies

10. Shell Programming and Scripting

awk search and replace field

I am writing a c++ program that has many calls of pow(input,2). I now realize that this is slowing down the program and these all should be input * input for greater speed. There should be a simple way of doing this replacement throughout my file with awk, but I am not very familiar with awk.... (2 Replies)
Discussion started by: bluejayek
2 Replies
Login or Register to Ask a Question