Bucco
Bucco

Reputation: 11

Terminal Editing File to Remove Certain Characters

I have a couple of files which have a format of something like this:

TCTCTGGAAAGGGACGCCTGGGAGG   10
AAAAATACATTCTAACCTCGGCGT    1
TAATTTCATCAATATATCAATG  1
(etc...)

I want to remove everything after the space so that I only get this in the end:

TCTCTGGAAAGGGACGCCTGGGAGG
AAAAATACATTCTAACCTCGGCGT
TAATTTCATCAATATATCAATG
(etc...)

How would I do this?

Upvotes: 1

Views: 1729

Answers (3)

David K
David K

Reputation: 690

Not as concise and pretty as timos's answer :), but another quick example of the same functionality but written in Ruby.

#!/usr/bin/env ruby

data = File.read("data.txt")
f = File.open("outData.txt", "w")
finalData = data.scan(/^\w+/)
finalData.each {|i| f.write(i + "\n")}
f.close

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340903

cut -d' ' -f1 file.txt

or:

sed 's/ .*//' file.txt

or

sed -e 's/[^ACTG]//g' file.txt

or

awk '{print $1}' file.txt

Upvotes: 2

timos
timos

Reputation: 2707

You can do this with awk:

cat oldfile | awk '{print $1}' > newfile

Upvotes: 2

Related Questions