Filtering some data and storing the remaing to a variable.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filtering some data and storing the remaing to a variable.
# 1  
Old 04-11-2010
Question 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 Smilie
Tc.
# 2  
Old 04-11-2010
something like?
Code:
var1=$(tail -1 file | awk -F "|" '{print $1}')
var2=$(tail -1 file | awk -F "|" '{print $3}')

# 3  
Old 04-11-2010
Thanks Anchal for the help.....but I am getting following error.... Smilie

$ 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..!Smilie
# 4  
Old 04-11-2010
To loop through the *.txt files in a directory you can do something like:
Code:
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.
# 5  
Old 04-11-2010
Code:
eval `awk -F\| 'END{printf "var1=%s var2=%s",$1,$3}' file`

# 6  
Old 04-11-2010
Code:
L="$(tail -1 file)"
NUM=${L%%|*}
DAT=${L##*|}

# 7  
Old 04-12-2010
Thanks Franklin, danmero and frans. !!!

Gr8 code frans. Smilie

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...Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

2. Shell Programming and Scripting

storing large data in unix array variable

Hi, I have table in sql ..from this table im storing the first coloumn values in shell array variable ... after this passing this variable as an arugument in SQL procedure. But the proc. is running fine only for 1024 values in array ... How to store more than 1024 values in the array... (5 Replies)
Discussion started by: ankitknit
5 Replies

3. UNIX for Dummies Questions & Answers

Data storing and chart

I apologize if this has been posted before but I have been doing a lot of searching and I have not found any real answers, this is quite possibly due to that I may not be searching for the right terms or something. Little bit of info about my situation is that I'm a newbie as you probably guessed... (1 Reply)
Discussion started by: ummSomeNameHere
1 Replies

4. Shell Programming and Scripting

About storing the value of wc -l into a variable and then using this value in while

Hi all, I m new to this forum. I ma facing onei issue. I have something like this: length= wc -l < b2| awk '{print $1}' where b2 is filename having detauls like: cat b2 abc1 abc4 xyc3 sbdghf4 but when I do echo "$length" it displays nothing Also I am using awk to overcome... (4 Replies)
Discussion started by: student2009
4 Replies

5. Shell Programming and Scripting

Storing value in a variable

Hi Everyone, I have a code which requires to be stored in different variables and I am achiving it like this. HOST=`echo $RMP | cut -f2 -d:` NAME=`echo $RMP | cut -f3 -d:` DIR=`echo $RMP | cut -f4 -d:` TYPE=`echo $RMP | cut -f5 -d:` Is there any other way of storing value... (2 Replies)
Discussion started by: gehlnar
2 Replies

6. UNIX for Dummies Questions & Answers

Storing data to many files

Please help me store the content of Input file to many files. Input file: record a b c record d record e f record ..... Create file1: a b c Create file2: (1 Reply)
Discussion started by: buddyme
1 Replies

7. Shell Programming and Scripting

Storing a variable?

I'm writing a bash shell script to backup several mysql databases. This script will run on a daily basis and send a copy to a remote FTP repository. The filenames are in the format DATE.backup.sql. How do I store the DATE variable so I can delete/move/etc the file on the FTP server the next time... (4 Replies)
Discussion started by: hoover90
4 Replies

8. UNIX for Dummies Questions & Answers

Filtering Data

file1 contain: (this just a small sample of data it may have thousand of lines) 1 aaa 1/01/1975 delhi 2 bbb 2/03/1977 mumbai 3 ccc 1/01/1975 mumbai 4 ddd 2/03/1977 chennai 5 aaa 1/01/1975 kolkatta 6 bbb 2/03/1977 bangalore program: nawk '{ idx= $2 SUBSEP $3 arr = (idx in arr) ?... (2 Replies)
Discussion started by: bobo
2 Replies

9. UNIX for Dummies Questions & Answers

Storing data of files

Hi, How to store datas of the file into a string? Thanks in advance.. (1 Reply)
Discussion started by: Fr0z3n999
1 Replies

10. UNIX for Dummies Questions & Answers

Filtering out data ...

I have following command which tells me File size in GBs which are greater than 0.01GBs recursively in a dir structure. ls -l -R | awk '{ if ($5/1073741824 >= 0.01) print $9, $5/1073741824 }' But there are some files whom I dont have enough permissions, after executing this script gives me... (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question