How to concatenate two command in shell





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







7















I used to have this command to count how many times I have click with a mouse, the command is xev | grep "ButtonPress".



my colleague modify the command so that it return:



ButtonPress 0
ButtonPress 1
ButtonPress 2
ButtonPress 3


and so on... Unfortunately he's no longer contactable so I can't reach him anymore.



I recall the involvement of i++ and something like that, how to reproduce the command?










share|improve this question























  • Do you mean xev | grep -c "ButtonPress", showing the number of clicks on exit?

    – dessert
    Mar 7 at 9:41











  • It shows ButtonPress + number everytime I click on white box pop up window, sorry I'm a novice...

    – Jackie Nelson
    Mar 7 at 9:47











  • dessert, your command worked but it only return the number when I exit xev. How to make it return live value ?

    – Jackie Nelson
    Mar 7 at 9:54


















7















I used to have this command to count how many times I have click with a mouse, the command is xev | grep "ButtonPress".



my colleague modify the command so that it return:



ButtonPress 0
ButtonPress 1
ButtonPress 2
ButtonPress 3


and so on... Unfortunately he's no longer contactable so I can't reach him anymore.



I recall the involvement of i++ and something like that, how to reproduce the command?










share|improve this question























  • Do you mean xev | grep -c "ButtonPress", showing the number of clicks on exit?

    – dessert
    Mar 7 at 9:41











  • It shows ButtonPress + number everytime I click on white box pop up window, sorry I'm a novice...

    – Jackie Nelson
    Mar 7 at 9:47











  • dessert, your command worked but it only return the number when I exit xev. How to make it return live value ?

    – Jackie Nelson
    Mar 7 at 9:54














7












7








7


2






I used to have this command to count how many times I have click with a mouse, the command is xev | grep "ButtonPress".



my colleague modify the command so that it return:



ButtonPress 0
ButtonPress 1
ButtonPress 2
ButtonPress 3


and so on... Unfortunately he's no longer contactable so I can't reach him anymore.



I recall the involvement of i++ and something like that, how to reproduce the command?










share|improve this question














I used to have this command to count how many times I have click with a mouse, the command is xev | grep "ButtonPress".



my colleague modify the command so that it return:



ButtonPress 0
ButtonPress 1
ButtonPress 2
ButtonPress 3


and so on... Unfortunately he's no longer contactable so I can't reach him anymore.



I recall the involvement of i++ and something like that, how to reproduce the command?







command-line bash scripts dash-shell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 9:33









Jackie NelsonJackie Nelson

384




384













  • Do you mean xev | grep -c "ButtonPress", showing the number of clicks on exit?

    – dessert
    Mar 7 at 9:41











  • It shows ButtonPress + number everytime I click on white box pop up window, sorry I'm a novice...

    – Jackie Nelson
    Mar 7 at 9:47











  • dessert, your command worked but it only return the number when I exit xev. How to make it return live value ?

    – Jackie Nelson
    Mar 7 at 9:54



















  • Do you mean xev | grep -c "ButtonPress", showing the number of clicks on exit?

    – dessert
    Mar 7 at 9:41











  • It shows ButtonPress + number everytime I click on white box pop up window, sorry I'm a novice...

    – Jackie Nelson
    Mar 7 at 9:47











  • dessert, your command worked but it only return the number when I exit xev. How to make it return live value ?

    – Jackie Nelson
    Mar 7 at 9:54

















Do you mean xev | grep -c "ButtonPress", showing the number of clicks on exit?

– dessert
Mar 7 at 9:41





Do you mean xev | grep -c "ButtonPress", showing the number of clicks on exit?

– dessert
Mar 7 at 9:41













It shows ButtonPress + number everytime I click on white box pop up window, sorry I'm a novice...

– Jackie Nelson
Mar 7 at 9:47





It shows ButtonPress + number everytime I click on white box pop up window, sorry I'm a novice...

– Jackie Nelson
Mar 7 at 9:47













dessert, your command worked but it only return the number when I exit xev. How to make it return live value ?

– Jackie Nelson
Mar 7 at 9:54





dessert, your command worked but it only return the number when I exit xev. How to make it return live value ?

– Jackie Nelson
Mar 7 at 9:54










1 Answer
1






active

oldest

votes


















11














The fact that there's i++ suggests there was either bash or ksh shell in use,potentially awk or perl as well. In either case, we can use process substitution <(...) to feed output of xev to counting loop (although simple pipeline xev | while... could work just fine).



text processing tools:



Portably and for fewer key strokes we can use awk :



$ xev | awk '/ButtonPress/{print "ButtonPress",i++}'
ButtonPress 0
ButtonPress 1
ButtonPress 2
ButtonPress 3


perl version:



