Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace WORD by Position

Status
Not open for further replies.

RulMorf

Technical User
Jul 19, 2013
14
MX
Hi Guys,

I Need some help Here please.

I have a large file like this:
437493.562 2041538.750 4104.000 0 0 195 1 2FE_04_STALKU_JJHP UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
437493.562 2042413.750 3660.000 0 0 195 1 2FE_04_STALKU_JJHP UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
437493.562 2043313.750 3048.000 0 0 195 1 2FE_04_STALKU_JJHP UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
437493.562 2043963.750 2364.000 0 0 195 1 3FE_04_STALKU_JJHP UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
398743.438 2048738.875 1320.000255 230 0 1 1FE_STALKU_JJHP UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
398743.438 2047538.875 1740.000255 230 0 1 2FE_STALKU_JJHP UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
398743.438 2046438.875 2052.000255 230 0 1 2FE_STALKU_JJHP UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
398743.438 2045213.750 2364.000255 230 0 1 2FE_STALKU_JJHP UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms

I need to add a suffix to the Word between characters 52 to 102 of every line. For example, for line 1 I need to replace FE_04_STALKU_JJHP for FE_04_STALKU_JJHP_MODIF while keeping spacing.

So, my desired output would be:
437493.562 2041538.750 4104.000 0 0 195 1 2FE_04_STALKU_JJHP_MODIF UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
437493.562 2042413.750 3660.000 0 0 195 1 2FE_04_STALKU_JJHP_MODIF UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
437493.562 2043313.750 3048.000 0 0 195 1 2FE_04_STALKU_JJHP_MODIF UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
437493.562 2043963.750 2364.000 0 0 195 1 3FE_04_STALKU_JJHP_MODIF UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
398743.438 2048738.875 1320.000255 230 0 1 1FE_STALKU_JJHP_MODIF UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
398743.438 2047538.875 1740.000255 230 0 1 2FE_STALKU_JJHP_MODIF UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
398743.438 2046438.875 2052.000255 230 0 1 2FE_STALKU_JJHP_MODIF UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
398743.438 2045213.750 2364.000255 230 0 1 2FE_STALKU_JJHP_MODIF UNKNOWN JHP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms

The Problem here is that columns 3 and 4 get together sometimes as you can see in row 5. So, I'm not sure if it's better to try SED, maybe something like:

sed -rni 's/^(.{52})(.* )(.*)/\1\2_MODIF \3/gp' file ( not working ).


Thanks a lot in advance.

 
Why not simply replace "_STALKU_JJHP" with "_STALKU_JJHP_MODIF" ?
 
Wit sed works it too:
Code:
$ sed -r 's/_STALKU_JJHP/_STALKU_JJHP_MODIF/g' rulmorf.txt > rulmorf_out.txt
or
Code:
$ sed -r 's/(_STALKU_JJHP)/\1_MODIF/g' rulmorf.txt > rulmorf_out.txt
 
Hello Mikrom, thank you very much for your help.

The Problem is that the word changes. For example, at the beginning of the file it is FE_04_STALKU_JJHP, then it changes to FE_STALKU_JJHP and
it continues to change as you go down in the file.

Thanks again
 
What about this ?
sed -ri 's/^(.{52})([^ ]*)(.*)/\1\2_MODIF\3/' file

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Almost PHV, thanks a lot.

Now the only Problem is that it displaces the last five columns 6 positions to the right and I need to preserve the position:

394993.438 2022063.750 3612.000255 0 0 1 1FN_23_STALKU_CEGG UNKNOWN EMP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
394993.438 2022776.250 3604.000255 0 0 1 2FN_23_STALKU_CEGG UNKNOWN EMP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
394993.438 2023651.250 3460.000255 0 0 1 2FN_23_STALKU_CEGG UNKNOWN EMP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms

394993.438 2022063.750 3612.000255 0 0 1 1FN_23_STALKU_CEGG_MODIF UNKNOWN EMP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
394993.438 2022776.250 3604.000255 0 0 1 2FN_23_STALKU_CEGG_MODIF UNKNOWN EMP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms
394993.438 2023651.250 3460.000255 0 0 1 2FN_23_STALKU_CEGG_MODIF UNKNOWN EMP TIME Mega_Union_Lit_de_Tabasco_Marino_2012 meters ms

You can't see it but there are 32 blanks between the words FN_23_STALKU_CEGG and UNKNOWN. When I run your SED script I still have 32 blanks between FN_23_STALKU_CEGG_MODIF and UNKNOWN and I need to have
26 blanks ( which is 32-6 <-- The width of the word _MODIF )

Thank you very much
 
So, try this:
Code:
sed -ri 's/^(.{52})([^ ]*)( {6})(.*)/\1\2_MODIF\4/' file

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
That's It Man.

Thank you very much.

You guys are really good with AWK and SED.
 
Even simpler:
Code:
sed -ri 's/^(.{52})([^ ]*)( {6})/\1\2_MODIF/' file

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top