split string to two parts using sed or awk or perl or bash
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have string like this:
Grades ABCDEF-123456
I want to split this string to two sections like this
Grades ABCDEF
Grades 123456
How can I do that in bash?
bash awk sed perl
New contributor
add a comment |Â
up vote
1
down vote
favorite
I have string like this:
Grades ABCDEF-123456
I want to split this string to two sections like this
Grades ABCDEF
Grades 123456
How can I do that in bash?
bash awk sed perl
New contributor
1
Where does that string come from ? Is it stored in a variable or in a file ? Why tagging thisbash
if you want (per the title) to usesed
,perl
orawk
?
â don_crissti
3 hours ago
i have script for my study and i was unable to figure out how to split strings with the dash-
which is pulling the letters and numbers together every time i tried to split it,
â Sincerity
2 hours ago
1
whatever you say buddy...
â don_crissti
2 hours ago
what do you mean?
â Sincerity
2 hours ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have string like this:
Grades ABCDEF-123456
I want to split this string to two sections like this
Grades ABCDEF
Grades 123456
How can I do that in bash?
bash awk sed perl
New contributor
I have string like this:
Grades ABCDEF-123456
I want to split this string to two sections like this
Grades ABCDEF
Grades 123456
How can I do that in bash?
bash awk sed perl
bash awk sed perl
New contributor
New contributor
edited 2 hours ago
New contributor
asked 3 hours ago
Sincerity
83
83
New contributor
New contributor
1
Where does that string come from ? Is it stored in a variable or in a file ? Why tagging thisbash
if you want (per the title) to usesed
,perl
orawk
?
â don_crissti
3 hours ago
i have script for my study and i was unable to figure out how to split strings with the dash-
which is pulling the letters and numbers together every time i tried to split it,
â Sincerity
2 hours ago
1
whatever you say buddy...
â don_crissti
2 hours ago
what do you mean?
â Sincerity
2 hours ago
add a comment |Â
1
Where does that string come from ? Is it stored in a variable or in a file ? Why tagging thisbash
if you want (per the title) to usesed
,perl
orawk
?
â don_crissti
3 hours ago
i have script for my study and i was unable to figure out how to split strings with the dash-
which is pulling the letters and numbers together every time i tried to split it,
â Sincerity
2 hours ago
1
whatever you say buddy...
â don_crissti
2 hours ago
what do you mean?
â Sincerity
2 hours ago
1
1
Where does that string come from ? Is it stored in a variable or in a file ? Why tagging this
bash
if you want (per the title) to use sed
, perl
or awk
?â don_crissti
3 hours ago
Where does that string come from ? Is it stored in a variable or in a file ? Why tagging this
bash
if you want (per the title) to use sed
, perl
or awk
?â don_crissti
3 hours ago
i have script for my study and i was unable to figure out how to split strings with the dash
-
which is pulling the letters and numbers together every time i tried to split it,â Sincerity
2 hours ago
i have script for my study and i was unable to figure out how to split strings with the dash
-
which is pulling the letters and numbers together every time i tried to split it,â Sincerity
2 hours ago
1
1
whatever you say buddy...
â don_crissti
2 hours ago
whatever you say buddy...
â don_crissti
2 hours ago
what do you mean?
â Sincerity
2 hours ago
what do you mean?
â Sincerity
2 hours ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
echo Grades "ABCDE-12345" | sed 's/-/ /g' | awk ' print $1" "$2"n"$1" "$3'
Grades ABCDE
Grades 12345
or per @steeldriver
awk -F'[ -]' 'print $1, $2; print $1, $3'
2
There's really no need forsed
here - just set theFS
to include-
i.e.awk -F'[ -]' 'print $1, $2; print $1, $3'
â steeldriver
3 hours ago
@ steeldriver thanks!
â Goro
2 hours ago
thank you for the quick answer
â Sincerity
2 hours ago
add a comment |Â
up vote
2
down vote
You can do this entirely in your shell, too:
text="Grades ABCDEF-123456"
Split off the leading text. You could capture it if required, but here we'll just discard it:
grades="$text#* "
Now we could extract the two parts as variables but for now we'll just print them:
echo "Grades $grades%-*"
echo "Grades $grades#*-"
You can also crash these together into a single output statement, but I don't think it's as readable (even if printf
is safer than echo
for certain classes of text):
printf "Grades %snGrades %sn" "$grades%-*" "$grades#*-"
add a comment |Â
up vote
1
down vote
You can do it by replacing the dash with a newline followed by the first field:
perl -alpe 's/-/n$F[0] /'
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
accepted
echo Grades "ABCDE-12345" | sed 's/-/ /g' | awk ' print $1" "$2"n"$1" "$3'
Grades ABCDE
Grades 12345
or per @steeldriver
awk -F'[ -]' 'print $1, $2; print $1, $3'
2
There's really no need forsed
here - just set theFS
to include-
i.e.awk -F'[ -]' 'print $1, $2; print $1, $3'
â steeldriver
3 hours ago
@ steeldriver thanks!
â Goro
2 hours ago
thank you for the quick answer
â Sincerity
2 hours ago
add a comment |Â
up vote
2
down vote
accepted
echo Grades "ABCDE-12345" | sed 's/-/ /g' | awk ' print $1" "$2"n"$1" "$3'
Grades ABCDE
Grades 12345
or per @steeldriver
awk -F'[ -]' 'print $1, $2; print $1, $3'
2
There's really no need forsed
here - just set theFS
to include-
i.e.awk -F'[ -]' 'print $1, $2; print $1, $3'
â steeldriver
3 hours ago
@ steeldriver thanks!
â Goro
2 hours ago
thank you for the quick answer
â Sincerity
2 hours ago
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
echo Grades "ABCDE-12345" | sed 's/-/ /g' | awk ' print $1" "$2"n"$1" "$3'
Grades ABCDE
Grades 12345
or per @steeldriver
awk -F'[ -]' 'print $1, $2; print $1, $3'
echo Grades "ABCDE-12345" | sed 's/-/ /g' | awk ' print $1" "$2"n"$1" "$3'
Grades ABCDE
Grades 12345
or per @steeldriver
awk -F'[ -]' 'print $1, $2; print $1, $3'
edited 2 hours ago
answered 3 hours ago
Goro
7,55753371
7,55753371
2
There's really no need forsed
here - just set theFS
to include-
i.e.awk -F'[ -]' 'print $1, $2; print $1, $3'
â steeldriver
3 hours ago
@ steeldriver thanks!
â Goro
2 hours ago
thank you for the quick answer
â Sincerity
2 hours ago
add a comment |Â
2
There's really no need forsed
here - just set theFS
to include-
i.e.awk -F'[ -]' 'print $1, $2; print $1, $3'
â steeldriver
3 hours ago
@ steeldriver thanks!
â Goro
2 hours ago
thank you for the quick answer
â Sincerity
2 hours ago
2
2
There's really no need for
sed
here - just set the FS
to include -
i.e. awk -F'[ -]' 'print $1, $2; print $1, $3'
â steeldriver
3 hours ago
There's really no need for
sed
here - just set the FS
to include -
i.e. awk -F'[ -]' 'print $1, $2; print $1, $3'
â steeldriver
3 hours ago
@ steeldriver thanks!
â Goro
2 hours ago
@ steeldriver thanks!
â Goro
2 hours ago
thank you for the quick answer
â Sincerity
2 hours ago
thank you for the quick answer
â Sincerity
2 hours ago
add a comment |Â
up vote
2
down vote
You can do this entirely in your shell, too:
text="Grades ABCDEF-123456"
Split off the leading text. You could capture it if required, but here we'll just discard it:
grades="$text#* "
Now we could extract the two parts as variables but for now we'll just print them:
echo "Grades $grades%-*"
echo "Grades $grades#*-"
You can also crash these together into a single output statement, but I don't think it's as readable (even if printf
is safer than echo
for certain classes of text):
printf "Grades %snGrades %sn" "$grades%-*" "$grades#*-"
add a comment |Â
up vote
2
down vote
You can do this entirely in your shell, too:
text="Grades ABCDEF-123456"
Split off the leading text. You could capture it if required, but here we'll just discard it:
grades="$text#* "
Now we could extract the two parts as variables but for now we'll just print them:
echo "Grades $grades%-*"
echo "Grades $grades#*-"
You can also crash these together into a single output statement, but I don't think it's as readable (even if printf
is safer than echo
for certain classes of text):
printf "Grades %snGrades %sn" "$grades%-*" "$grades#*-"
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can do this entirely in your shell, too:
text="Grades ABCDEF-123456"
Split off the leading text. You could capture it if required, but here we'll just discard it:
grades="$text#* "
Now we could extract the two parts as variables but for now we'll just print them:
echo "Grades $grades%-*"
echo "Grades $grades#*-"
You can also crash these together into a single output statement, but I don't think it's as readable (even if printf
is safer than echo
for certain classes of text):
printf "Grades %snGrades %sn" "$grades%-*" "$grades#*-"
You can do this entirely in your shell, too:
text="Grades ABCDEF-123456"
Split off the leading text. You could capture it if required, but here we'll just discard it:
grades="$text#* "
Now we could extract the two parts as variables but for now we'll just print them:
echo "Grades $grades%-*"
echo "Grades $grades#*-"
You can also crash these together into a single output statement, but I don't think it's as readable (even if printf
is safer than echo
for certain classes of text):
printf "Grades %snGrades %sn" "$grades%-*" "$grades#*-"
answered 2 hours ago
roaima
41k547111
41k547111
add a comment |Â
add a comment |Â
up vote
1
down vote
You can do it by replacing the dash with a newline followed by the first field:
perl -alpe 's/-/n$F[0] /'
add a comment |Â
up vote
1
down vote
You can do it by replacing the dash with a newline followed by the first field:
perl -alpe 's/-/n$F[0] /'
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can do it by replacing the dash with a newline followed by the first field:
perl -alpe 's/-/n$F[0] /'
You can do it by replacing the dash with a newline followed by the first field:
perl -alpe 's/-/n$F[0] /'
answered 47 mins ago
Rakesh Sharma
217113
217113
add a comment |Â
add a comment |Â
Sincerity is a new contributor. Be nice, and check out our Code of Conduct.
Sincerity is a new contributor. Be nice, and check out our Code of Conduct.
Sincerity is a new contributor. Be nice, and check out our Code of Conduct.
Sincerity is a new contributor. Be nice, and check out our Code of Conduct.
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474051%2fsplit-string-to-two-parts-using-sed-or-awk-or-perl-or-bash%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
Where does that string come from ? Is it stored in a variable or in a file ? Why tagging this
bash
if you want (per the title) to usesed
,perl
orawk
?â don_crissti
3 hours ago
i have script for my study and i was unable to figure out how to split strings with the dash
-
which is pulling the letters and numbers together every time i tried to split it,â Sincerity
2 hours ago
1
whatever you say buddy...
â don_crissti
2 hours ago
what do you mean?
â Sincerity
2 hours ago