help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help
# 1  
Old 07-31-2007
help

Hi folks,
I have a file which has 9 column. I want to replace A by RA in 4th column. Remember there are A in 8th column also. I only want to replace A by RA in 4th column. How can I do it? Please help me.
# 2  
Old 07-31-2007
Code:
sed 's/^\(...\)A/\1RA/' input_file

# 3  
Old 07-31-2007
Quote:
Originally Posted by chuchu
Hi folks,
I have a file which has 9 column. I want to replace A by RA in 4th column. Remember there are A in 8th column also. I only want to replace A by RA in 4th column. How can I do it? Please help me.
Code:
>cat file
A B C A E F G A I

>awk '{ if ( $4 == "A" ) { $4 = "RA" } print }' file
A B C RA E F G A I

If this is not what you need,
please post sample input and output ! Smilie
# 4  
Old 07-31-2007
Quote:
Originally Posted by Shell_Life
Code:
sed 's/^\(...\)A/\1RA/' input_file

this command doesnt work for a sample input file like

Code:
cat f1
X B C A E F G A I

I had modified it with some assumptions
Code:
sed 's/A/RA/1' f1

( subject to assumption that there are no other occurrence of the character 'A' before 4th column )

Last edited by matrixmadhan; 07-31-2007 at 03:13 PM.. Reason: Input file
# 5  
Old 07-31-2007
Quote:
Originally Posted by matrixmadhan
this command doesnt work for a sample input file like

Code:
cat f1
X B C A E F G A I

I had modified it with some assumptions
Code:
sed 's/A/RA/1' f1

( subject to assumption that there are no other occurrence of the character 'A' before 4th column )
Matrix, if you read the OPs original post again:
Quote:
Originally Posted by chuchu
Hi folks,
I have a file which has 9 column. I want to replace A by RA in 4th column. Remember there are A in 8th column also. I only want to replace A by RA in 4th column. How can I do it? Please help me.
He has a file with 9 (nine) columns and wants to replace the 4th column.

Your test data has 17 columns, much more than 9 columns.

Also the 'A' in your test data is on the 7th column, not the 4th column.

Your solution:
Code:
sed 's/A/RA/1' f1

replaces only the first occurrence of 'A', ignoring what column it is.

My solution does what the OP is asking.
# 6  
Old 07-31-2007
Quote:
He has a file with 9 (nine) columns and wants to replace the 4th column.

Your test data has 17 columns, much more than 9 columns.

Also the 'A' in your test data is on the 7th column, not the 4th column.
Confusion here !

I assumed data value separated by spaces

data1 space data2 space .... data<n>

something like that.

If the file format is different just by tuning the solution to the needs it should work Smilie
# 7  
Old 07-31-2007
You are right, Matrix, the OP should have clarified the specification.

This situation will continue to happen with the OPs not giving us all details.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question