2

I have TSV file like this:

abc_1
def_2
ghi_3
jkl_4
mno_5

I want to split that in to the:

abc 
def 
ghi 
jkl
mno

and

1
2
3
4
5

How I can get that?

2 Answers2

8

With awk:

awk -F_ '{ print $1 > "file_1"; print $2 > "file_2" }' source_file
4

Method 1

Split by character position:

To obtain the first output (here, the output is redirected to a file called first_output) use:

cut -c1-3 your_input_file >first_output

To obtain the second output use:

cut -c5- your_input_file >second_output

Here, the option -c means "select only the characters specified." The list following the option -c specifies the character positions or ranges (where 1 is the first character in the line).

Method 2

Split according to delimiter:

cut -f1 -d_ your_input_file >first_output
cut -f2 -d_ your_input_file >second_output

Here, the option -d indicates the delimiting character (_ in our case) and the option -f indicates field position to select.

FedKad
  • 13,420