0

I want to remove the character | from output.

My output is from these commands:

lnstat -i 3 -s 0 -k entries,in_slow_tot,out_slow_tot > test.txt
tail -2 test.txt
0| 5| 3|
1| 2| 4|

and I want this output

0 5 3
1 2 4

I have already tried this one using

grep -v or sed
tail -2 test.txt | sed `|//g`  
tail -2 test.txt | grep -v "|"

but I got this error:

unknown command: '|'
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Mr iSvALiD
  • 67
  • 1
  • 10
  • check `tr` command – Giacomo Catenazzi Jan 12 '21 at 13:31
  • 1
    Its coming from that malformed `sed`. Try either `tail -2 test.txt | sed 's/|//g'`, or as @giacomo suggested `tr`: `tail -2 test.txt | tr -d '|'` – costaparas Jan 12 '21 at 13:35
  • Deleting a character from some output is a very well examined problem, and you should have absolutely zero trouble finding many solutions for that problem. Why ask here? Did you take time to read the `sed` manual or a tutorial? – U. Windl Jan 12 '21 at 14:06

1 Answers1

3

This ...

tail -2 test.txt | sed `|//g`

... should be:

tail -2 test.txt | sed 's/|//g'

That's probably what I would do. Note that not only is the sed expression in the original version incorrect, but the wrong kind of quotes are used as well. The wrong quotes are the reason for the "unknown command" error. They instruct the shell to execute |//g as a command, and to insert its standard output into the sed command line.

grep does not offer a solution to the problem presented -- it allows you to filter lines based on whether they match a pattern, but it does not transform the contents of lines.

There is also tr, however, which is mnemonic for "translate". This is to be understood as a character-level translation, not language translation. It can translate some characters to nothing. In particular:

tail -2 test.txt | tr -d '|'
John Bollinger
  • 160,171
  • 8
  • 81
  • 157