$ xev | perl -ne '/ButtonPress/ && printf("ButtonPress:%dn",++$i)'
ButtonPress:1
ButtonPress:2
ButtonPress:3


Shells:



Here's what works in bash:



$ i=0; while IFS= read -r line; do [[ $line =~ ButtonPress ]] && { ((i++)); printf 'ButtonPress: %dn' "$i";} ;done < <(xev)
ButtonPress: 1
ButtonPress: 2
ButtonPress: 3


In case you don't want spammy output of many lines, we can use printf to send control code to clear previous line and output only the running count (that is you'd only see integer value change on the line):



$ i=0; while IFS= read -r line; do [[ $line =~ ButtonPress ]] && { ((i++)); printf "r%b" "33[2K"; printf 'ButtonPress: %d' "$i";} ;done < <(xev)


Portably in POSIX shell:



$ xev | ( i=0; while IFS= read -r l; do case "$l" in  *ButtonPress*) i=$((i+1)) && printf 'ButtonPress:%dn' "$i";;  esac ;done)
ButtonPress:1
ButtonPress:2
ButtonPress:3


basic utils:



For simple, quick, and dirty way we can hack this via cat -n with line count being printed on the left instead of right:



$ xev | grep --line-buffered 'ButtonPress' | cat -n
1 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,
2 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,
3 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,





share|improve this answer


























  • I tried your first command that has awk in it, it works but there's a bit of lag when the output return

    – Jackie Nelson
    Mar 7 at 9:53











  • However, your second and third command works perfectly, thanks a lot Sergiy

    – Jackie Nelson
    Mar 7 at 9:53











  • @JackieNelson A bit of lag might suggest buffering, which may be related to another awk version in use. Recent Ubuntu uses gawk and before 16.04 default was mawk IIRC. Regardless, glad I could help :)

    – Sergiy Kolodyazhnyy
    Mar 7 at 9:56











  • Can you make so that the number start from one on perl version ? Not zero

    – Jackie Nelson
    Mar 7 at 9:57











  • @JackieNelson Yep, already changed that

    – Sergiy Kolodyazhnyy
    Mar 7 at 10:02












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1123723%2fhow-to-concatenate-two-command-in-shell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









11














The fact that there's i++ suggests there was either bash or ksh shell in use,potentially awk or perl as well. In either case, we can use process substitution <(...) to feed output of xev to counting loop (although simple pipeline xev | while... could work just fine).



text processing tools:



Portably and for fewer key strokes we can use awk :



$ xev | awk '/ButtonPress/{print "ButtonPress",i++}'
ButtonPress 0
ButtonPress 1
ButtonPress 2
ButtonPress 3


perl version:



$ xev | perl -ne '/ButtonPress/ && printf("ButtonPress:%dn",++$i)'
ButtonPress:1
ButtonPress:2
ButtonPress:3


Shells:



Here's what works in bash:



$ i=0; while IFS= read -r line; do [[ $line =~ ButtonPress ]] && { ((i++)); printf 'ButtonPress: %dn' "$i";} ;done < <(xev)
ButtonPress: 1
ButtonPress: 2
ButtonPress: 3


In case you don't want spammy output of many lines, we can use printf to send control code to clear previous line and output only the running count (that is you'd only see integer value change on the line):



$ i=0; while IFS= read -r line; do [[ $line =~ ButtonPress ]] && { ((i++)); printf "r%b" "33[2K"; printf 'ButtonPress: %d' "$i";} ;done < <(xev)


Portably in POSIX shell:



$ xev | ( i=0; while IFS= read -r l; do case "$l" in  *ButtonPress*) i=$((i+1)) && printf 'ButtonPress:%dn' "$i";;  esac ;done)
ButtonPress:1
ButtonPress:2
ButtonPress:3


basic utils:



For simple, quick, and dirty way we can hack this via cat -n with line count being printed on the left instead of right:



$ xev | grep --line-buffered 'ButtonPress' | cat -n
1 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,
2 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,
3 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,





share|improve this answer


























  • I tried your first command that has awk in it, it works but there's a bit of lag when the output return

    – Jackie Nelson
    Mar 7 at 9:53











  • However, your second and third command works perfectly, thanks a lot Sergiy

    – Jackie Nelson
    Mar 7 at 9:53











  • @JackieNelson A bit of lag might suggest buffering, which may be related to another awk version in use. Recent Ubuntu uses gawk and before 16.04 default was mawk IIRC. Regardless, glad I could help :)

    – Sergiy Kolodyazhnyy
    Mar 7 at 9:56











  • Can you make so that the number start from one on perl version ? Not zero

    – Jackie Nelson
    Mar 7 at 9:57











  • @JackieNelson Yep, already changed that

    – Sergiy Kolodyazhnyy
    Mar 7 at 10:02
















11














