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.
sed 's/^\(...\)A/\1RA/' input_file
>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 !
this command doesnt work for a sample input file like
cat f1
X B C A E F G A I
I had modified it with some assumptions
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:
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:
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.
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
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.