Replace character in certain column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace character in certain column
# 1  
Old 09-23-2012
Replace character in certain column

I have a data with 4 columns separete with tab delimiter as following:
Code:
A  B  {123}  E{456}
C  D  {789}  T{159}

My expected result as following: (become 5 columns)
Code:
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 am using the tr command, but it will add another tab delimiter to the 3rd column as well which is I don't wish to, I just want to touch the last column. See my command.
Code:
tr '{' '\t' < $InFile >> $OutFile

Thanks.

Last edited by Franklin52; 09-24-2012 at 09:50 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 09-23-2012
Code:
sed 's/\([^     ]\){/\1 {/' $InFile >> $OutFile

In case it isn't obvious, there are two characters between the [ and the ]: <circumflex> and <tab>.
# 3  
Old 09-23-2012
Code:
sed 's/[\t]*{/\t{/g' infile


Last edited by rdrtx1; 09-23-2012 at 05:30 PM..
# 4  
Old 09-23-2012
Quote:
Originally Posted by rdrtx1
Code:
sed "s/[\t]*{/\t{/g" infile

No. Inside a bracket expression, backslash is a regular character. This script looks for a string of zero or more backslash and t characters before a { and replaces each one it finds with t{. For the given input file, this produces:
Code:
A	B	t{123}	Et{456}
C	D	t{789}	Tt{159}

instead of:
Code:
A	B	{123}	E	{456}
C	D	{789}	T	{159}

# 5  
Old 09-23-2012
Wrongo. Script works fine.
# 6  
Old 09-23-2012
It works fine with GNU sed. It won't work with the sed implementations used by just about any non-Linux platform.

Regards,
Alister
# 7  
Old 09-23-2012
Quote:
Originally Posted by rdrtx1
Wrongo. Script works fine.
Not on OS X. And, not if sed handles bracket expressions as defined by the standards.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Replace certain character at specific place with related character

hello i have file with 100k records and each one has certain value that starts at 28th column and certain value that starts at 88th column e.g. 1st file <25>1234567 ..... <88> 8573785485 i have aditional file with values which are related to value that starts at 88th column of the... (1 Reply)
Discussion started by: dell1520
1 Replies

2. Shell Programming and Scripting

[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... (5 Replies)
Discussion started by: Ophiuchus
5 Replies

3. 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

4. Shell Programming and Scripting

Remove the first character from the fourth column only if the column has four characters

I have a file as follows ATOM 5181 N AMET K 406 12.440 6.552 25.691 0.50 7.37 N ATOM 5182 CA AMET K 406 13.685 5.798 25.578 0.50 5.87 C ATOM 5183 C AMET K 406 14.045 5.179 26.909 0.50 5.07 C ATOM 5184 O MET K... (14 Replies)
Discussion started by: hasanabdulla
14 Replies

5. 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

6. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

7. Shell Programming and Scripting

Replace multiple occurances of same character with a single character.

Hi all, Greetings, I have the following scenario, The contents of main file are like : Unix|||||forum|||||||||||||||is||||||the||best so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it And i need the output in the following form: Unix=forum=is=the=best so=be=on=it ... (3 Replies)
Discussion started by: dipanchandra
3 Replies

8. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

9. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

10. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies
Login or Register to Ask a Question