1

I have a file that looks like this:

CHROM POS ID REF ALT
22 345 567 A G
22 454 666 T G
23 454 555 C C
23 565 777 G G

And I want to change it to:

CHROM POS ID REF ALT
22 345 567 A G
22 454 666 T G
X 454 555 C C
X 565 777 G G

i.e. In column 1, I want to swap 23 with X. How can I do this?

Thanks.

janos
  • 120,954
  • 29
  • 226
  • 236

2 Answers2

3

In GNU sed you could do like this:

sed -e 's/^23\>/X/' file.txt

Or using perl:

perl -pe 's/^23\b/X/' file.txt

Or using awk:

awk '{ if ($1 == 23) sub("23", "X"); print }' file.txt

If you want to update the file after the replacement, then you can use any of these commands:

sed -i -e 's/^23\>/X/' file.txt
perl -pi -e 's/^23\b/X/' file.txt
janos
  • 120,954
  • 29
  • 226
  • 236
2

Do you have access to any scripting languages? a perl or python script to replace a line start followed by 23 with a line start followed by X would be pretty simple.

see this answer: Find and Replace Inside a Text File from a Bash Command

so something like...

perl -pi -e 's/^23\s/X /g' /tmp/file.txt
Community
  • 1
  • 1
AJAX86
  • 45
  • 6