Register
It is currently Sun Aug 31, 2014 4:22 am

extract a var from text-file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 16 posts ] 
Author Message
 PostPosted: Sun Dec 03, 2006 7:31 pm   

Joined: Sun Dec 03, 2006 6:06 pm
Posts: 10
hi,
i've a text file like this:

The Optimum Index Factor analysis result:
(Best combination comes first.)
147: 66.7305
247: 63.6313
145: 60.718
347: 57.3675
245: 57.0204
457: 56.3514
345: 54.5108
124: 54.443
134: 47.9291
234: 40.9564
135: 30.5729
157: 29.1525
125: 28.692
357: 28.5414
257: 26.2344
235: 26.0987
137: 26.0821
127: 25.33
237: 22.2289
123: 21.7889

i have bash script that generayte this text file.
in it at line 3 there is a combination of 3 numbers.
(in the file that i paste here, this combination is: 147)
my targhet is :
create an other script (few lines of code) to: read the text file and extract the 3 number...in 3 new var named:
var1=1, var2=4, var3=7

any help is most apprechated!
thanks a lot.
epifanio


Top
 Profile  
 PostPosted: Mon Dec 04, 2006 5:28 am   

Joined: Sun Dec 03, 2006 6:06 pm
Posts: 10
with this line:
cat /Users/epifanio/oifp | sed -e '1,2d' | awk '{print $1}' | sed 's/://' | sed -e '2,20d'

i've this result:

147

with this number '147'
i want create 3 new variable that are like :

TM_1
TM_4
TM_7

how can i perform this?
thanks a lot.
epifanio


Top
 Profile  
 PostPosted: Wed Dec 06, 2006 12:02 am   

Joined: Tue Dec 05, 2006 8:37 pm
Posts: 30
Location: Thiruvananthapuram / India
Hi,

Here is the solution:

TM_1=$(cat /Users/epifanio/oifp | sed '1,2d' | awk -F: '{print $1}' | sed '2,20d' | sed 's/\([0-9]\)/\1 /g' | awk '{print $1}')
TM_4=$(cat /Users/epifanio/oifp | sed '1,2d' | awk -F: '{print $1}' | sed '2,20d' | sed 's/\([0-9]\)/\1 /g' | awk '{print $2}')
TM_7=$(cat /Users/epifanio/oifp | sed '1,2d' | awk -F: '{print $1}' | sed '2,20d' | sed 's/\([0-9]\)/\1 /g' | awk '{print $3}')


Top
 Profile WWW YIM  
 PostPosted: Wed Dec 06, 2006 2:39 am   

Joined: Sun Dec 03, 2006 6:06 pm
Posts: 10
:)
great,

thanks for your help!


Top
 Profile  
 PostPosted: Wed Dec 06, 2006 11:42 pm   

Joined: Tue Dec 05, 2006 8:37 pm
Posts: 30
Location: Thiruvananthapuram / India
Hi,

Its a gr8 news if it helped you coz it was my first day with bashscripts.org and it was my first solution on this wonderful site 8)


Top
 Profile WWW YIM  
 PostPosted: Thu Dec 14, 2006 4:56 am   

Joined: Sun Dec 03, 2006 6:06 pm
Posts: 10
Hi bash-man,
i think that is unuseffol open an other topik, so i've an other question to manage a text "string".
in a bash script i've an input string like this:

mappa_1, mappa_2, mappa_3, .... , .... , .... , mappa_n

my target is to trasform the ^^ line in to :

"mappa_1", "mappa_2", "mappa_3", .... , .... , .... , "mappa_n"


i.e. i wont just add a double quote for each var: mappa_x
i think that the magic command "awk" can help me, but i do not know how :?
thanks a lot for any suggestion!


Top
 Profile  
 PostPosted: Thu Dec 14, 2006 7:19 am   

Joined: Tue Dec 05, 2006 8:37 pm
Posts: 30
Location: Thiruvananthapuram / India
Hi,

This can be achieved with the following 'sed' script:

assume data.txt is the input file,

sed -i 's/mappa-[0-9]*/"&"/g' data.txt

After executing the above sed command data.txt will contain all 'mappa-x' strings double quoted.


Top
 Profile WWW YIM  
 PostPosted: Thu Dec 14, 2006 7:20 am   

Joined: Tue Dec 05, 2006 8:37 pm
Posts: 30
Location: Thiruvananthapuram / India
A small correction:

sed -i 's/mappa_[0-9]*/"&"/g' data.txt


Top
 Profile WWW YIM  
 PostPosted: Thu Dec 14, 2006 10:38 am   

Joined: Sun Dec 03, 2006 6:06 pm
Posts: 10
hi i've done it!
now oll wors fine :-)
i used this code :

STRING=`echo $mystring | sed s/,/\ /g`

for i in $STRING
do
STRING2="$STRING2,\"$i\""

done

STRING2=`echo $STRING2 | sed s/^,/\ /`

thnks!!!