The fact that there's i++ suggests there was either bash or ksh shell in use,potentially awk or perl as well. In either case, we can use process substitution <(...) to feed output of xev to counting loop (although simple pipeline xev | while... could work just fine).



text processing tools:



Portably and for fewer key strokes we can use awk :



$ xev | awk '/ButtonPress/{print "ButtonPress",i++}'
ButtonPress 0
ButtonPress 1
ButtonPress 2
ButtonPress 3


perl version:



$ xev | perl -ne '/ButtonPress/ && printf("ButtonPress:%dn",++$i)'
ButtonPress:1
ButtonPress:2
ButtonPress:3


Shells:



Here's what works in bash:



$ i=0; while IFS= read -r line; do [[ $line =~ ButtonPress ]] && { ((i++)); printf 'ButtonPress: %dn' "$i";} ;done < <(xev)
ButtonPress: 1
ButtonPress: 2
ButtonPress: 3


In case you don't want spammy output of many lines, we can use printf to send control code to clear previous line and output only the running count (that is you'd only see integer value change on the line):



$ i=0; while IFS= read -r line; do [[ $line =~ ButtonPress ]] && { ((i++)); printf "r%b" "33[2K"; printf 'ButtonPress: %d' "$i";} ;done < <(xev)


Portably in POSIX shell:



$ xev | ( i=0; while IFS= read -r l; do case "$l" in  *ButtonPress*) i=$((i+1)) && printf 'ButtonPress:%dn' "$i";;  esac ;done)
ButtonPress:1
ButtonPress:2
ButtonPress:3


basic utils:



For simple, quick, and dirty way we can hack this via cat -n with line count being printed on the left instead of right:



$ xev | grep --line-buffered 'ButtonPress' | cat -n
1 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,
2 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,
3 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,





share|improve this answer


























  • I tried your first command that has awk in it, it works but there's a bit of lag when the output return

    – Jackie Nelson
    Mar 7 at 9:53











  • However, your second and third command works perfectly, thanks a lot Sergiy

    – Jackie Nelson
    Mar 7 at 9:53











  • @JackieNelson A bit of lag might suggest buffering, which may be related to another awk version in use. Recent Ubuntu uses gawk and before 16.04 default was mawk IIRC. Regardless, glad I could help :)

    – Sergiy Kolodyazhnyy
    Mar 7 at 9:56











  • Can you make so that the number start from one on perl version ? Not zero

    – Jackie Nelson
    Mar 7 at 9:57











  • @JackieNelson Yep, already changed that

    – Sergiy Kolodyazhnyy
    Mar 7 at 10:02














11












11








11







The fact that there's i++ suggests there was either bash or ksh shell in use,potentially awk or perl as well. In either case, we can use process substitution <(...) to feed output of xev to counting loop (although simple pipeline xev | while... could work just fine).



text processing tools:



Portably and for fewer key strokes we can use awk :



$ xev | awk '/ButtonPress/{print "ButtonPress",i++}'
ButtonPress 0
ButtonPress 1
ButtonPress 2
ButtonPress 3


perl version:



$ xev | perl -ne '/ButtonPress/ && printf("ButtonPress:%dn",++$i)'
ButtonPress:1
ButtonPress:2
ButtonPress:3


Shells:



Here's what works in bash:



$ i=0; while IFS= read -r line; do [[ $line =~ ButtonPress ]] && { ((i++)); printf 'ButtonPress: %dn' "$i";} ;done < <(xev)
ButtonPress: 1
ButtonPress: 2
ButtonPress: 3


In case you don't want spammy output of many lines, we can use printf to send control code to clear previous line and output only the running count (that is you'd only see integer value change on the line):



$ i=0; while IFS= read -r line; do [[ $line =~ ButtonPress ]] && { ((i++)); printf "r%b" "33[2K"; printf 'ButtonPress: %d' "$i";} ;done < <(xev)


Portably in POSIX shell:



$ xev | ( i=0; while IFS= read -r l; do case "$l" in  *ButtonPress*) i=$((i+1)) && printf 'ButtonPress:%dn' "$i";;  esac ;done)
ButtonPress:1
ButtonPress:2
ButtonPress:3


basic utils:



For simple, quick, and dirty way we can hack this via cat -n with line count being printed on the left instead of right:



$ xev | grep --line-buffered 'ButtonPress' | cat -n
1 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,
2 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,
3 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,





share|improve this answer















The fact that there's i++ suggests there was either bash or ksh shell in use,potentially awk or perl as well. In either case, we can use process substitution <(...) to feed output of xev to counting loop (although simple pipeline xev | while... could work just fine).



text processing tools:



Portably and for fewer key strokes we can use awk :



$ xev | awk '/ButtonPress/{print "ButtonPress",i++}'
ButtonPress 0
ButtonPress 1
ButtonPress 2
ButtonPress 3


