Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Just 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:

    Code:
    # test.txt
    input_filename:  /data/sat/l0.pds
    start_time:  2007-10-20T09:27
    stop_time:  2007-10-20T09:41
    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:
    YEAR=2007
    MONTH=10
    DAY=20
    I tried using awk, but didn't achieve good results yet. Any idea how to do that? Any help is greatly appreciated.

    Misha

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.

  3. #3
    Linux 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

  4. #4
    Just 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:
    Code:
    $ tail -2 test.txt | head -1 | awk '{ print $2}'
    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...

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I think we're all stumbling over each other. :)

    ghostdog74's solution should work quite well.

  6. #6
    Just 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!

  7. #7
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Some people like normal manuals; others do better with tutorials. I was going to advise you to google this:
    Code:
    awk tutorial
    The problem is that awk is such an ancient language that many of the google search results point to dead links.

    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.

  8. #8
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    i always recommend this. That's where i learnt my first awk. However, for GNU awk, you can look at the official manual

  9. #9
    drl
    drl is online now
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi, ghostdog74.
    Quote Originally Posted by ghostdog74 View Post
    i always recommend this. That's where i learnt my first awk. However, for GNU awk, you can look at the official manual
    I haven' t been able to see anything on the .ua site except for "page deleted" (уничтоженная страница).

    Do you get something other than that? ... cheers, drl
    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 )

  10. #10
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    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

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...