Results 1 to 10 of 17
Hello everyone!
I am sure it's possible to do, but i can't resolve it in an "elegant" way:
- I need to read/extract few characters from a text file and ...
- 11-01-2007 #1Just Joined!
- Join Date
- Apr 2007
- Location
- St. Petersburg, Russia
- Posts
- 7
How to read a file and set a variable from it's certain characters
Hello everyone!
I am sure it's possible to do, but i can't resolve it in an "elegant" way:
- I need to read/extract few characters from a text file and set those contents as a variable.
Here is an example of that text file:
I need to read 3th line and set few variables: Year (4 first symbols in the second column), Month (6-7 characters from 2 column), Date, etc. from this line, so in the end i could export it as:Code:# test.txt input_filename: /data/sat/l0.pds start_time: 2007-10-20T09:27 stop_time: 2007-10-20T09:41
I tried using awk, but didn't achieve good results yet. Any idea how to do that? Any help is greatly appreciated.Code:YEAR=2007 MONTH=10 DAY=20
Misha
- 11-01-2007 #2
Could you please post your awk script as it stands so far, and show us what happened when you ran it? "Show us what happened" means copy and paste the output into your reply, preferably between CODE markers.
- 11-01-2007 #3Linux User
- Join Date
- Aug 2006
- Posts
- 458
in bash and GNU awk
Code:#!/bin/bash var=$(awk 'BEGIN{FS="[-:T]"}/start_time/{print $2,$3,$4}' "file") set -- $var YEAR=$1 MONTH=$2 DAY=$3 echo $YEAR $MONTH $DAY
- 11-01-2007 #4Just Joined!
- Join Date
- Apr 2007
- Location
- St. Petersburg, Russia
- Posts
- 7
Well, i haven't reached the end, so far i could only manage to get an output with second column of third line. And that i have done with (maybe ugly, but simple) command:
So, what i haven't managed to do is to read a part of the (now) single line and pipe those characters to set them as a variable...Code:$ tail -2 test.txt | head -1 | awk '{ print $2}'
- 11-01-2007 #5
I think we're all stumbling over each other. :)
ghostdog74's solution should work quite well.
- 11-01-2007 #6Just Joined!
- Join Date
- Apr 2007
- Location
- St. Petersburg, Russia
- Posts
- 7
Amazing! Thank you both very much!!
Now i am eager again to study awk manuals, though i can't find them as easy to read and implement...
Thanks once again!
- 11-01-2007 #7
Some people like normal manuals; others do better with tutorials. I was going to advise you to google this:
The problem is that awk is such an ancient language that many of the google search results point to dead links.Code:awk tutorial
You can ask google to show you the cached version of a page, but that will only work for tutorials which exist on a single page. The other kind, tutorials which start with a table of contents and then go to other pages, give you the same dead link problem.
Here is an awk tutorial which was cached by google and is all on one page. I changed the URL slightly so that the work "awk" isn't highlighted all over the place, as google likes to do. There are links in this page, but you don't really need them. One kind of dead link simply points back to the table of contents at the beginning of the page. Another kind of dead link points to where you can get the awk source files. But in each case that I looked at, the content of the source file was already right there on the page, so you can copy and paste.
Hope this helps.
- 11-01-2007 #8Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 11-01-2007 #9Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 11-01-2007 #10
With zsh:
Code:% cat test.txt # test.txt input_filename: /data/sat/l0.pds start_time: 2007-10-20T09:27 stop_time: 2007-10-20T09:41 % set -- "${=${(m)${(m)${(f)$(<test.txt)}#*start_time: }%%T*}//-/ }" % y=$1 m=$2 d=$3 % print -l $y $m $d 2007 10 20


Reply With Quote
