Is something wrong with my use of wc or grep in the linux command line? I"m getting +1 on my char count [closed] - linux

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
When I run
echo "obase=2;3" | bc | grep -v \n\s | wc -m
bash returns 3. But when I run
echo "obase=2;3" | bc
bash returns 11.
Why is wc -m one digit high on its count?

The extra character is the trailing newline.
wc -m receives and counts the following three characters: 1 1 \n.
$ echo "obase=2;3" | bc | grep -v \n\s | od -c
0000000 1 1 \n
0000003
If you get rid of the newline, the count will be as you're expecting:
$ echo "obase=2;3" | bc | grep -v \n\s | tr -d '\n' | wc -m
2

Related

how do I call multiple commands in a single variable for bash script? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have set of commands and I want all of them to be defined in a single variable. below are the commands I want display in output.
pwdx `ps -ef | grep java | cut -d' ' -f4` | cut -d/ -f7
I tried with
app=`pwdx `ps -ef | grep java | cut -d' ' -f4` | cut -d/ -f7`
echo $app
but this gives empty output. Tried using for loop, that failed too.
Use the |& operator to pipe the output (stdout and stderr) of the previous command into the standard input of another one:
pwdx `ps -ef |& grep java |& cut -d' ' -f4` |& cut -d/ -f7
See this write-up for more on executing several bash commands: Running multiple commands in one line in shell

Bash Console putting some invisible chars into string var [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Below I shared my console. I want to cut some string from output of some commands.
But there are 17 extra chars which I have no idea where comes from.
Can someone pls explain to me?
$ ls -al | grep total | sed 's/[[:blank:]].*$//' | wc -m
23
$ ns="total"
$ echo $ns | sed 's/[[:blank:]].*$//' | wc -c
6
But there are 17 extra chars which I have no idea where comes from.
Those are ANSI escape codes that grep uses for coloring matching substrings. You probably have an alias (run alias | grep grep to examine) like
alias grep='grep --color=always'
somewhere that causes grep to color matches even if output is not a tty, or something similar.
Try
ls -al | grep --color=never total | sed 's/[[:blank:]].*$//' | wc -m
and you'll get six.

Sorting numbers in a row on the BASH / Shell [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
There is a line:
00000000000000;000022233333;2;NONE;true;100,100,5,1,28;UNKNOWN
It is necessary to sort 100,100,5,1,28 numbers in descending order.
Example:
00000000000000;000022233333;2;NONE;true;100,100,28,5,1;UNKNOWN
try this;
#!/bin/bash
while read line
do
beforeC=$(echo "$line" | cut -f-5 -d';')
sortcolumn=$(echo "$line" | awk -F ";" '{print $6}' | tr -t , "\n" | sort -r -n | xargs | sed 's/ /,/g')
afterC=$(echo "$line" | cut -f7- -d';')
echo -e $beforeC";"$sortcolumn";"$afterC
done <file
user#host:/tmp/test$ cat file
00000000000000;000022233333;2;NONE;true;100,100,5,1,28;UNKNOWN
00000000000000;000022233333;2;NONE;true;99,100,5,1,28;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,99,5,1,28;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,100,4,1,28;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,100,4,0,28;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,100,4,1,27;UNKNOWN
user#host:/tmp/test$ ./sortAColumn.sh
00000000000000;000022233333;2;NONE;true;100,100,28,5,1;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,99,28,5,1;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,99,28,5,1;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,100,28,4,1;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,100,28,4,0;UNKNOWN
00000000000000;000022233333;2;NONE;true;100,100,27,4,1;UNKNOWN

How to sort multiple files? Unix [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Usually i can do this to sort a textfile:
cat infile.txt | sort > outfile.out
mv outfile.out > infile.txt
I can also do it in a loop:
for inp in ./*; do
fname=${inp##*/}
cat "$inp" | sort > ./"$fname".out
done
Other than writing a loop, is there a one liner to do the above for all files in the terminal?
This strikes me as an absurd exercise since there's nothing wrong with a loop, but you can do:
ls | xargs -n 1 sh -c 'sort $1 > $1.tmp; mv $1.tmp $1' sh
With GNU sort you can do:
$ sort file -o file
You could use xargs instead of looping like:
$ ls | xargs -i% -n1 sort % -o %
If you don't have the -o option:
$ sort file > tmp && mv tmp file
$ ls | xargs -i% -n1 sort % > tmp && mv tmp %

Command for finding process using too much CPU [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
What command can I use to find a process that's using a lot of CPU? Can I do this without installing something new?
Or using a few other utils you could do:
ps aux | sort -rk 3,3 | head -n 5
Change the value of head to get the number of processes you want to see.
Try doing this :
top -b -n1 -c
And if you want the process that takes the most %CPU times :
top -b -n1 -c | awk '/PID *USER/{print;getline;print}'
or
top -b -n1 -c | grep -A 2 '^$'

Resources