I have a file with many lines, like
1 jfkdajfd 1 2 3 5
2 fkldfjld
3 fdkfloaf 9 10
4 fldfldkf
5 fdskf;ak 12 1 4
I want to get all the numbers and put them in a column in a file, like
1
2
3
5
9
10
12
1
4
how can I achieve this?
thanks
In your case, it looks like you can do this:
awk '{for (i=3;i<=NF;++i) {print $i}}'
This is assuming that all the numbers you want to print occur in column 3 or after.
cat file | while read line
do
for i in $(echo ${line})
do
isnumeric=$(echo ${i} | grep -q [0-9]; echo ${?})
if [ ${isnumeric} -eq 0 ]
then
echo ${i} >> outfile
fi
done
done
not bulletproof and not as elegant as the previously given solutions, but it shows what is being used for determining if this is a numeric or not.
while read num alpha rest; do
[[ "$rest" ]] && printf "%s\n" $rest # <-- variable is unquoted
done < filename
Related
I need a bash script to find the sum of the absolute value of integers separated by spaces. For instance, if the input is:
1 2 -3
the script should print 6 to standard output
I have:
while read x ; do echo $(( ${x// /+} )) ; done
which gives me
0
Without over complicated things, how would I include an absolute value of each x in that statement so the output would be:
6
With Barmar's idea:
echo "1 2 -3" | tr -d - | tr ' ' '+' | bc -l
Output:
6
You have almost done it, but the -s must have been removed from the line read:
while read x; do x=${x//-}; echo $(( ${x// /+} )); done
POSIX friendly implementation without running a loop and without spawning a sub-shell:
#!/usr/bin/env sh
abssum() {
IFS='-'
set -- $*
IFS=' '
set -- $*
IFS=+
printf %d\\n $(($*))
}
abssum 1 2 -3
Result:
6
I need help transposing a file that just simply has some numbers in them with rows and columns. I can't use awk shell or perl so it makes it kind of hard. I've been working on it for a couple of hours and can't get it working correctly. I tried a couple of other things but this is what I have right now. It runs, but it doesn't print out anything, so that leads me to conclude that something is wrong within my code. Also if you dont know by transpose if a file had :
1 2 3
4 5 6
... it would then print out
1 4
2 5
3 6
Here is my code:
if [ $# -gt 2 ]
then
echo"System error">&2
exit 1
elif [[ $# -eq 2 && -e "$2" && -r "$2" ]]
then
while read -a line; do
for ((i=0; i < "${#line[#]}"; i++)); do
a[$i]="${a[$i]} ${line[$i]}"
done
done < $2
for ((i=0; i < ${#a[#]}; i++)); do
echo ${a[i]}
done
fi
If possible use awk:
Source (file.txt):
1 2 3
4 5 6
Result:
1 4
2 5
3 6
Oneline awk sctript:
awk '{ for (i=1; i<=NF; i++) a[i]= (a[i]? a[i] FS $i: $i) } END{ for (i in a) print a[i] }' file.txt
It works same with
1 2 3 1 4 7
4 5 6 -> 2 5
7 3 6
and with
1 2 3 4 1 5
5 6 7 -> 2 6
3 7
4
Instead of writing a Bash function, we could use rs, written specifically for reshaping matrices. This command does exactly what you ask for:
rs -T
I have a file called export_list that has 8 values on separate lines. I am looking to make another file, that echoes the values. For some reason I cannot find the write expression to increment the line value of sed. Any help is appreciated.
export_list looks like this:
1
2
3
4
5
6
7
8
I have an import file that looks like this:
count=1
if [ $count -lt 8 ]
then
value=$(sed -n ''$count'p' < export_list)
echo $value
((count++))
fi
I think you need to look at read:
count=0
while read value
do
echo $value
((count++))
done < export_list
This looks a lot simpler to me. Note that the redirection does have to be after the done. There are also reasons in Bash not to try cat export_list | while read value; do … done.
You can use awk like this command awk '{print $0+1}' export_list
[root#~ ] # awk '{print $0+1}' export_list
2
3
4
5
6
7
8
9
so I'm dealing with column averaging problems
I have several columns of number,
1 2 3 4 5
2 3 4 5 6
8 4 5 6 7
9 8 7 6 5
1 9 9 9 2
What I want to do is to take an average of each column so that they will yield
4 5 5 6 5
The summing part is not a problem, I'm able to get the sum of each column. However, somehow I'm having trouble in counting the number of columns (variable $x)
Here is my code
while read line; do
x = 0
read -a array <<< "$line"
for i in "${!array[#]}"
do
column[${i}]=$((${column[${i}]}+${array[$i]}))
((x++))
done
done < $TMP
for sum in ${column[#]}
do
average=`expr $sum / $x`
remainder=`expr $sum % $x`
mult=`expr $remainder \* 10`
fracvalue=`expr $mult / $x`
echo $x
echo $average
echo $sum
echo $remainder
echo $mult
echo $fracvalue
done
The last several lines are for my testing purposes, here the $X keeps showing 1 instead of 5. That's why it screws up all the other variable. Does anybody know where's the flaw in this code? Really appreciate your help. Thanks
Another approach:
#!/bin/bash
declare -ia sum # declare array of integers
declare -i lines=0 # declare integer to count lines
# read file to array and sum values
while read -ra array; do
for ((i=0; i<${#array[#]};i++)); do
sum[$i]+=${array[$i]}
done
lines+=1
done < file
# print averages
for ((j=0; j<$i;j++)); do
echo -n "$((${sum[$j]}/$lines)) "
done
Output:
4 5 5 6 5
The problem is in the line
x = 0
should be
x=0 and make ((x+1)) as let "x+=1" it should work.
Maybe your file doesn't have the same number of lines in each column.
while read line; do
read -a array <<< "$line"
for i in "${!array[#]}"
do
column[${i}]=$((${column[${i}]}+${array[$i]}))
((x[${i}]++))
done
done < $TMP
for i in ${!column[#]}
do
sum=${column[$i]}
x=${x[${i}]}
#your calcul
done
The initial issue, aside from a few syntax peculiarities, was the resetting of x on each read which caused you to lose your line count. Additionally, your array is an indexed array, not an associative array.
Making the small adjustments and declareing the variables at the beginning (to hint to bash they are integers or arrays), it works as planned. note: there is no need for a here-string to assign the array, just use regular array syntax. Also note, for indexed arrays, there is no need to dereference the variable within [ ], simply ${array[i]} is fine.
While the use of expr is fine (old, slow, but portable and fine), you could use bash arithmetic syntax for your parameter calculations at the end. (you already have arrays). Finally, you are better served initializing each column[$i]=0 on the first read loop. You can do that with a simple flag:
#!/bin/bash
declare -i ncols=0
declare -i x=0
declare -a column
while read -r line; do
array=( $line )
[ $ncols -eq 0 ] && ncols=${#array[#]}
for ((i = 0; i < ncols; i++))
do
[ $x -eq 0 ] && column[$i]=0
column[$i]=$(( ${column[i]} + ${array[i]}))
done
((x++))
done < "$1"
for sum in ${column[#]}
do
average=`expr $sum / $x`
remainder=`expr $sum % $x`
mult=`expr $remainder \* 10`
fracvalue=`expr $mult / $x`
echo $x
echo $average
echo $sum
echo $remainder
echo $mult
echo $fracvalue
done
exit 0
Output
$ bash colavg.sh dat/colavg.txt
5
4
21
1
10
2
5
5
26
1
10
2
5
5
28
3
30
6
5
6
30
0
0
0
5
5
25
0
0
0
I have a list of shell scripts (that end with .sh) in a folder and I am trying to make a list of them, and the list should have two script names (separated by a space) in each line. I wrote the following script, but it does not work without showing any errors. I was hoping to get some help here:
file1=""
for file in $(ls ./*.sh); do
i=1
file1="$file1$file "
if [ $i -lt 3 ]; then
i=$(( i++ ))
continue
fi
echo "file1 is $file1" # expected $file1 is: scriptName1.sh scriptName2.sh
echo $file1 >> ScriptList.txt # save the two file names in the text file
file1=""
done
To get tab separated output:
ls *.sh | paste - -
The pr utility is handy for columnizing as well. It can split the data "vertically":
$ seq 10 | pr -2 -T -s" "
1 6
2 7
3 8
4 9
5 10
or "horizontally"
$ seq 10 | pr -2 -T -s" " -a
1 2
3 4
5 6
7 8
9 10
It's not a good idea to set i=1 everytime in your loop.
Try
ls -1 | while read a;
do
if [ -z $save ]; then
save="$a"
else
echo "$save $a"
save=""
fi
done