Reputation: 2144
I have a file which looks like
60 -> 36
48 -> 11
60 -> 59
35 -> 29
27 -> 76
I would like to split this file into two separate files called source and destination such that the source file only contains elements before ' -> ' and the destination file, the part after.
I tried using cut as follows
cut -d' -> ' -f1 input > source
cut -d' -> ' -f2 input > destination
But cut gives me this error
cut: the delimiter must be a single character
Upvotes: 0
Views: 3597
Reputation: 10039
sed '1!H;1h;$!d
g;s/ ->[ 0-9]*//g;w Source
g;s/[ 0-9]* ->//g;w Destination
' YourFile
Write source by removing all end of line, than destination removing head of line after loading the file in memory and used it for each substition
Upvotes: 0
Reputation: 88553
Try to use a whitespace as delimiter: " "
. And in second case use -f3
.
Or with GNU sed:
sed -ne 'h;s/ .*//w source' -e 'g;s/.* //w destination' input
Upvotes: 1
Reputation: 103714
awk
is your best bet for a regex type split.
Given:
$ echo "$tgt"
60 -> 36
48 -> 11
60 -> 59
35 -> 29
27 -> 76
You can split the input with awk
on a regex:
$ echo "$tgt" | awk -F " -> " '{print $1}'
60
48
60
35
27
$ echo "$tgt" | awk -F " -> " '{print $2}'
36
11
59
29
76
And redirect into two files as desired.
Upvotes: 1
Reputation: 23830
As far as I understand it, cut
is incapable of using more than a single character as delimiter.
Using sed
, it should work like this:
sed -E 's/^(.*) -> .*$/\1/g' input > source
sed -E 's/^.* -> (.*)$/\1/g' input > destination
Upvotes: 0
Reputation: 1726
Something like this might be (gnu sed)
sed 's/^\([^ ]*\)/\1/' <file >source
sed 's/.* \(.*\)/\1/' <file >destination
Upvotes: 0