Filtering some data and storing the remaing to a variable.

Hi,

i have a problem writing a script.
Actually i have many files which contains some data, now the last line has some data as in the format : 2556||04-04-10
now i want to do tail -1 filename.txt and store the result into a variable which will be used later for some other calculations.
My problem is how to include jus first numeric digits (2556 here) and store date in another variable (04-04-10 here).
These stored data would be then stored into some other text file.I have multiple files so would be creating script for this.

Please help me out with the solution.
Thanks :slight_smile:
Tc.

something like?

var1=$(tail -1 file | awk -F "|" '{print $1}')
var2=$(tail -1 file | awk -F "|" '{print $3}')

Thanks Anchal for the help.....but I am getting following error.... :frowning:

$ tail -1 a.txt | awk -F "|" '{print $1}'
awk: syntax error near line 1
awk: bailing out near line 1

n cant sort where is the error..!:confused:

To loop through the *.txt files in a directory you can do something like:

for file in *.txt
 tail -1 "$file" | awk -F"|" '{print $1, $NF}' | read num dat
 # Do something with $num and $dat
 # other stuff
done

Use nawk or /usr/xpg4/bin/awk on Solaris.

eval `awk -F\| 'END{printf "var1=%s var2=%s",$1,$3}' file`
L="$(tail -1 file)"
NUM=${L%%|*}
DAT=${L##*|}

Thanks Franklin, danmero and frans. !!!

Gr8 code frans. :cool:

Now workin on makin num and dat as array so as to use it in later stage to format my output...!
lets c if i can, or else wil revert back to u al guys again...:stuck_out_tongue:

Hi All,

I have some files in my directory, and i want to pull all data using for loop....I am using following code but getting error..!

for file in {file1, file2, file3, ..... filen}
do
 L="$(tail -1 $file)";NUM=${L%%|*};DAT=${L##*|}
 echo $NUM>>filedata.txt
done

Error:

tail: cannot open input
tail: cannot open input

i tried with other codes, some gave blank data and some something else.....but can understand where the prob is.... :frowning:

please help