Need Desire output using shell [on hold]2019 Community Moderator ElectionHow to strip multiple spaces to one using sed?Replacing a pattern with another between first and second occurrence of a pattern in fileprinting special characters in awk outputHow can I split a line into two lines if the length is greater than 7 using awk?Find first occurence of a number in each line of a fileHow to Compare two files words by words and it should shows at which line and at which position the differences are presentsearch a string and print the string and it's headerHow to use grep to get the matching part only, without introducing extra newlinesHow to print only 1 filename together with the matching pattern?How to find last occurrence of pattern and print all lines following the last occurance
Hostile work environment after whistle-blowing on coworker and our boss. What do I do?
Science Fiction story where a man invents a machine that can help him watch history unfold
Simple recursive Sudoku solver
Perfect riffle shuffles
How to check participants in at events?
Organic chemistry Iodoform Reaction
Have I saved too much for retirement so far?
Stereotypical names
Who must act to prevent Brexit on March 29th?
Meta programming: Declare a new struct on the fly
Can somebody explain Brexit in a few child-proof sentences?
For airliners, what prevents wing strikes on landing in bad weather?
Superhero words!
A known event to a history junkie
Resetting two CD4017 counters simultaneously, only one resets
Greatest common substring
Is the next prime number always the next number divisible by the current prime number, except for any numbers previously divisible by primes?
Adding empty element to declared container without declaring type of element
Can I Retrieve Email Addresses from BCC?
Can I rely on these GitHub repository files?
Can a Gentile theist be saved?
The most efficient algorithm to find all possible integer pairs which sum to a given integer
Does "Dominei" mean something?
Can a malicious addon access internet history and such in chrome/firefox?
Need Desire output using shell [on hold]
2019 Community Moderator ElectionHow to strip multiple spaces to one using sed?Replacing a pattern with another between first and second occurrence of a pattern in fileprinting special characters in awk outputHow can I split a line into two lines if the length is greater than 7 using awk?Find first occurence of a number in each line of a fileHow to Compare two files words by words and it should shows at which line and at which position the differences are presentsearch a string and print the string and it's headerHow to use grep to get the matching part only, without introducing extra newlinesHow to print only 1 filename together with the matching pattern?How to find last occurrence of pattern and print all lines following the last occurance
I need to get desired output using a shell script.
Input:
aaabbcaaabbcc
Desired Output:
a3b2ca3b2c2
Command should print number of occurrence of each alphabet only when it's greater than 1, i.e aaab
should come as a3b
.
I tried with below commands and they didn't work for me, as I have to pass characters manually to my command.
echo "aaabbcaaabbcc" | grep -o a
echo "aaabbcaaabbcc" | uniq -c
echo "aaabbcaaabbcc" | grep -o a | uniq -c
I am unable to find how I can split it character-wise and then check number of occurrences.
shell-script text-processing
put on hold as unclear what you're asking by Kusalananda, JdeBP, Jeff Schaller, jimmij, Mr Shunz 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 3 more comments
I need to get desired output using a shell script.
Input:
aaabbcaaabbcc
Desired Output:
a3b2ca3b2c2
Command should print number of occurrence of each alphabet only when it's greater than 1, i.e aaab
should come as a3b
.
I tried with below commands and they didn't work for me, as I have to pass characters manually to my command.
echo "aaabbcaaabbcc" | grep -o a
echo "aaabbcaaabbcc" | uniq -c
echo "aaabbcaaabbcc" | grep -o a | uniq -c
I am unable to find how I can split it character-wise and then check number of occurrences.
shell-script text-processing
put on hold as unclear what you're asking by Kusalananda, JdeBP, Jeff Schaller, jimmij, Mr Shunz 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1) Where is the input coming from? Is it the contents of a text file? The output of a command or application? 2) What have you tried to do? If you just want to replace the string in the input with the string in the desired output then you don't need a shell script. Have a look at thesed
command.
– Nasir Riley
2 days ago
1
You seem to want to implement some form run-length encoding. What issues are you having with doing this?
– Kusalananda
2 days ago
There are far too many possible answers with this incomplete and underspecified question, starting withecho a3b2ca3b2c2
.
– JdeBP
2 days ago
2
Related: rosettacode.org/wiki/Run-length_encoding
– Kusalananda
2 days ago
1
@Machine Run-length encoding (RLE) is a well known and simple way of compressing highly redundant data. If you have a particular issue with theawk
variant on the Rosetta page that I linked to, then you may want to ask another question about it. Pure programming questions relating to the implementation of specific algorithms would arguably be more suitable on StackOverflow though.
– Kusalananda
13 hours ago
|
show 3 more comments
I need to get desired output using a shell script.
Input:
aaabbcaaabbcc
Desired Output:
a3b2ca3b2c2
Command should print number of occurrence of each alphabet only when it's greater than 1, i.e aaab
should come as a3b
.
I tried with below commands and they didn't work for me, as I have to pass characters manually to my command.
echo "aaabbcaaabbcc" | grep -o a
echo "aaabbcaaabbcc" | uniq -c
echo "aaabbcaaabbcc" | grep -o a | uniq -c
I am unable to find how I can split it character-wise and then check number of occurrences.
shell-script text-processing
I need to get desired output using a shell script.
Input:
aaabbcaaabbcc
Desired Output:
a3b2ca3b2c2
Command should print number of occurrence of each alphabet only when it's greater than 1, i.e aaab
should come as a3b
.
I tried with below commands and they didn't work for me, as I have to pass characters manually to my command.
echo "aaabbcaaabbcc" | grep -o a
echo "aaabbcaaabbcc" | uniq -c
echo "aaabbcaaabbcc" | grep -o a | uniq -c
I am unable to find how I can split it character-wise and then check number of occurrences.
shell-script text-processing
shell-script text-processing
edited 13 hours ago
Jeff Schaller
43.9k1161141
43.9k1161141
asked 2 days ago
MachineMachine
335
335
put on hold as unclear what you're asking by Kusalananda, JdeBP, Jeff Schaller, jimmij, Mr Shunz 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as unclear what you're asking by Kusalananda, JdeBP, Jeff Schaller, jimmij, Mr Shunz 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1) Where is the input coming from? Is it the contents of a text file? The output of a command or application? 2) What have you tried to do? If you just want to replace the string in the input with the string in the desired output then you don't need a shell script. Have a look at thesed
command.
– Nasir Riley
2 days ago
1
You seem to want to implement some form run-length encoding. What issues are you having with doing this?
– Kusalananda
2 days ago
There are far too many possible answers with this incomplete and underspecified question, starting withecho a3b2ca3b2c2
.
– JdeBP
2 days ago
2
Related: rosettacode.org/wiki/Run-length_encoding
– Kusalananda
2 days ago
1
@Machine Run-length encoding (RLE) is a well known and simple way of compressing highly redundant data. If you have a particular issue with theawk
variant on the Rosetta page that I linked to, then you may want to ask another question about it. Pure programming questions relating to the implementation of specific algorithms would arguably be more suitable on StackOverflow though.
– Kusalananda
13 hours ago
|
show 3 more comments
1) Where is the input coming from? Is it the contents of a text file? The output of a command or application? 2) What have you tried to do? If you just want to replace the string in the input with the string in the desired output then you don't need a shell script. Have a look at thesed
command.
– Nasir Riley
2 days ago
1
You seem to want to implement some form run-length encoding. What issues are you having with doing this?
– Kusalananda
2 days ago
There are far too many possible answers with this incomplete and underspecified question, starting withecho a3b2ca3b2c2
.
– JdeBP
2 days ago
2
Related: rosettacode.org/wiki/Run-length_encoding
– Kusalananda
2 days ago
1
@Machine Run-length encoding (RLE) is a well known and simple way of compressing highly redundant data. If you have a particular issue with theawk
variant on the Rosetta page that I linked to, then you may want to ask another question about it. Pure programming questions relating to the implementation of specific algorithms would arguably be more suitable on StackOverflow though.
– Kusalananda
13 hours ago
1) Where is the input coming from? Is it the contents of a text file? The output of a command or application? 2) What have you tried to do? If you just want to replace the string in the input with the string in the desired output then you don't need a shell script. Have a look at the
sed
command.– Nasir Riley
2 days ago
1) Where is the input coming from? Is it the contents of a text file? The output of a command or application? 2) What have you tried to do? If you just want to replace the string in the input with the string in the desired output then you don't need a shell script. Have a look at the
sed
command.– Nasir Riley
2 days ago
1
1
You seem to want to implement some form run-length encoding. What issues are you having with doing this?
– Kusalananda
2 days ago
You seem to want to implement some form run-length encoding. What issues are you having with doing this?
– Kusalananda
2 days ago
There are far too many possible answers with this incomplete and underspecified question, starting with
echo a3b2ca3b2c2
.– JdeBP
2 days ago
There are far too many possible answers with this incomplete and underspecified question, starting with
echo a3b2ca3b2c2
.– JdeBP
2 days ago
2
2
Related: rosettacode.org/wiki/Run-length_encoding
– Kusalananda
2 days ago
Related: rosettacode.org/wiki/Run-length_encoding
– Kusalananda
2 days ago
1
1
@Machine Run-length encoding (RLE) is a well known and simple way of compressing highly redundant data. If you have a particular issue with the
awk
variant on the Rosetta page that I linked to, then you may want to ask another question about it. Pure programming questions relating to the implementation of specific algorithms would arguably be more suitable on StackOverflow though.– Kusalananda
13 hours ago
@Machine Run-length encoding (RLE) is a well known and simple way of compressing highly redundant data. If you have a particular issue with the
awk
variant on the Rosetta page that I linked to, then you may want to ask another question about it. Pure programming questions relating to the implementation of specific algorithms would arguably be more suitable on StackOverflow though.– Kusalananda
13 hours ago
|
show 3 more comments
1 Answer
1
active
oldest
votes
str="aaabbcaaabbcc"
echo $str | fold -w1 | uniq -c |
while read count char; do
if [ $count -gt 1 ]; then
printf "$char$count"
else
printf "$char"
fi
done
a3b2ca3b2c2
1
There seems to be a special case for$count -eq 1
– nohillside
2 days ago
1
Indeed. Fixed for that case.
– Fedor Dikarev
2 days ago
This would unfortunately fail if the string contained backslashes or percentage signs, or filename globbing patterns, or started with a dash, or ifIFS
was set to a set of digits or letters.
– Kusalananda
2 days ago
@ Fedor Dikarev , I never knew about fold which can split a word vertically in char form. Thanks for this
– Machine
13 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
str="aaabbcaaabbcc"
echo $str | fold -w1 | uniq -c |
while read count char; do
if [ $count -gt 1 ]; then
printf "$char$count"
else
printf "$char"
fi
done
a3b2ca3b2c2
1
There seems to be a special case for$count -eq 1
– nohillside
2 days ago
1
Indeed. Fixed for that case.
– Fedor Dikarev
2 days ago
This would unfortunately fail if the string contained backslashes or percentage signs, or filename globbing patterns, or started with a dash, or ifIFS
was set to a set of digits or letters.
– Kusalananda
2 days ago
@ Fedor Dikarev , I never knew about fold which can split a word vertically in char form. Thanks for this
– Machine
13 hours ago
add a comment |
str="aaabbcaaabbcc"
echo $str | fold -w1 | uniq -c |
while read count char; do
if [ $count -gt 1 ]; then
printf "$char$count"
else
printf "$char"
fi
done
a3b2ca3b2c2
1
There seems to be a special case for$count -eq 1
– nohillside
2 days ago
1
Indeed. Fixed for that case.
– Fedor Dikarev
2 days ago
This would unfortunately fail if the string contained backslashes or percentage signs, or filename globbing patterns, or started with a dash, or ifIFS
was set to a set of digits or letters.
– Kusalananda
2 days ago
@ Fedor Dikarev , I never knew about fold which can split a word vertically in char form. Thanks for this
– Machine
13 hours ago
add a comment |
str="aaabbcaaabbcc"
echo $str | fold -w1 | uniq -c |
while read count char; do
if [ $count -gt 1 ]; then
printf "$char$count"
else
printf "$char"
fi
done
a3b2ca3b2c2
str="aaabbcaaabbcc"
echo $str | fold -w1 | uniq -c |
while read count char; do
if [ $count -gt 1 ]; then
printf "$char$count"
else
printf "$char"
fi
done
a3b2ca3b2c2
edited 2 days ago
answered 2 days ago
Fedor DikarevFedor Dikarev
1,103310
1,103310
1
There seems to be a special case for$count -eq 1
– nohillside
2 days ago
1
Indeed. Fixed for that case.
– Fedor Dikarev
2 days ago
This would unfortunately fail if the string contained backslashes or percentage signs, or filename globbing patterns, or started with a dash, or ifIFS
was set to a set of digits or letters.
– Kusalananda
2 days ago
@ Fedor Dikarev , I never knew about fold which can split a word vertically in char form. Thanks for this
– Machine
13 hours ago
add a comment |
1
There seems to be a special case for$count -eq 1
– nohillside
2 days ago
1
Indeed. Fixed for that case.
– Fedor Dikarev
2 days ago
This would unfortunately fail if the string contained backslashes or percentage signs, or filename globbing patterns, or started with a dash, or ifIFS
was set to a set of digits or letters.
– Kusalananda
2 days ago
@ Fedor Dikarev , I never knew about fold which can split a word vertically in char form. Thanks for this
– Machine
13 hours ago
1
1
There seems to be a special case for
$count -eq 1
– nohillside
2 days ago
There seems to be a special case for
$count -eq 1
– nohillside
2 days ago
1
1
Indeed. Fixed for that case.
– Fedor Dikarev
2 days ago
Indeed. Fixed for that case.
– Fedor Dikarev
2 days ago
This would unfortunately fail if the string contained backslashes or percentage signs, or filename globbing patterns, or started with a dash, or if
IFS
was set to a set of digits or letters.– Kusalananda
2 days ago
This would unfortunately fail if the string contained backslashes or percentage signs, or filename globbing patterns, or started with a dash, or if
IFS
was set to a set of digits or letters.– Kusalananda
2 days ago
@ Fedor Dikarev , I never knew about fold which can split a word vertically in char form. Thanks for this
– Machine
13 hours ago
@ Fedor Dikarev , I never knew about fold which can split a word vertically in char form. Thanks for this
– Machine
13 hours ago
add a comment |
-shell-script, text-processing
1) Where is the input coming from? Is it the contents of a text file? The output of a command or application? 2) What have you tried to do? If you just want to replace the string in the input with the string in the desired output then you don't need a shell script. Have a look at the
sed
command.– Nasir Riley
2 days ago
1
You seem to want to implement some form run-length encoding. What issues are you having with doing this?
– Kusalananda
2 days ago
There are far too many possible answers with this incomplete and underspecified question, starting with
echo a3b2ca3b2c2
.– JdeBP
2 days ago
2
Related: rosettacode.org/wiki/Run-length_encoding
– Kusalananda
2 days ago
1
@Machine Run-length encoding (RLE) is a well known and simple way of compressing highly redundant data. If you have a particular issue with the
awk
variant on the Rosetta page that I linked to, then you may want to ask another question about it. Pure programming questions relating to the implementation of specific algorithms would arguably be more suitable on StackOverflow though.– Kusalananda
13 hours ago