perl version:



$ xev | perl -ne '/ButtonPress/ && printf("ButtonPress:%dn",++$i)'
ButtonPress:1
ButtonPress:2
ButtonPress:3


Shells:



Here's what works in bash:



$ i=0; while IFS= read -r line; do [[ $line =~ ButtonPress ]] && { ((i++)); printf 'ButtonPress: %dn' "$i";} ;done < <(xev)
ButtonPress: 1
ButtonPress: 2
ButtonPress: 3


In case you don't want spammy output of many lines, we can use printf to send control code to clear previous line and output only the running count (that is you'd only see integer value change on the line):



$ i=0; while IFS= read -r line; do [[ $line =~ ButtonPress ]] && { ((i++)); printf "r%b" "33[2K"; printf 'ButtonPress: %d' "$i";} ;done < <(xev)


Portably in POSIX shell:



$ xev | ( i=0; while IFS= read -r l; do case "$l" in  *ButtonPress*) i=$((i+1)) && printf 'ButtonPress:%dn' "$i";;  esac ;done)
ButtonPress:1
ButtonPress:2
ButtonPress:3


basic utils:



For simple, quick, and dirty way we can hack this via cat -n with line count being printed on the left instead of right:



$ xev | grep --line-buffered 'ButtonPress' | cat -n
1 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,
2 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,
3 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 7 at 10:08

























answered Mar 7 at 9:43









Sergiy KolodyazhnyySergiy Kolodyazhnyy

75.1k9155327




75.1k9155327













  • I tried your first command that has awk in it, it works but there's a bit of lag when the output return

    – Jackie Nelson
    Mar 7 at 9:53











  • However, your second and third command works perfectly, thanks a lot Sergiy

    – Jackie Nelson
    Mar 7 at 9:53











  • @JackieNelson A bit of lag might suggest buffering, which may be related to another awk version in use. Recent Ubuntu uses gawk and before 16.04 default was mawk IIRC. Regardless, glad I could help :)

    – Sergiy Kolodyazhnyy
    Mar 7 at 9:56











  • Can you make so that the number start from one on perl version ? Not zero

    – Jackie Nelson
    Mar 7 at 9:57











  • @JackieNelson Yep, already changed that

    – Sergiy Kolodyazhnyy
    Mar 7 at 10:02



















  • I tried your first command that has awk in it, it works but there's a bit of lag when the output return

    – Jackie Nelson
    Mar 7 at 9:53











  • However, your second and third command works perfectly, thanks a lot Sergiy

    – Jackie Nelson
    Mar 7 at 9:53











  • @JackieNelson A bit of lag might suggest buffering, which may be related to another awk version in use. Recent Ubuntu uses gawk and before 16.04 default was mawk IIRC. Regardless, glad I could help :)

    – Sergiy Kolodyazhnyy
    Mar 7 at 9:56











  • Can you make so that the number start from one on perl version ? Not zero

    – Jackie Nelson
    Mar 7 at 9:57











  • @JackieNelson Yep, already changed that

    – Sergiy Kolodyazhnyy
    Mar 7 at 10:02

















I tried your first command that has awk in it, it works but there's a bit of lag when the output return

– Jackie Nelson
Mar 7 at 9:53





I tried your first command that has awk in it, it works but there's a bit of lag when the output return

– Jackie Nelson
Mar 7 at 9:53













However, your second and third command works perfectly, thanks a lot Sergiy

– Jackie Nelson
Mar 7 at 9:53





However, your second and third command works perfectly, thanks a lot Sergiy

– Jackie Nelson
Mar 7 at 9:53













@JackieNelson A bit of lag might suggest buffering, which may be related to another awk version in use. Recent Ubuntu uses gawk and before 16.04 default was mawk IIRC. Regardless, glad I could help :)

– Sergiy Kolodyazhnyy
Mar 7 at 9:56





@JackieNelson A bit of lag might suggest buffering, which may be related to another awk version in use. Recent Ubuntu uses gawk and before 16.04 default was mawk IIRC. Regardless, glad I could help :)

– Sergiy Kolodyazhnyy
Mar 7 at 9:56













Can you make so that the number start from one on perl version ? Not zero

– Jackie Nelson
Mar 7 at 9:57





Can you make so that the number start from one on perl version ? Not zero

– Jackie Nelson
Mar 7 at 9:57













@JackieNelson Yep, already changed that

– Sergiy Kolodyazhnyy
Mar 7 at 10:02





@JackieNelson Yep, already changed that

– Sergiy Kolodyazhnyy
Mar 7 at 10:02


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1123723%2fhow-to-concatenate-two-command-in-shell%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

How do I know what Microsoft account the skydrive app is syncing to?

Grease: Live!

When does type information flow backwards in C++?