How can i replace certain columns in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can i replace certain columns in the file
# 1  
Old 06-22-2007
How can i replace certain columns in the file

Can i use sed command to replace certain column in the file

let say i hav

D1254215221542 MANA3DS2OOR454
C1254815221121 MDGA4GH4OOR454
A1254215221522 AFFA4DF4OODS54
S3454815221121 TDTA4GH465R454

I wanted to change only at postition 21 and 22 which is DS,GH,DF and GH
i want find that if that position is GH then change it to KH
so the output should be

D1254215221542 MANA3DS2OOR454
C1254815221121 MDGA4KH4OOR454
A1254215221522 AFFA4DF4OODS54
S3454815221121 TDTA4KH465R454

can i do in in sed command or any other simple command if you have
thanks
# 2  
Old 06-22-2007
You can do something like that :
Code:
sed 's/^\(.\{20\}\)GH/\1KH/'  infile

# 3  
Old 06-22-2007
Round about way! Smilie
Code:
awk '{ if( substr($0, 21, 2) ~ /GH/ ) { printf "%s%s%s\n", substr($0, 0, 20), "KH", substr($0, 23, length($0)) } else { print } }'  filename

# 4  
Old 06-22-2007
Thanks man
i will trying it out
it should work
thanks again
# 5  
Old 06-22-2007
Code:
awk 'BEGIN{FS="";OFS=""}
     $21~"G" && $22 ~ "H"{ $21="K";}
	 {print }
' "file1"

# 6  
Old 06-22-2007
Quote:
Originally Posted by ghostdog74
Code:
awk 'BEGIN{FS="";OFS=""}
     $21~"G" && $22 ~ "H"{ $21="K";}
	 {print }
' "file1"

This solution doesn't work with my version of awk under AIX. With an empty Input File Separator, the field 1 contains the whole record :
Code:
$ awk 'BEGIN{FS="";OFS=""}
>      { printf("$0=<%s>\n$1=<%s>\n", $0, $1) }
> ' rep.txt
$0=<D1254215221542 MANA3DS2OOR454>
$1=<D1254215221542 MANA3DS2OOR454>
$0=<C1254815221121 MDGA4GH4OOR454>
$1=<C1254815221121 MDGA4GH4OOR454>
$0=<A1254215221522 AFFA4DF4OODS54>
$1=<A1254215221522 AFFA4DF4OODS54>
$0=<S3454815221121 TDTA4GH465R454>
$1=<S3454815221121 TDTA4GH465R454>
$

# 7  
Old 06-22-2007
I use GNU awk. Defining FS="" will set every character as individual fields.
output:
Code:
# ./test.sh
D1254215221542 MANA3DS2OOR454
C1254815221121 MDGA4KH4OOR454
A1254215221522 AFFA4DF4OODS54
S3454815221121 TDTA4KH465R454

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare and replace two columns from two files

Hello, I have two text tab delimited files File 1 has 30 columns. I am pasting only first 9 Chr Position Ref Alt Score Gene HGVS_C HGVS_P Coding_Consequence dbSNP 1 17312743 C T 1 Gene1 - ... (2 Replies)
Discussion started by: nans
2 Replies

2. Shell Programming and Scripting

Replace text in columns

I have two files in below formats: cat file1 abc|abcd|10|1020 10|xyz|1010|1020 abc|abcd|10|1020 10|xyz|1010|1020 cat file2 abc|abcd|10|1020 11|xyz|1010|1020 abc|abcd|12|1020 10|xyz|1011|1020 abc|abcd|11|1020 10|xyz|1010|1020 abc|abcd|10|1020 10|xyz|1010|1020 I am generating a... (3 Replies)
Discussion started by: ctrld
3 Replies

3. Shell Programming and Scripting

Replace values in columns

I have the following input format: AA00000712000 -0.17 0.90 -1.04 -0.37 -1.45 -1.13 -0.22 -0.34 -0.55 2.37 0.67 -0.48 -0.48 AA00000712001 0.15 0.03 0.47 0.62 2.04 1.14 0.29 -0.81 0.85 0.53 1.00 -0.10 -0.48 BB00000712000 1.32 -0.47 0.44 0.00 0.98 ... (4 Replies)
Discussion started by: ncwxpanther
4 Replies

4. Shell Programming and Scripting

Match on columns and replace other columns

Hi Friends, I have the following input file cat input chr1 100 200 0.1 0.2 na 1 na nd chr1 105 200 0.1 0.2 1 1 na 98 chr1 110 290 nf 1 na nd na 1 chr2 130 150 12 3 na 1 na 1 chr3 450 600 nf nf na 10 na nd chr4 300 330 1 1 10 11 23 34 My requirement is 1. If $6 is na make $7 nd and... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

Replace text in column1 of a file matching columns of another file

Hi all, I have 2 files: species-names.txt Abaca-bunchy-top-virus ((((Abaca-bunchy-top-virus((Babuvirus((Unassigned((Nanoviridae((Unassigned)))) Abutilon-mosaic-virus ((((Abutilon-mosaic-virus((Begomovirus((Unassigned((Geminiviridae((Unassigned))))... (2 Replies)
Discussion started by: thienxho
2 Replies

6. Shell Programming and Scripting

Replace columns of one file into another

Hi, I have two files that are different in size (column #'s differ). Each file has the exact same 3 starting columns. File 1 has 240 columns while File 2 has 45 columns. So if the first 3 columns are the same, I want to replace columns 83 to 163 from File 1 with columns 18 to 33 from File... (7 Replies)
Discussion started by: kylle345
7 Replies

7. UNIX for Dummies Questions & Answers

Switch and replace columns in a file

HP-UX I have a fixed length file like this 9921190625797AE2560 20091001US20091001@@NEWSITE @@ 20091013001X X 01NEW00DNA00007081 @@ SPRINGFIELD @@ ... (2 Replies)
Discussion started by: rs1969
2 Replies

8. Shell Programming and Scripting

Replace specific columns in one file with columns in another file

HELLO! This is my first post here! By the way, I think it is great that people do this. My question: I have two files, one is a .dilm and one is a .txt. It is my understanding that the .dilm file can be treated as a .txt file. I wrote another program where I was able to manipulate it as if it... (3 Replies)
Discussion started by: mehdib
3 Replies

9. UNIX for Dummies Questions & Answers

Replace columns from File1 with columns from File2

Hi all, I would like to replace some columns from file1 with columns from file2. Currently, I'm able to do it with the following command: awk 'NR==FNR{a=$1;b=$2;c=$3;next;} {$2=a;$4=b;$5=c;print}' file2 file1 > temp mv -f temp file1 First, i make the changes and save it as a temp... (1 Reply)
Discussion started by: seijihiko
1 Replies

10. Shell Programming and Scripting

Replace specific columns

hi All, Thi sis very urgent. I have large files with pipe delimited. For example: 1.txt 1001024|120|9|-0.0|#| 1001025|120|9|#| 1001026|120|9|#| 1001032|120|2|-0.0|#| 1002026|110|9|#| 1002027|110|9|-0.0|#| 1002028|120|1|1.0|#| I need to replace the 4th filed if it is # by |-| my... (2 Replies)
Discussion started by: jisha
2 Replies
Login or Register to Ask a Question