Reverse the order of first 3 digits in a line

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am trying to do a assignment for university, but I am currently stuck.
The goal is to read some phone numbers and reverse the order of the first 3 digits and put them in brackets. I can get it to read the phone numbers but not do the reversal of the digits.
ex: input
214 4234-5555
ex: output
412 4234-5555
this is what I have so far
sed -r "s/([0-9]), ([0-9]), ([0-9])/321/g" phone.txt
bash sed gawk
add a comment |Â
up vote
1
down vote
favorite
I am trying to do a assignment for university, but I am currently stuck.
The goal is to read some phone numbers and reverse the order of the first 3 digits and put them in brackets. I can get it to read the phone numbers but not do the reversal of the digits.
ex: input
214 4234-5555
ex: output
412 4234-5555
this is what I have so far
sed -r "s/([0-9]), ([0-9]), ([0-9])/321/g" phone.txt
bash sed gawk
1
different site duplicate ;) reddit.com/r/commandline/comments/7y51wd/⦠.. also, question mentionsput them in bracketsbut expected output doesn't show it..
â Sundeep
Feb 19 at 3:49
reddit link was the answer ty
â Soda
Feb 19 at 4:04
Similar to How to reverse a string made of digit in bash?.
â Ã±ÃÂsýù÷
Feb 19 at 7:56
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to do a assignment for university, but I am currently stuck.
The goal is to read some phone numbers and reverse the order of the first 3 digits and put them in brackets. I can get it to read the phone numbers but not do the reversal of the digits.
ex: input
214 4234-5555
ex: output
412 4234-5555
this is what I have so far
sed -r "s/([0-9]), ([0-9]), ([0-9])/321/g" phone.txt
bash sed gawk
I am trying to do a assignment for university, but I am currently stuck.
The goal is to read some phone numbers and reverse the order of the first 3 digits and put them in brackets. I can get it to read the phone numbers but not do the reversal of the digits.
ex: input
214 4234-5555
ex: output
412 4234-5555
this is what I have so far
sed -r "s/([0-9]), ([0-9]), ([0-9])/321/g" phone.txt
bash sed gawk
edited Feb 19 at 6:42
Kusalananda
103k13202318
103k13202318
asked Feb 19 at 3:41
Soda
111
111
1
different site duplicate ;) reddit.com/r/commandline/comments/7y51wd/⦠.. also, question mentionsput them in bracketsbut expected output doesn't show it..
â Sundeep
Feb 19 at 3:49
reddit link was the answer ty
â Soda
Feb 19 at 4:04
Similar to How to reverse a string made of digit in bash?.
â Ã±ÃÂsýù÷
Feb 19 at 7:56
add a comment |Â
1
different site duplicate ;) reddit.com/r/commandline/comments/7y51wd/⦠.. also, question mentionsput them in bracketsbut expected output doesn't show it..
â Sundeep
Feb 19 at 3:49
reddit link was the answer ty
â Soda
Feb 19 at 4:04
Similar to How to reverse a string made of digit in bash?.
â Ã±ÃÂsýù÷
Feb 19 at 7:56
1
1
different site duplicate ;) reddit.com/r/commandline/comments/7y51wd/⦠.. also, question mentions
put them in brackets but expected output doesn't show it..â Sundeep
Feb 19 at 3:49
different site duplicate ;) reddit.com/r/commandline/comments/7y51wd/⦠.. also, question mentions
put them in brackets but expected output doesn't show it..â Sundeep
Feb 19 at 3:49
reddit link was the answer ty
â Soda
Feb 19 at 4:04
reddit link was the answer ty
â Soda
Feb 19 at 4:04
Similar to How to reverse a string made of digit in bash?.
â Ã±ÃÂsýù÷
Feb 19 at 7:56
Similar to How to reverse a string made of digit in bash?.
â Ã±ÃÂsýù÷
Feb 19 at 7:56
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
5
down vote
To modify OP's attempt
$ cat ip.txt
214 4234-5555
foo 123 4533-3242
$ sed -r 's/([0-9])([0-9])([0-9])/321/' ip.txt
412 4234-5555
foo 321 4533-3242
$ # adding parenthesis as well
$ sed -r 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
(412) 4234-5555
foo (321) 4533-3242
$ # if ERE is not supported
$ sed 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
(412) 4234-5555
foo (321) 4533-3242
- Note that some
sedimplementation would need-Einstead of-r - Use single quotes unless you need interpolation, see also https://mywiki.wooledge.org/Quotes
([0-9]), ([0-9]), ([0-9])means matching 3 digits separated by comma and spacegmodifier is needed if all matches in a line should be changed
For a generic solution, i.e defining number of digits to reverse as a numeric argument
$ perl -pe 's/d3/reverse $&/e' ip.txt
412 4234-5555
foo 321 4533-3242
$ perl -pe 's/d3/sprintf "(%s)", scalar reverse $&/e' ip.txt
(412) 4234-5555
foo (321) 4533-3242
1
+1'ed, good answer, especiallyperl. You may want to include POSIX grouping([0-9])since POSIXseddoesn't have-r, neither does FreeBSD sed.
â Sergiy Kolodyazhnyy
Feb 19 at 5:55
1
@SergiyKolodyazhnyy, FreeBSD'ssednow does support-rfor compatibility with GNUsed. GNUsednow also supports-Efor compatibility with BSDs and also because it will be the one specified in the next major version of the POSIX specification.
â Stéphane Chazelas
Feb 19 at 16:19
add a comment |Â
up vote
1
down vote
Here's a very long, convoluted, and probably unnecessary sed, but here it is nonetheless because fun:
sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
This works as so:
# pretend 214 4234-5555 is the current line
h; # copy the current line into hold space
s/^([0-9]*) *(.*)/1n/; # keep only first 3 numbers, 214
:1 s/(.)(.*n)/21/;t1; s/.//; # reversing string in sed,
# see notes below; 214 becomes 412
s/^(.*)$/(1)/; # After string is reversed, add brackets; (412)
x;s/([0-9]3)(.*)/2/; # swap hold and pattern buffer,
# delete first 3 chars;
# pattern space now is <space>4234-5555
x;G;s/n// # swap again, append hold buffer to pattern buffer;
# now pattern buffer is (412)<newline> 4234-5555;
# finally delete newline; we get (412) 4234-5555
And that's how it looks like in action:
$ printf "214 4234-5555n123 3333n" | sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
(412) 4234-5555
(321) 3333
Note: String reversal originally found on Stephane Chazelas's comment
add a comment |Â
up vote
0
down vote
$ n='214 4234-5555'
$ echo `echo $n:0:3|rev`$n:3:9
412 4234-555
Brevity is acceptable, but fuller explanations are better.
â Kusalananda
Feb 19 at 10:52
add a comment |Â
up vote
0
down vote
Method1
I have used below method get the same result
i=`awk 'print $1' example.txt| rev`
awk -v i="$i" 'print i,$2' example.txt
Output
412 4234-5555
Method2
sed 's/(.)(.)(.)/321/' example.txt
output
412 4234-5555
I think your first line is missing some command substitution.
â Jeff Schaller
Feb 20 at 3:21
@JeffSchaller Added the ` in first line which was missing. As checked it working fine now. Kindly let me know for any confusion
â Praveen Kumar BS
Feb 20 at 15:16
Still no parenthesis; and what about when thereâÂÂs more than one line of input? Any comment to the OP to teach them what was wrong with their attempt? Otherwise theyâÂÂre stuck copy-pasting solutions that they donâÂÂt understand.
â Jeff Schaller
Feb 20 at 16:02
add a comment |Â
up vote
-3
down vote
If you have the number in phone.txt as "xxx xxx-xxxx", then you can use the below:
!/bin/bash
echo '('$(cat phone.txt | cut -d ' ' -f1 | rev)')'
1
That only prints the 1st three numbers reversed, it removes the rest. And this will print one parenthesis at the top of the file, one at the bottom and everything else in the middle. You also don't need thecat, justcut -d ' ' -f1 phone.txt | revwould do the same (not very helpful) thing.
â terdonâ¦
Feb 19 at 10:32
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
To modify OP's attempt
$ cat ip.txt
214 4234-5555
foo 123 4533-3242
$ sed -r 's/([0-9])([0-9])([0-9])/321/' ip.txt
412 4234-5555
foo 321 4533-3242
$ # adding parenthesis as well
$ sed -r 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
(412) 4234-5555
foo (321) 4533-3242
$ # if ERE is not supported
$ sed 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
(412) 4234-5555
foo (321) 4533-3242
- Note that some
sedimplementation would need-Einstead of-r - Use single quotes unless you need interpolation, see also https://mywiki.wooledge.org/Quotes
([0-9]), ([0-9]), ([0-9])means matching 3 digits separated by comma and spacegmodifier is needed if all matches in a line should be changed
For a generic solution, i.e defining number of digits to reverse as a numeric argument
$ perl -pe 's/d3/reverse $&/e' ip.txt
412 4234-5555
foo 321 4533-3242
$ perl -pe 's/d3/sprintf "(%s)", scalar reverse $&/e' ip.txt
(412) 4234-5555
foo (321) 4533-3242
1
+1'ed, good answer, especiallyperl. You may want to include POSIX grouping([0-9])since POSIXseddoesn't have-r, neither does FreeBSD sed.
â Sergiy Kolodyazhnyy
Feb 19 at 5:55
1
@SergiyKolodyazhnyy, FreeBSD'ssednow does support-rfor compatibility with GNUsed. GNUsednow also supports-Efor compatibility with BSDs and also because it will be the one specified in the next major version of the POSIX specification.
â Stéphane Chazelas
Feb 19 at 16:19
add a comment |Â
up vote
5
down vote
To modify OP's attempt
$ cat ip.txt
214 4234-5555
foo 123 4533-3242
$ sed -r 's/([0-9])([0-9])([0-9])/321/' ip.txt
412 4234-5555
foo 321 4533-3242
$ # adding parenthesis as well
$ sed -r 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
(412) 4234-5555
foo (321) 4533-3242
$ # if ERE is not supported
$ sed 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
(412) 4234-5555
foo (321) 4533-3242
- Note that some
sedimplementation would need-Einstead of-r - Use single quotes unless you need interpolation, see also https://mywiki.wooledge.org/Quotes
([0-9]), ([0-9]), ([0-9])means matching 3 digits separated by comma and spacegmodifier is needed if all matches in a line should be changed
For a generic solution, i.e defining number of digits to reverse as a numeric argument
$ perl -pe 's/d3/reverse $&/e' ip.txt
412 4234-5555
foo 321 4533-3242
$ perl -pe 's/d3/sprintf "(%s)", scalar reverse $&/e' ip.txt
(412) 4234-5555
foo (321) 4533-3242
1
+1'ed, good answer, especiallyperl. You may want to include POSIX grouping([0-9])since POSIXseddoesn't have-r, neither does FreeBSD sed.
â Sergiy Kolodyazhnyy
Feb 19 at 5:55
1
@SergiyKolodyazhnyy, FreeBSD'ssednow does support-rfor compatibility with GNUsed. GNUsednow also supports-Efor compatibility with BSDs and also because it will be the one specified in the next major version of the POSIX specification.
â Stéphane Chazelas
Feb 19 at 16:19
add a comment |Â
up vote
5
down vote
up vote
5
down vote
To modify OP's attempt
$ cat ip.txt
214 4234-5555
foo 123 4533-3242
$ sed -r 's/([0-9])([0-9])([0-9])/321/' ip.txt
412 4234-5555
foo 321 4533-3242
$ # adding parenthesis as well
$ sed -r 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
(412) 4234-5555
foo (321) 4533-3242
$ # if ERE is not supported
$ sed 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
(412) 4234-5555
foo (321) 4533-3242
- Note that some
sedimplementation would need-Einstead of-r - Use single quotes unless you need interpolation, see also https://mywiki.wooledge.org/Quotes
([0-9]), ([0-9]), ([0-9])means matching 3 digits separated by comma and spacegmodifier is needed if all matches in a line should be changed
For a generic solution, i.e defining number of digits to reverse as a numeric argument
$ perl -pe 's/d3/reverse $&/e' ip.txt
412 4234-5555
foo 321 4533-3242
$ perl -pe 's/d3/sprintf "(%s)", scalar reverse $&/e' ip.txt
(412) 4234-5555
foo (321) 4533-3242
To modify OP's attempt
$ cat ip.txt
214 4234-5555
foo 123 4533-3242
$ sed -r 's/([0-9])([0-9])([0-9])/321/' ip.txt
412 4234-5555
foo 321 4533-3242
$ # adding parenthesis as well
$ sed -r 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
(412) 4234-5555
foo (321) 4533-3242
$ # if ERE is not supported
$ sed 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
(412) 4234-5555
foo (321) 4533-3242
- Note that some
sedimplementation would need-Einstead of-r - Use single quotes unless you need interpolation, see also https://mywiki.wooledge.org/Quotes
([0-9]), ([0-9]), ([0-9])means matching 3 digits separated by comma and spacegmodifier is needed if all matches in a line should be changed
For a generic solution, i.e defining number of digits to reverse as a numeric argument
$ perl -pe 's/d3/reverse $&/e' ip.txt
412 4234-5555
foo 321 4533-3242
$ perl -pe 's/d3/sprintf "(%s)", scalar reverse $&/e' ip.txt
(412) 4234-5555
foo (321) 4533-3242
edited Feb 19 at 6:15
answered Feb 19 at 5:03
Sundeep
6,9511826
6,9511826
1
+1'ed, good answer, especiallyperl. You may want to include POSIX grouping([0-9])since POSIXseddoesn't have-r, neither does FreeBSD sed.
â Sergiy Kolodyazhnyy
Feb 19 at 5:55
1
@SergiyKolodyazhnyy, FreeBSD'ssednow does support-rfor compatibility with GNUsed. GNUsednow also supports-Efor compatibility with BSDs and also because it will be the one specified in the next major version of the POSIX specification.
â Stéphane Chazelas
Feb 19 at 16:19
add a comment |Â
1
+1'ed, good answer, especiallyperl. You may want to include POSIX grouping([0-9])since POSIXseddoesn't have-r, neither does FreeBSD sed.
â Sergiy Kolodyazhnyy
Feb 19 at 5:55
1
@SergiyKolodyazhnyy, FreeBSD'ssednow does support-rfor compatibility with GNUsed. GNUsednow also supports-Efor compatibility with BSDs and also because it will be the one specified in the next major version of the POSIX specification.
â Stéphane Chazelas
Feb 19 at 16:19
1
1
+1'ed, good answer, especially
perl . You may want to include POSIX grouping ([0-9]) since POSIX sed doesn't have -r , neither does FreeBSD sed.â Sergiy Kolodyazhnyy
Feb 19 at 5:55
+1'ed, good answer, especially
perl . You may want to include POSIX grouping ([0-9]) since POSIX sed doesn't have -r , neither does FreeBSD sed.â Sergiy Kolodyazhnyy
Feb 19 at 5:55
1
1
@SergiyKolodyazhnyy, FreeBSD's
sed now does support -r for compatibility with GNU sed. GNU sed now also supports -E for compatibility with BSDs and also because it will be the one specified in the next major version of the POSIX specification.â Stéphane Chazelas
Feb 19 at 16:19
@SergiyKolodyazhnyy, FreeBSD's
sed now does support -r for compatibility with GNU sed. GNU sed now also supports -E for compatibility with BSDs and also because it will be the one specified in the next major version of the POSIX specification.â Stéphane Chazelas
Feb 19 at 16:19
add a comment |Â
up vote
1
down vote
Here's a very long, convoluted, and probably unnecessary sed, but here it is nonetheless because fun:
sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
This works as so:
# pretend 214 4234-5555 is the current line
h; # copy the current line into hold space
s/^([0-9]*) *(.*)/1n/; # keep only first 3 numbers, 214
:1 s/(.)(.*n)/21/;t1; s/.//; # reversing string in sed,
# see notes below; 214 becomes 412
s/^(.*)$/(1)/; # After string is reversed, add brackets; (412)
x;s/([0-9]3)(.*)/2/; # swap hold and pattern buffer,
# delete first 3 chars;
# pattern space now is <space>4234-5555
x;G;s/n// # swap again, append hold buffer to pattern buffer;
# now pattern buffer is (412)<newline> 4234-5555;
# finally delete newline; we get (412) 4234-5555
And that's how it looks like in action:
$ printf "214 4234-5555n123 3333n" | sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
(412) 4234-5555
(321) 3333
Note: String reversal originally found on Stephane Chazelas's comment
add a comment |Â
up vote
1
down vote
Here's a very long, convoluted, and probably unnecessary sed, but here it is nonetheless because fun:
sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
This works as so:
# pretend 214 4234-5555 is the current line
h; # copy the current line into hold space
s/^([0-9]*) *(.*)/1n/; # keep only first 3 numbers, 214
:1 s/(.)(.*n)/21/;t1; s/.//; # reversing string in sed,
# see notes below; 214 becomes 412
s/^(.*)$/(1)/; # After string is reversed, add brackets; (412)
x;s/([0-9]3)(.*)/2/; # swap hold and pattern buffer,
# delete first 3 chars;
# pattern space now is <space>4234-5555
x;G;s/n// # swap again, append hold buffer to pattern buffer;
# now pattern buffer is (412)<newline> 4234-5555;
# finally delete newline; we get (412) 4234-5555
And that's how it looks like in action:
$ printf "214 4234-5555n123 3333n" | sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
(412) 4234-5555
(321) 3333
Note: String reversal originally found on Stephane Chazelas's comment
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Here's a very long, convoluted, and probably unnecessary sed, but here it is nonetheless because fun:
sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
This works as so:
# pretend 214 4234-5555 is the current line
h; # copy the current line into hold space
s/^([0-9]*) *(.*)/1n/; # keep only first 3 numbers, 214
:1 s/(.)(.*n)/21/;t1; s/.//; # reversing string in sed,
# see notes below; 214 becomes 412
s/^(.*)$/(1)/; # After string is reversed, add brackets; (412)
x;s/([0-9]3)(.*)/2/; # swap hold and pattern buffer,
# delete first 3 chars;
# pattern space now is <space>4234-5555
x;G;s/n// # swap again, append hold buffer to pattern buffer;
# now pattern buffer is (412)<newline> 4234-5555;
# finally delete newline; we get (412) 4234-5555
And that's how it looks like in action:
$ printf "214 4234-5555n123 3333n" | sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
(412) 4234-5555
(321) 3333
Note: String reversal originally found on Stephane Chazelas's comment
Here's a very long, convoluted, and probably unnecessary sed, but here it is nonetheless because fun:
sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
This works as so:
# pretend 214 4234-5555 is the current line
h; # copy the current line into hold space
s/^([0-9]*) *(.*)/1n/; # keep only first 3 numbers, 214
:1 s/(.)(.*n)/21/;t1; s/.//; # reversing string in sed,
# see notes below; 214 becomes 412
s/^(.*)$/(1)/; # After string is reversed, add brackets; (412)
x;s/([0-9]3)(.*)/2/; # swap hold and pattern buffer,
# delete first 3 chars;
# pattern space now is <space>4234-5555
x;G;s/n// # swap again, append hold buffer to pattern buffer;
# now pattern buffer is (412)<newline> 4234-5555;
# finally delete newline; we get (412) 4234-5555
And that's how it looks like in action:
$ printf "214 4234-5555n123 3333n" | sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
(412) 4234-5555
(321) 3333
Note: String reversal originally found on Stephane Chazelas's comment
answered Feb 19 at 7:47
Sergiy Kolodyazhnyy
7,63311547
7,63311547
add a comment |Â
add a comment |Â
up vote
0
down vote
$ n='214 4234-5555'
$ echo `echo $n:0:3|rev`$n:3:9
412 4234-555
Brevity is acceptable, but fuller explanations are better.
â Kusalananda
Feb 19 at 10:52
add a comment |Â
up vote
0
down vote
$ n='214 4234-5555'
$ echo `echo $n:0:3|rev`$n:3:9
412 4234-555
Brevity is acceptable, but fuller explanations are better.
â Kusalananda
Feb 19 at 10:52
add a comment |Â
up vote
0
down vote
up vote
0
down vote
$ n='214 4234-5555'
$ echo `echo $n:0:3|rev`$n:3:9
412 4234-555
$ n='214 4234-5555'
$ echo `echo $n:0:3|rev`$n:3:9
412 4234-555
answered Feb 19 at 6:26
Budi
112
112
Brevity is acceptable, but fuller explanations are better.
â Kusalananda
Feb 19 at 10:52
add a comment |Â
Brevity is acceptable, but fuller explanations are better.
â Kusalananda
Feb 19 at 10:52
Brevity is acceptable, but fuller explanations are better.
â Kusalananda
Feb 19 at 10:52
Brevity is acceptable, but fuller explanations are better.
â Kusalananda
Feb 19 at 10:52
add a comment |Â
up vote
0
down vote
Method1
I have used below method get the same result
i=`awk 'print $1' example.txt| rev`
awk -v i="$i" 'print i,$2' example.txt
Output
412 4234-5555
Method2
sed 's/(.)(.)(.)/321/' example.txt
output
412 4234-5555
I think your first line is missing some command substitution.
â Jeff Schaller
Feb 20 at 3:21
@JeffSchaller Added the ` in first line which was missing. As checked it working fine now. Kindly let me know for any confusion
â Praveen Kumar BS
Feb 20 at 15:16
Still no parenthesis; and what about when thereâÂÂs more than one line of input? Any comment to the OP to teach them what was wrong with their attempt? Otherwise theyâÂÂre stuck copy-pasting solutions that they donâÂÂt understand.
â Jeff Schaller
Feb 20 at 16:02
add a comment |Â
up vote
0
down vote
Method1
I have used below method get the same result
i=`awk 'print $1' example.txt| rev`
awk -v i="$i" 'print i,$2' example.txt
Output
412 4234-5555
Method2
sed 's/(.)(.)(.)/321/' example.txt
output
412 4234-5555
I think your first line is missing some command substitution.
â Jeff Schaller
Feb 20 at 3:21
@JeffSchaller Added the ` in first line which was missing. As checked it working fine now. Kindly let me know for any confusion
â Praveen Kumar BS
Feb 20 at 15:16
Still no parenthesis; and what about when thereâÂÂs more than one line of input? Any comment to the OP to teach them what was wrong with their attempt? Otherwise theyâÂÂre stuck copy-pasting solutions that they donâÂÂt understand.
â Jeff Schaller
Feb 20 at 16:02
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Method1
I have used below method get the same result
i=`awk 'print $1' example.txt| rev`
awk -v i="$i" 'print i,$2' example.txt
Output
412 4234-5555
Method2
sed 's/(.)(.)(.)/321/' example.txt
output
412 4234-5555
Method1
I have used below method get the same result
i=`awk 'print $1' example.txt| rev`
awk -v i="$i" 'print i,$2' example.txt
Output
412 4234-5555
Method2
sed 's/(.)(.)(.)/321/' example.txt
output
412 4234-5555
edited Feb 20 at 15:13
answered Feb 19 at 16:04
Praveen Kumar BS
1,010128
1,010128
I think your first line is missing some command substitution.
â Jeff Schaller
Feb 20 at 3:21
@JeffSchaller Added the ` in first line which was missing. As checked it working fine now. Kindly let me know for any confusion
â Praveen Kumar BS
Feb 20 at 15:16
Still no parenthesis; and what about when thereâÂÂs more than one line of input? Any comment to the OP to teach them what was wrong with their attempt? Otherwise theyâÂÂre stuck copy-pasting solutions that they donâÂÂt understand.
â Jeff Schaller
Feb 20 at 16:02
add a comment |Â
I think your first line is missing some command substitution.
â Jeff Schaller
Feb 20 at 3:21
@JeffSchaller Added the ` in first line which was missing. As checked it working fine now. Kindly let me know for any confusion
â Praveen Kumar BS
Feb 20 at 15:16
Still no parenthesis; and what about when thereâÂÂs more than one line of input? Any comment to the OP to teach them what was wrong with their attempt? Otherwise theyâÂÂre stuck copy-pasting solutions that they donâÂÂt understand.
â Jeff Schaller
Feb 20 at 16:02
I think your first line is missing some command substitution.
â Jeff Schaller
Feb 20 at 3:21
I think your first line is missing some command substitution.
â Jeff Schaller
Feb 20 at 3:21
@JeffSchaller Added the ` in first line which was missing. As checked it working fine now. Kindly let me know for any confusion
â Praveen Kumar BS
Feb 20 at 15:16
@JeffSchaller Added the ` in first line which was missing. As checked it working fine now. Kindly let me know for any confusion
â Praveen Kumar BS
Feb 20 at 15:16
Still no parenthesis; and what about when thereâÂÂs more than one line of input? Any comment to the OP to teach them what was wrong with their attempt? Otherwise theyâÂÂre stuck copy-pasting solutions that they donâÂÂt understand.
â Jeff Schaller
Feb 20 at 16:02
Still no parenthesis; and what about when thereâÂÂs more than one line of input? Any comment to the OP to teach them what was wrong with their attempt? Otherwise theyâÂÂre stuck copy-pasting solutions that they donâÂÂt understand.
â Jeff Schaller
Feb 20 at 16:02
add a comment |Â
up vote
-3
down vote
If you have the number in phone.txt as "xxx xxx-xxxx", then you can use the below:
!/bin/bash
echo '('$(cat phone.txt | cut -d ' ' -f1 | rev)')'
1
That only prints the 1st three numbers reversed, it removes the rest. And this will print one parenthesis at the top of the file, one at the bottom and everything else in the middle. You also don't need thecat, justcut -d ' ' -f1 phone.txt | revwould do the same (not very helpful) thing.
â terdonâ¦
Feb 19 at 10:32
add a comment |Â
up vote
-3
down vote
If you have the number in phone.txt as "xxx xxx-xxxx", then you can use the below:
!/bin/bash
echo '('$(cat phone.txt | cut -d ' ' -f1 | rev)')'
1
That only prints the 1st three numbers reversed, it removes the rest. And this will print one parenthesis at the top of the file, one at the bottom and everything else in the middle. You also don't need thecat, justcut -d ' ' -f1 phone.txt | revwould do the same (not very helpful) thing.
â terdonâ¦
Feb 19 at 10:32
add a comment |Â
up vote
-3
down vote
up vote
-3
down vote
If you have the number in phone.txt as "xxx xxx-xxxx", then you can use the below:
!/bin/bash
echo '('$(cat phone.txt | cut -d ' ' -f1 | rev)')'
If you have the number in phone.txt as "xxx xxx-xxxx", then you can use the below:
!/bin/bash
echo '('$(cat phone.txt | cut -d ' ' -f1 | rev)')'
edited Feb 19 at 5:40
answered Feb 19 at 5:25
sai
13
13
1
That only prints the 1st three numbers reversed, it removes the rest. And this will print one parenthesis at the top of the file, one at the bottom and everything else in the middle. You also don't need thecat, justcut -d ' ' -f1 phone.txt | revwould do the same (not very helpful) thing.
â terdonâ¦
Feb 19 at 10:32
add a comment |Â
1
That only prints the 1st three numbers reversed, it removes the rest. And this will print one parenthesis at the top of the file, one at the bottom and everything else in the middle. You also don't need thecat, justcut -d ' ' -f1 phone.txt | revwould do the same (not very helpful) thing.
â terdonâ¦
Feb 19 at 10:32
1
1
That only prints the 1st three numbers reversed, it removes the rest. And this will print one parenthesis at the top of the file, one at the bottom and everything else in the middle. You also don't need the
cat, just cut -d ' ' -f1 phone.txt | rev would do the same (not very helpful) thing.â terdonâ¦
Feb 19 at 10:32
That only prints the 1st three numbers reversed, it removes the rest. And this will print one parenthesis at the top of the file, one at the bottom and everything else in the middle. You also don't need the
cat, just cut -d ' ' -f1 phone.txt | rev would do the same (not very helpful) thing.â terdonâ¦
Feb 19 at 10:32
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f425073%2freverse-the-order-of-first-3-digits-in-a-line%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
different site duplicate ;) reddit.com/r/commandline/comments/7y51wd/⦠.. also, question mentions
put them in bracketsbut expected output doesn't show it..â Sundeep
Feb 19 at 3:49
reddit link was the answer ty
â Soda
Feb 19 at 4:04
Similar to How to reverse a string made of digit in bash?.
â Ã±ÃÂsýù÷
Feb 19 at 7:56