Top
 Profile  
 PostPosted: Sun Dec 17, 2006 2:08 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
lijeesh wrote:
Hi,

Here is the solution:

TM_1=$(cat /Users/epifanio/oifp | sed '1,2d' | awk -F: '{print $1}' | sed '2,20d' | sed 's/\([0-9]\)/\1 /g' | awk '{print $1}')
TM_4=$(cat /Users/epifanio/oifp | sed '1,2d' | awk -F: '{print $1}' | sed '2,20d' | sed 's/\([0-9]\)/\1 /g' | awk '{print $2}')
TM_7=$(cat /Users/epifanio/oifp | sed '1,2d' | awk -F: '{print $1}' | sed '2,20d' | sed 's/\([0-9]\)/\1 /g' | awk '{print $3}')


the above seems like alot of work...

Code:
until ! read line
do
    var_whole=`echo $line | cut -d: -f1`
    var1=${var_whole:0:1}
    var2=${var_whole:1:1}
    var3=${var_whole:2:1}
done < file_to_read.txt


now the above assumes you only have 3 characters...
ofcourse there are countless ways to do this... that's just one of the great things about bash... it basically boils down to, how do *you* want to do it :wink:


Top
 Profile  
 PostPosted: Tue Feb 20, 2007 7:20 am   

Joined: Sun Dec 03, 2006 6:06 pm
Posts: 10
hi,

i've a 2 text file like this:


file one:

X1 Y1 Z1
....
....
Xn Yn Zn

file two:

XX1 YY1
...
...
XXn YYn


i want generate a new file like:

XX1 YY1 Z
....
....
XXn YYn Zn


have you a solution?

p.s.:
i think again that post an other topic isn't a ggod idea, beacouse my question is again a "text file management"
we can change the title of the post from :
extract a var from text-file
to
manage text file
so it is
what do you think about?

thanks!


Top
 Profile  
 PostPosted: Tue Feb 20, 2007 8:25 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 666
Location: Des Moines, Iowa
epifanio,

This isn't homework is it ;) ?


Top
 Profile WWW  
 PostPosted: Tue Feb 20, 2007 8:35 am   

Joined: Sun Dec 03, 2006 6:06 pm
Posts: 10
ok :-)
i'm not an informatic student, i'm a biologist
sorry :-(

this forum gived me a great help to undstand the bash command.
not only a solution to problems, but a description on how the coomand works...
so thanks again for your preciouse help.

to my purpose can i use the cat + sed commad to exstract the column from the first file
then use awk to print it in the targhrt file, but i do not know how use the right sintax...
...i'm tring by myself, but it is hard
:oops:


Top
 Profile  
 PostPosted: Tue Feb 20, 2007 9:11 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 666
Location: Des Moines, Iowa
Ok, just checking.

Do you have more of a data file that we could use.... and the output it "should" generate ?


Top
 Profile WWW  
 PostPosted: Tue Feb 20, 2007 2:59 pm   

Joined: Sun Dec 03, 2006 6:06 pm
Posts: 10
hi,

this is a sample of the file one :


2524464.205 4510655.961 12
2524459.58400078 4510657.39162497 15
2524455.06343952 4510658.51987919 11
2524454.4559001 4510662.2213528 45
2524458.4969279 4510663.26737144 15
2524462.17553609 4510660.29479857 14
2524464.205 4510655.961 18

this is a sample of the file two :

# Created by Octave 2.1.73, Tue Feb 20 13:25:09 2007 CET <epifanio@computer-di-epifanio.local>
# name: E_N_2
# type: matrix
# rows: 176139
# columns: 2
504455.753011171 4510648.91403669
504451.132009197 4510650.34461556
504446.61144776 4510651.4728253
504446.003875401 4510655.17428485
504450.044884308 4510656.22033858
504453.723511693 4510653.24780659
504455.753011171 4510648.91403669


my results must be like this :

504455.753011171 4510648.91403669 12
504451.132009197 4510650.34461556 15
504446.61144776 4510651.4728253 11
504446.003875401 4510655.17428485 45
504450.044884308 4510656.22033858 15
504453.723511693 4510653.24780659 14
504455.753011171 4510648.91403669 18

i.e:
i need to add the third column of the file one to the file two
:oops:

i know tha it is a banal question, i tried using sed to eliminate the comment row
and the use again sed in various mode but nathig is similar to my targhrt :-(
i'm able "using sed" to work with the row of my file, but i must manage the column...
i'm confused


Top
 Profile  
 PostPosted: Tue Feb 20, 2007 10:25 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
so you need the last field of the first file appended to the end of each line on the second file...
[edit]
you would need to remove the comment lines in the beginning of the files if they exist
Code:
grep -v ^\# file1 > tmp && mv tmp file1

ofcourse remove any empty lines from each of them also...
i'll let you find the sed code on that one (hint: check here)
Code:
cut -d" " -f3 file1 > tmp
paste -d" " file2 tmp > file.merged


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP