[Solved] Replace character in 3rd column and leave 1rst and last


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Replace character in 3rd column and leave 1rst and last
# 1  
Old 02-01-2014
[Solved] Replace character in 3rd column and leave 1rst and last

Hello to all,

I have the following text where columns are separated by spaces. I want to have the 3rd column separating
3 strings with 2 "_" in the format below:

LeftSring_CentralString_RightString

So, in 3rd column I want to replace all "_" with "-", except the first and last "_"

The input file is (in red "_" that should be replaced):
Code:
573839 12737 XFFK_UUD-KKDD_JDUDU_PPWI28
24300123 9927827 LUUUEO_OP-MJJF_JJI98_5526_TTTKC
4429999 988601 UDDYDYY_AAAABBNV_RWYYY
7878 92222 HHDRR_IPd.koo_ljjdLPP-IIDUUDD.OPPE_T23JJK

and the output desired (in orange new character "-"):
Code:
573839 12737 XFFK_UUD-KKDD-JDUDU_PPWI28
24300123 9927827 LUUUEO_OP-MJJF-JJI98-5526_TTTKC
4429999 988601 UDDYDYY_AAAABBNV_RWYYY
7878 92222 HHDRR_IPd.koo-ljjdLPP-IIDUUDD.OPPE_T23JJK

May somebody help to do this with awk or sed?

Thanks in advance
# 2  
Old 02-01-2014
What have you tried?
# 3  
Old 02-03-2014
Hello Don,

Sorry for my late response. I've tried with gensub as below, but is replacing all "_" with "-" in 3rd column
and I don't want to replace the first "_" and last "_", only replace from the 2nd underscore to n-1 underscore.

Code tried:
Code:
awk '{print $1, $2, gensub(/_/,"-",g,$3) }'
573839 12737 XFFK-UUD-KKDD-JDUDU-PPWI28
24300123 9927827 LUUUEO-OP-MJJF-JJI98-5526-TTTKC
4429999 988601 UDDYDYY-AAAABBNV-RWYYY
7878 92222 HHDRR-IPd.koo-ljjdLPP-IIDUUDD.OPPE-T23JJK

For the output I'm getting above, the dashes in red should not be replaced from underscore to dash. The output desired below:
Code:
573839 12737 XFFK_UUD-KKDD-JDUDU_PPWI28
24300123 9927827 LUUUEO_OP-MJJF-JJI98-5526_TTTKC
4429999 988601 UDDYDYY_AAAABBNV_RWYYY
7878 92222 HHDRR_IPd.koo-ljjdLPP-IIDUUDD.OPPE_T23JJK

Maybe someone could help me with this.

Thanks
# 4  
Old 02-03-2014
Code:
$ awk -F"_" ' { printf("%s",$1 FS $2);for(i=3;i<NF;i++) printf("%s","-"$i); printf("%s\n",FS NF) } ' file
573839 12737 XFFK_UUD-KKDD-JDUDU_4
24300123 9927827 LUUUEO_OP-MJJF-JJI98-5526_5
4429999 988601 UDDYDYY_AAAABBNV_3
7878 92222 HHDRR_IPd.koo-ljjdLPP-IIDUUDD.OPPE_4

This User Gave Thanks to anbu23 For This Post:
# 5  
Old 02-03-2014
I'm not familiar with gensub(). Assuming that there are at least 2 underscores in field 3, you could try something like:
Code:
awk '
{       n = split($3, x, /_/)
        printf("%s %s %s_", $1, $2, x[1])
        for(i = 2; i <= n; i++)
                printf("%s%s", x[i], i == n ? "\n" : i == (n - 1) ? "_" : "-")
}' input

This should work even if there are underscores in field 1 or field 2.

If there will never be any underscores in field 1 or field 2 and there are at least two underscores in field 3, anbu23's suggestion should also work if you change the last printf from printf("%s\n",FS NF) to printf("%s\n",FS $NF).

If you want to run either of these script on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of the default /usr/bin/awk.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 02-03-2014
Thanks so much anbu23 and Don for your solution and fix anbu´s code. It worked so nice!

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Extract First character in fourth column

Hi Experts, I am new to UNIX. One of my file records are like below 220 IN C/A 515013 NULL NULL 220 IN C/A 515017 NULL NULL 225 IN C/A 333701 NULL NULL 225 IN C/A 515034 NULL NULL 225 IN C/A 499201 NULL NULL 225 IN C/A 499202 NULL NULL The above mentioned records delimiter is... (4 Replies)
Discussion started by: suresh_target
4 Replies

2. Shell Programming and Scripting

Replace a character of specified column(s) of all rows in a file

Hi - I have a file "file1" of below format. Its a comma seperated file. Note that each string is enclosed in double quotes. "abc","-0.15","10,000.00","IJK" "xyz","1,000.01","1,000,000.50","OPR" I want the result as: "abc","-0.15","10000.00","IJK" "xyz","1,000.01","1000000.50","OPR" I... (8 Replies)
Discussion started by: njny
8 Replies

3. Shell Programming and Scripting

sed replace one space and leave other spaces untouched

Hi Friends, I looked up online, but couldn't figure out a proper solution. I have an input file where the columns are separated by multiple spaces and the column content is separated by single space. For example, Chr1 hello world unix is fun In the above example, chr1 is first... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

How to replace a character in a specific column in a file?

This is a file that I have test line 1 (55) ) test line 2 (45) ) I would like to change all the parens in position 1 of this file to a ); i only want to check position 1 in every line of the file. I have tried different varations of sed, but cannot seem to be able to limit it to... (1 Reply)
Discussion started by: JoeG
1 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Replace NA with column mean

Hi experts, I'm looking for a unix solution to replacing "NA" within a matrix with the mean of the column: $cat file ID a b c d day 10 5 100 50 cat 20 6 200 50 dog NA 8 NA 50 car 15 NA NA ... (3 Replies)
Discussion started by: torchij
3 Replies

6. Shell Programming and Scripting

[Solved] Find and replace till nth occurence of a special character

Hi, I have a requirement to search for a pattern in each line in a file and remove the in between words till the 3rd occurrence of double quote ("). Ex: CREATE TABLE "SCHEMANAME"."AMS_LTV_STATUS" (Note: "SCHEMANAME" may changes for different schemas. Its not a fixed value) I need to... (2 Replies)
Discussion started by: satyaatcgi
2 Replies

7. UNIX for Dummies Questions & Answers

Replace character in certain column

I have a data with 4 columns separete with tab delimiter as following: A B {123} E{456} C D {789} T{159} My expected result as following: (become 5 columns) A B {123} E {456} C D {789} T {159} Is there any way to separate the 4th column to 2 field by using the tab delimiter? I... (6 Replies)
Discussion started by: newbie2011
6 Replies

8. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

9. Shell Programming and Scripting

[Solved] sed : last character replace

Hi all , I have to write a shell script that takes a number as input , like 123 and the output will be 6 ,i.e the output will be the sum of digits of the input. I have an idea as follows, echo "123"|fold -1|tr '\n' '+'|bc But the problem is after " echo "123"|fold -1|tr '\n' '+' "... (5 Replies)
Discussion started by: M.Choudhury
5 Replies

10. UNIX for Advanced & Expert Users

if 4th and 5th character of sting -ge 45 then add 1 to 3rd character

I want to know how to, given a string like W87151WR71C, if the 4th and 5th character (in this case 15) are greater than 45, then to add 1 to the 3rd character (in this case 7) and assign the revised string the variable name MODSTRING. Thanks in advance. This is ultimately to grab info from... (6 Replies)
Discussion started by: glev2005
6 Replies
Login or Register to Ask a Question