Swap lat lon and group by square brackets
up vote
1
down vote
favorite
I have A file which is having data like
input.csv
1;1.30453 103.83589;1.306663 103.83587;1.30 103.5;1.30693 103.83883;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.8798
2;1.353 103.83589;1.306663 103.83587;1.30693 103.83583;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.835
3;1.30653 103.83589;1.306663 103.83587;1.306797 103.83585;1.3693
.....
I am trying to convert this file to be in the format (I swap the lat and long values position and group them by a square bracket and seperate them by a comma)
output.csv
1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741][103.8798,1.30753]
.... and so on.
text-processing awk sed perl
add a comment |
up vote
1
down vote
favorite
I have A file which is having data like
input.csv
1;1.30453 103.83589;1.306663 103.83587;1.30 103.5;1.30693 103.83883;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.8798
2;1.353 103.83589;1.306663 103.83587;1.30693 103.83583;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.835
3;1.30653 103.83589;1.306663 103.83587;1.306797 103.83585;1.3693
.....
I am trying to convert this file to be in the format (I swap the lat and long values position and group them by a square bracket and seperate them by a comma)
output.csv
1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741][103.8798,1.30753]
.... and so on.
text-processing awk sed perl
1
Is103.835871.30693
on line 2 of the input a misspelling? Also, there are seemingly random spaces added to the output, or is that also misspelling?
– Ralph Rönnquist
Mar 3 '17 at 5:49
Sorry for the Misspelling.Updated the question
– RKR
Mar 3 '17 at 5:56
1
mmm you probably meant to add a;
on the input rather than a space.(?) Isawk
a requirement (given that this one is much easier withsed
)
– Ralph Rönnquist
Mar 3 '17 at 6:00
No awk is not a requirement.sed is fine.
– RKR
Mar 3 '17 at 6:15
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have A file which is having data like
input.csv
1;1.30453 103.83589;1.306663 103.83587;1.30 103.5;1.30693 103.83883;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.8798
2;1.353 103.83589;1.306663 103.83587;1.30693 103.83583;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.835
3;1.30653 103.83589;1.306663 103.83587;1.306797 103.83585;1.3693
.....
I am trying to convert this file to be in the format (I swap the lat and long values position and group them by a square bracket and seperate them by a comma)
output.csv
1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741][103.8798,1.30753]
.... and so on.
text-processing awk sed perl
I have A file which is having data like
input.csv
1;1.30453 103.83589;1.306663 103.83587;1.30 103.5;1.30693 103.83883;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.8798
2;1.353 103.83589;1.306663 103.83587;1.30693 103.83583;1.30709 103.835793;1.30725 103.835757;1.30741 103.83572;1.30753 103.835
3;1.30653 103.83589;1.306663 103.83587;1.306797 103.83585;1.3693
.....
I am trying to convert this file to be in the format (I swap the lat and long values position and group them by a square bracket and seperate them by a comma)
output.csv
1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741][103.8798,1.30753]
.... and so on.
text-processing awk sed perl
text-processing awk sed perl
edited Nov 25 at 14:59
Rui F Ribeiro
38.3k1475126
38.3k1475126
asked Mar 3 '17 at 4:22
RKR
23219
23219
1
Is103.835871.30693
on line 2 of the input a misspelling? Also, there are seemingly random spaces added to the output, or is that also misspelling?
– Ralph Rönnquist
Mar 3 '17 at 5:49
Sorry for the Misspelling.Updated the question
– RKR
Mar 3 '17 at 5:56
1
mmm you probably meant to add a;
on the input rather than a space.(?) Isawk
a requirement (given that this one is much easier withsed
)
– Ralph Rönnquist
Mar 3 '17 at 6:00
No awk is not a requirement.sed is fine.
– RKR
Mar 3 '17 at 6:15
add a comment |
1
Is103.835871.30693
on line 2 of the input a misspelling? Also, there are seemingly random spaces added to the output, or is that also misspelling?
– Ralph Rönnquist
Mar 3 '17 at 5:49
Sorry for the Misspelling.Updated the question
– RKR
Mar 3 '17 at 5:56
1
mmm you probably meant to add a;
on the input rather than a space.(?) Isawk
a requirement (given that this one is much easier withsed
)
– Ralph Rönnquist
Mar 3 '17 at 6:00
No awk is not a requirement.sed is fine.
– RKR
Mar 3 '17 at 6:15
1
1
Is
103.835871.30693
on line 2 of the input a misspelling? Also, there are seemingly random spaces added to the output, or is that also misspelling?– Ralph Rönnquist
Mar 3 '17 at 5:49
Is
103.835871.30693
on line 2 of the input a misspelling? Also, there are seemingly random spaces added to the output, or is that also misspelling?– Ralph Rönnquist
Mar 3 '17 at 5:49
Sorry for the Misspelling.Updated the question
– RKR
Mar 3 '17 at 5:56
Sorry for the Misspelling.Updated the question
– RKR
Mar 3 '17 at 5:56
1
1
mmm you probably meant to add a
;
on the input rather than a space.(?) Is awk
a requirement (given that this one is much easier with sed
)– Ralph Rönnquist
Mar 3 '17 at 6:00
mmm you probably meant to add a
;
on the input rather than a space.(?) Is awk
a requirement (given that this one is much easier with sed
)– Ralph Rönnquist
Mar 3 '17 at 6:00
No awk is not a requirement.sed is fine.
– RKR
Mar 3 '17 at 6:15
No awk is not a requirement.sed is fine.
– RKR
Mar 3 '17 at 6:15
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
sed 's/;([0-9.]*) ([0-9.]*)/,[2,1]/g' input.csv > output.csv
can do the job but in 2nd line there is 1.306663 103.83587 1.30693 103.83583
group which has no ;
separator. If it is not a misprint script should be modified as
sed 's/[; ]?([0-9.]+) ([0-9.]+)/,[2,1]/g' input.csv > output.csv
which produce
1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.8798,1.30753]
2,[103.83589,1.353],[103.83587,1.306663],[103.83583,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.835,1.30753]
3,[103.83589,1.30653],[103.83587,1.306663],[103.83585,1.306797];1.3693
add a comment |
up vote
2
down vote
With GNU sed 4.2.2
, you'd use a simple sed
script, like the following:
sed 's/([0-9.]+) ([0-9.]+)/[2,1]/g;s/;/,/g' < input.csv > output.csv
It needs backslashes for parentheses and +
in the match pattern (as well as the numbers in the replacement) to make sed
regard them as meta characters.
The script consists of two exhaustive replacements for each line: firstly replacing two number-and-dot sequences separated by space with those sequences in opposite order, separated by comma and enclosed in brackets; secondly replacing semi-colon with comma.
Though, with the dual use of ,
in the output, it might not be easily loaded as a CSV.
add a comment |
up vote
2
down vote
perl -F's+|;' -pale '$" = ",";
$_ = "$F[0],[" . join("],[", map { "@F[2*$_,2*$_-1]" } 1 .. @F/2) . "]";
' yourfile
Brief
The field separator are set at multiple whitespaces or semicolon. Then
excepting for the 1st field, the rest come in pairs. So keeping aside
($F[0]) we look at 2 fields at a time, flip them, & join all these pairs
with "],[".
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
sed 's/;([0-9.]*) ([0-9.]*)/,[2,1]/g' input.csv > output.csv
can do the job but in 2nd line there is 1.306663 103.83587 1.30693 103.83583
group which has no ;
separator. If it is not a misprint script should be modified as
sed 's/[; ]?([0-9.]+) ([0-9.]+)/,[2,1]/g' input.csv > output.csv
which produce
1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.8798,1.30753]
2,[103.83589,1.353],[103.83587,1.306663],[103.83583,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.835,1.30753]
3,[103.83589,1.30653],[103.83587,1.306663],[103.83585,1.306797];1.3693
add a comment |
up vote
2
down vote
sed 's/;([0-9.]*) ([0-9.]*)/,[2,1]/g' input.csv > output.csv
can do the job but in 2nd line there is 1.306663 103.83587 1.30693 103.83583
group which has no ;
separator. If it is not a misprint script should be modified as
sed 's/[; ]?([0-9.]+) ([0-9.]+)/,[2,1]/g' input.csv > output.csv
which produce
1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.8798,1.30753]
2,[103.83589,1.353],[103.83587,1.306663],[103.83583,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.835,1.30753]
3,[103.83589,1.30653],[103.83587,1.306663],[103.83585,1.306797];1.3693
add a comment |
up vote
2
down vote
up vote
2
down vote
sed 's/;([0-9.]*) ([0-9.]*)/,[2,1]/g' input.csv > output.csv
can do the job but in 2nd line there is 1.306663 103.83587 1.30693 103.83583
group which has no ;
separator. If it is not a misprint script should be modified as
sed 's/[; ]?([0-9.]+) ([0-9.]+)/,[2,1]/g' input.csv > output.csv
which produce
1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.8798,1.30753]
2,[103.83589,1.353],[103.83587,1.306663],[103.83583,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.835,1.30753]
3,[103.83589,1.30653],[103.83587,1.306663],[103.83585,1.306797];1.3693
sed 's/;([0-9.]*) ([0-9.]*)/,[2,1]/g' input.csv > output.csv
can do the job but in 2nd line there is 1.306663 103.83587 1.30693 103.83583
group which has no ;
separator. If it is not a misprint script should be modified as
sed 's/[; ]?([0-9.]+) ([0-9.]+)/,[2,1]/g' input.csv > output.csv
which produce
1,[103.83589,1.30453],[103.83587,1.306663],[103.5,1.30],[103.83883,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.8798,1.30753]
2,[103.83589,1.353],[103.83587,1.306663],[103.83583,1.30693],[103.835793,1.30709],[103.835757,1.30725],[103.83572,1.30741],[103.835,1.30753]
3,[103.83589,1.30653],[103.83587,1.306663],[103.83585,1.306797];1.3693
answered Mar 3 '17 at 6:31
Costas
12.6k1129
12.6k1129
add a comment |
add a comment |
up vote
2
down vote
With GNU sed 4.2.2
, you'd use a simple sed
script, like the following:
sed 's/([0-9.]+) ([0-9.]+)/[2,1]/g;s/;/,/g' < input.csv > output.csv
It needs backslashes for parentheses and +
in the match pattern (as well as the numbers in the replacement) to make sed
regard them as meta characters.
The script consists of two exhaustive replacements for each line: firstly replacing two number-and-dot sequences separated by space with those sequences in opposite order, separated by comma and enclosed in brackets; secondly replacing semi-colon with comma.
Though, with the dual use of ,
in the output, it might not be easily loaded as a CSV.
add a comment |
up vote
2
down vote
With GNU sed 4.2.2
, you'd use a simple sed
script, like the following:
sed 's/([0-9.]+) ([0-9.]+)/[2,1]/g;s/;/,/g' < input.csv > output.csv
It needs backslashes for parentheses and +
in the match pattern (as well as the numbers in the replacement) to make sed
regard them as meta characters.
The script consists of two exhaustive replacements for each line: firstly replacing two number-and-dot sequences separated by space with those sequences in opposite order, separated by comma and enclosed in brackets; secondly replacing semi-colon with comma.
Though, with the dual use of ,
in the output, it might not be easily loaded as a CSV.
add a comment |
up vote
2
down vote
up vote
2
down vote
With GNU sed 4.2.2
, you'd use a simple sed
script, like the following:
sed 's/([0-9.]+) ([0-9.]+)/[2,1]/g;s/;/,/g' < input.csv > output.csv
It needs backslashes for parentheses and +
in the match pattern (as well as the numbers in the replacement) to make sed
regard them as meta characters.
The script consists of two exhaustive replacements for each line: firstly replacing two number-and-dot sequences separated by space with those sequences in opposite order, separated by comma and enclosed in brackets; secondly replacing semi-colon with comma.
Though, with the dual use of ,
in the output, it might not be easily loaded as a CSV.
With GNU sed 4.2.2
, you'd use a simple sed
script, like the following:
sed 's/([0-9.]+) ([0-9.]+)/[2,1]/g;s/;/,/g' < input.csv > output.csv
It needs backslashes for parentheses and +
in the match pattern (as well as the numbers in the replacement) to make sed
regard them as meta characters.
The script consists of two exhaustive replacements for each line: firstly replacing two number-and-dot sequences separated by space with those sequences in opposite order, separated by comma and enclosed in brackets; secondly replacing semi-colon with comma.
Though, with the dual use of ,
in the output, it might not be easily loaded as a CSV.
answered Mar 3 '17 at 6:40
Ralph Rönnquist
2,48238
2,48238
add a comment |
add a comment |
up vote
2
down vote
perl -F's+|;' -pale '$" = ",";
$_ = "$F[0],[" . join("],[", map { "@F[2*$_,2*$_-1]" } 1 .. @F/2) . "]";
' yourfile
Brief
The field separator are set at multiple whitespaces or semicolon. Then
excepting for the 1st field, the rest come in pairs. So keeping aside
($F[0]) we look at 2 fields at a time, flip them, & join all these pairs
with "],[".
add a comment |
up vote
2
down vote
perl -F's+|;' -pale '$" = ",";
$_ = "$F[0],[" . join("],[", map { "@F[2*$_,2*$_-1]" } 1 .. @F/2) . "]";
' yourfile
Brief
The field separator are set at multiple whitespaces or semicolon. Then
excepting for the 1st field, the rest come in pairs. So keeping aside
($F[0]) we look at 2 fields at a time, flip them, & join all these pairs
with "],[".
add a comment |
up vote
2
down vote
up vote
2
down vote
perl -F's+|;' -pale '$" = ",";
$_ = "$F[0],[" . join("],[", map { "@F[2*$_,2*$_-1]" } 1 .. @F/2) . "]";
' yourfile
Brief
The field separator are set at multiple whitespaces or semicolon. Then
excepting for the 1st field, the rest come in pairs. So keeping aside
($F[0]) we look at 2 fields at a time, flip them, & join all these pairs
with "],[".
perl -F's+|;' -pale '$" = ",";
$_ = "$F[0],[" . join("],[", map { "@F[2*$_,2*$_-1]" } 1 .. @F/2) . "]";
' yourfile
Brief
The field separator are set at multiple whitespaces or semicolon. Then
excepting for the 1st field, the rest come in pairs. So keeping aside
($F[0]) we look at 2 fields at a time, flip them, & join all these pairs
with "],[".
answered Mar 3 '17 at 9:36
user218374
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f348824%2fswap-lat-lon-and-group-by-square-brackets%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Is
103.835871.30693
on line 2 of the input a misspelling? Also, there are seemingly random spaces added to the output, or is that also misspelling?– Ralph Rönnquist
Mar 3 '17 at 5:49
Sorry for the Misspelling.Updated the question
– RKR
Mar 3 '17 at 5:56
1
mmm you probably meant to add a
;
on the input rather than a space.(?) Isawk
a requirement (given that this one is much easier withsed
)– Ralph Rönnquist
Mar 3 '17 at 6:00
No awk is not a requirement.sed is fine.
– RKR
Mar 3 '17 at 6:15