You are not logged in.
Pages: 1
Trying use awk for this
What i have is three text files of the same name, but numericly differnet. EX: File1, File2, File3. What i've been trying to do with awk is to read the files individually, have egrep serach for a pattern, use $NF to read the last field, and print the value that $NF stored..
awk '{if ($1=="file[1].txt") -r | egrep "Total" | print "$NF"; }'
Im thinking that the -r ( means read file ) should be included within the condiinition..
But whats weird, is that i cant print that value on one line....when i execute the command file it goes to the next linefeed and thats where i can test my argument.... But i want it on the same line as my cmd file.
Offline
I almost figured it out with cases, but theres a little problem:
Result1=$( cat file1.txt | egrep "Total" | awk '{ print $NF }')
Result2=$( cat file2.txt | egrep "Total" | awk '{ print $NF }')
while [ "$1" != "" ]
do
case $1
in
file[1].txt) echo "$Result1";
exit;;
file[2].txt) echo "$Result2";
exit;;
file[123].txt) echo "$Result1 + $Result2 + 50" | bc;
exit;;
*) echo "Option [$1] not found";
exit;;
esac
done
When i try and use file[123].txt as an argument it will give me file[1]'s case..Is there a way of fixing this without losing the brackets?
The compiler for Unix Cmd Files, ( i guess ) is thinking of these cases as arrays? ( a question )
Last edited by oxoxo (2008-11-25 22:10:03)
Offline
cat file?.txt | awk '{ print $NF }'
????
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
Nah, he means:
awk '/Total/ {print $NF}' file<x>
As in
for file in file[123].txt; do
awk '/Total/ {print $NF}' $file
done
EDIT:
Sorry... i missed the part with the parameters...
This might work:
#!/bin/bash
while [ "$1" != "" ]
echo -n "$1 Total: "
awk '/Total/ {print $NF}' "$1"
shift
done
EDIT2:
When i try and use file[123].txt as an argument it will give me file[1]'s case..Is there a way of fixing this without losing the brackets?
The reason for this is that
script file[123].txt
gets expanded by bash to
script file1.txt file2.txt file3.txt
before the script runs... Therefore the script sees "file1.txt file2.txt file3.txt" as it's input...
if you want to pass "file[123].txt" literally, use single quotes around it to prevent bash from expanding it, as in
script 'file[123].txt'
Last edited by klixon (2008-11-25 22:36:00)
Stand back, intruder, or i'll blast you out of space! I am Klixon and I don't want any dealings with you human lifeforms. I'm a cyborg!
Offline
Actually, when i specify file[123].txt i need it to read fourth element, but its just reading file[1].txt
What i want, is to supply file[123].txt as the argument, and have it read file[3].txt
Offline
Its almost working they way it needs to be.
./script myfile[12345].txt
needs to be everythiing added together and echoed back.
./script myfile[123].txt
needs to be just files 1, 2 and 3 added together.
awk '/Total/ {print $NF}' "$1"
Thats right! But now it'll give me "Option [] not found" for file[1].txt as the argument
And file[123].txt will just display the same result as file[12].txt
I think that shift is the problem..
Offline
If grep is working on files instead of stdin it can/will output the filename:
grep Total myfile[1234].txt | sed 's/:.* /: /'
Offline
im trying to code the script in a way so that it wont echo file[1].txt's case for file[12].txt's case, and so on.. I want every case to be independent of each other.
Whats challenging is that the argument that i enter is interpretted as an array. Probably because it is. I want the argument: file[123].txt to be seen as the actual case that i've implemented.
Last edited by oxoxo (2008-11-26 01:09:11)
Offline
Oh I see. And what is that + 50 doing there?
Offline
in line 16 of my original? I just got lazy i didnt want to declare a new varaible. Its acting as $result3 . and when using the bc function it shouldnt matter if the value is stored in a varaible or not.
Offline
How is this? Doesn't print the filenames, because if it's more than one, what must be printed?
eval echo $(grep Total myfile[1234].txt | sed -n '
s/.* //
$!H
${G
s~\n\+~+~g
s~^~$((~;s~$~))~
p}')
Offline
or
sed -n '/Total/!d;s/.* //;$!H;${G;s~\n\+~+~g;p}' myfile[1234].txt | bc
or use dc and awk
awk 'BEGIN { print "0" } /Total/ { print $NF; print "+" } END { print "p"}' myfile[1234].txt | dc
and awk can do it by itself of course:
awk '/Total/ { total+=$NF } END { print total}' myfile[1234].txt
Last edited by Procyon (2008-11-26 01:57:22)
Offline
Ouch..
What im trying to do is use them as varaibles..
Var1=(awk 'BEGIN { print "0" } /Total/ { print $NF; print "+" } END { print "p"}' myfile[1234].txt) | dc
What i think its doing is searching for those files, myfile[1234].txt and finding the total patter and then adding those values in each of the files..
If that is whats happening then,
myfile[123].txt) echo "$Var1"
should output files 123 added together..?
What i want to have, however possible, is when the user enters myfile[19].txt or myfile[41].txt or any combination, it should find those existing files on my system and add them together.
Last edited by oxoxo (2008-11-26 03:09:22)
Offline
Well sed does allow a bit more freedom, but it was not needed, not even for preserving the filename, because awk can print that too.
You should use locate if you don't know where the files are. Try this after updatedb:
awk '/Total/ { total+=$NF } END { print total}' $(locate myfile[31].txt)
Offline
Im taking a different approach here:
total="0"
for file in $*
do
echo "Processing $file"
while read line
do
if [[ $line =~ "^Total:" ]]
then
echo " $line"
tmp=`echo $line | cut -d':' -f2`
echo "$tmp"
total=`expr $total + $tmp`
echo "$total"
fi
done<$file
done
I got it working, but for some reason its not anymore, i really dont know what i changed.
The '+' to this script is that it would find out if the argument '$line' is on my system. Most likely the argument will be a file specification.
Offline
Pages: 1