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
Related
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 7 months ago.
Improve this question
How to get from a file exactly what I want in Linux?
I have: 123456789012,refid2141,test1,test2,test3 and I want this: 123456789012 or 123456789012 test3.
$ echo "123456789012,refid2141,test1,test2,test3" | awk -F "," '{print $1}'
123456789012
$ echo "123456789012,refid2141,test1,test2,test3" | awk -F "," '{printf("%s, %s", $1,$5)}'
123456789012, test3
foo.csv:
123456789012,refid2141,test1,test2,test3
import csv
with open("foo.csv", "rt") as fd:
data = list(csv.reader(fd))
print(data[0][0])
For a bash solution:
cat foo.csv | cut -d',' -f1
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
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 5 years ago.
Improve this question
I have a text file with the following format.
12345
abcdefg
I need this to be in the same line. So the output should look like this...
12345 abcdefg
How should i proceed ? using sed or awk ?
If you want to join every line[i] and line[i+1] with a space,
you could use paste:
paste -d' ' - - < file
For given input and expected output below one should work
Using xargs
$ cat infile
12345
abcdefg
$ xargs < infile
12345 abcdefg
Using tr
$ tr -s '\n' ' ' <infile ; echo
12345 abcdefg
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 %
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