-1

this is the code I wrote

echo "Positive count =" > file.txt | grep -o -i positive IMDB_Dataset.csv | wc -l >> file.txt

on ubuntu and the result in the file was

Positive count =
26188

can I write it in the same line to become like this

Positive count = 26188
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    Are you really using CMD on Ubuntu? You're probably actually using Bash. BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. – wjandrea Jul 17 '21 at 16:06
  • 1
    BTW, the pipe between `echo` and `grep` does nothing. Replace it with a newline. – wjandrea Jul 17 '21 at 16:07
  • 1
    Can also use `printf "Positive count = %d\n" "$(grep -Foi positive IMDB_Dataset.csv | wc -l)" >> file.txt` (I tend to avoid `echo` for anything but literal strings) – Shawn Jul 17 '21 at 16:49

1 Answers1

1
echo "Positive count =" `grep -o -i positive IMDB_Dataset.csv | wc -l` > file.txt

Or

echo "Positive count =" "$(grep -o -i positive IMDB_Dataset.csv | wc -l)" > file.txt

S2L
  • 1,746
  • 1
  • 16
  • 20
  • 1
    Backticks are deprecated. Use the newer command substitution syntax instead: `$()` – wjandrea Jul 17 '21 at 16:14
  • 1
    Also, it's best practice to include the substituted part within the double quotes – wjandrea Jul 17 '21 at 16:14
  • @wjandrea - Don't spread the misinformation _Backticks are deprecated._ They are conforming to current standards. – Armali Jul 17 '21 at 16:17
  • 1
    @Armali Maybe they're not deprecated per se, but it's best to avoid them. The newer syntax is much more flexible anyway. – wjandrea Jul 17 '21 at 16:22
  • 1
    https://mywiki.wooledge.org/BashFAQ/082 has a good writeup about why `$()` is better. (And newer? It's decades old.) – Shawn Jul 17 '21 at 16:38
  • 1
    @Armali I don't appreciate your tone, so normally I wouldn't reply, but you raise some interesting points. For backticks, see: [Have backticks in *sh shells been deprecated? - Unix & Linux](https://unix.stackexchange.com/q/126927/117037). When I say "deprecated" I mean that they've been superseded and should now be avoided due to their limitations. When I say "best practice", I don't mean that it's required here, but that it's required in some similar situations, so it's best to just do it all the time. See [ShellCheck SC2046](https://github.com/koalaman/shellcheck/wiki/SC2046) – wjandrea Jul 17 '21 at 16:50