i need to read a text file and find a particular column in that text file ..i have no idea on how to do it ..can anyone help please..

Recommended Answers

All 4 Replies

Well if you were using a real OS with a decent set of command line tools, then I would suggest http://www.manpagez.com/man/1/cut/

But since you seem (from your other posts) to be using an OS where all you can do is attempt to create fine art with wax crayons, then I dunno.
Download ActiveState perl would be one suggestion.

The only command in windows console that doesn't get really messy in a hurry is 'exit' ;)

hey saleem thanks for the reply..really appreciate it ....

sum=0
count=0
j=0
i=0
#echo "enter File:"
#read ff
for i in `cat mm`
do
j=`echo $i | grep -v [a-z] | grep -v "-" | tr -s ' ' | cut -d' ' -f11`
echo $j
sum=`expr $sum + $j`
count=`expr $count + 1`
done
avg=`expr $sum / $count`;
echo "SUM: $sum         AVG: $avg"

my os is linux
bash shell
this code works fine but it need to find file from any given location ...how can i do it..the file that i'm reading is a space seperated vmstat log file...

In a shell environment, doing arithmetic on various columns, awk is your friend. (Well, I guess it isn't really friendly, but it is efficient and useful). You can use expr, but awk is easier (once you understand it) and faster, if that matters.

I don't understand your 'need to find file from any given location' requirement. If the file has an absolute path, use it. If it has a path relative to $HOME or some other variable, use the variable. If you need to let the user specify it, make your tool into an executable script and require a file name argument: Interactively reading an answer is not very 'unixy'.

hey saleem thanks for the reply..really appreciate it ....

sum=0
count=0
j=0
i=0
#echo "enter File:"
#read ff
for i in `cat mm`

end quote.

That is almost always the wrong way to read a file. It read the file word by word, not line by line. Use redirection instead:

while IFS= read -r i
do
   : do something here with "$line"
done  < mm

start quote.

do
j=`echo $i | grep -v [a-z] | grep -v "-" | tr -s ' ' | cut -d' ' -f11`

end quote.

You really don't want to call 4 external commands for every line of the file.

Also that command will fail if you have any files with a single lowercase letter for the name: [a-z] will expand to all such files.

start quote.

echo $j
sum=`expr $sum + $j`
count=`expr $count + 1`

end quote.

There is no need for an external command (expr) to do integer arithmetic in the standard Unix shell:

sum=$(( $sum + $j ))
count=$(( $count + 1 ))
avg=$(( $sum / $count ))

start quote.

done
echo "SUM: $sum         AVG: $avg"

end quote.

Replace the above code with:

grep -v -e '[a-z]' -e '-' |
 tr -s ' ' |
  awk -F " " '
    { sum += $1 }
END { printf "SUM: %d    AVG: %d", sum, ; sum / NR }
' "$file"

my os is linux
bash shell
this code works fine but it need to find file from any given location ...how can i do it..the file that i'm reading is a space seperated vmstat log file...

At the top of the script, define the file variable:

file=/path/to/log file

Or if it is given an argument on the command line:

file=$1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.