0

I am trying the following command on bash-4.3

$history | grep history | xargs cut -d ' ' -f1

but i am getting the following delimiter Error that I can't get rid of

cut: the delimiter must be a single character

I tried removing xargs but it gives a blank output. The whole idea is to get a particular command number from the history and then pass it to history -d to delete that particular command from the history. Here is just the output of

history | grep history



  498  history | grep history | cat | xargs cut -d " " -f1
  500  history | grep history | cat | xargs awk -ifs " "
  501  history | grep history | xargs cut -d " " -f1
  502  history | grep history | xargs cut -d '0' -f1
  503  history | grep history | xargs cut -d 0 -f1
  504* history | 
  505  history | ack history | xargs cut -d ' ' -f1
  506  history | ack history | xargs cut -d ' ' -f1
  507  history | ack history | cut -d ' ' -f1
  508  history | grep history 
  • 1
    Try it without the xargs. Although I'm not sure what this command is meant to do... – Stephen Harris Sep 28 '18 at 20:02
  • @StephenHarris updated post. So the idea is to get a particular command number from the history and then pass it to history -d to delete a particular command from the history – user2065276 Sep 28 '18 at 20:06
  • So you probably want to use awk to get the first field. Something like history | awk '/command_to_search_for/ { print $1}'. That'll return the history number of "command_to_search_for" – Stephen Harris Sep 28 '18 at 20:09
  • @StephenHarris What is wrong with my command. How can i fix it? – user2065276 Sep 28 '18 at 20:11
  • 1
    cut -d' ' will split on spaces... but there are multiple spaces and so -f1 will probably just return a space. awk {print $1} is easier to get the first field. So you could do history | grep command_to_search_for | awk '{print $1}' but that's simpler written with the history | awk command I suggested. – Stephen Harris Sep 28 '18 at 20:13
2

Some explanations:

  • You actually want the second field, so you need to change to `cut -d ' ' -f 2'.
  • xargs is not applicable here. What it does is take standard input and passes them as arguments to a command. However, cut operates on standard input by default, which is what you want. history | grep history | xargs cut […] ends up creating commands like cut […] [some content from the Bash history]'. To process a series of line numbers printed with a newline after each you'll want to use a while read loop:

    while IFS=$'\n' read -r -u9 number
    do
        history -d "$number"
    done 9< <(history | grep […] | cut […])
    
  • some_command | cat | other_command is completely redundant. cat simply copies its standard input to standard output by default.
0

Try this command

    history | grep history | awk NR==1'{print $8 " " $9 " " $10 " " $11 " " $12 "" $13 }'
xargs cut -d ' '-f1

NR==1: awk will read the cells in the first row of the table, then prints the columns relevant to "xargs cut -d ' '-f1" which are 8-12

  • please explain the awk syntax and arguments. Thanks – user2065276 Sep 28 '18 at 20:11
  • What is wrong with my command. How can i fix it? – user2065276 Sep 28 '18 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.