Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
hi my situation is like this i am extracting a substring from a string its like , i do a uname -r and if the output is 2.6.11-1.1369_FC4smp, i need ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141

    extracting a substring from a string


    hi

    my situation is like this
    i am extracting a substring from a string
    its like , i do a uname -r
    and if the output is 2.6.11-1.1369_FC4smp, i need to extract the characters till '-' to another string say str

    so my final output should be somewhat like this the variable str should contain "2.6.11"

    i tried doing it
    and got so far .

    uname -r | awk '{print NR ,substr($path,0,index($path,"-")-1) }'

    This will give the output as

    "1 2.6.11" in the variable str

    how do i get rid of the 1 also since i need only 2.6.11?

    any ideas?

    thanks

  2. #2
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    one more thing $path had the output of "uname -r" already

  3. #3
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    sorry the command i gave was this

    str1= uname -r | awk '{print NR ,substr($0,0,index($0,"-")-1) }'

    and not the previous one which had the path variable in it

  4. #4
    Just Joined!
    Join Date
    Jun 2005
    Location
    Canada, Halifax
    Posts
    86
    Code:
    $ uname -r | sed 's/#.*//'

  5. #5
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    i tried out that it is just giving me the following output

    [root@macubex macubex]# uname -r | sed 's/#-*//'
    2.6.11-1.1369_FC4smp


    [/code]

  6. #6
    Just Joined!
    Join Date
    Jun 2005
    Location
    Canada, Halifax
    Posts
    86
    You replaced the period with a dash.

  7. #7
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Here's another way to do it:
    Code:
    uname -r | cut -f 1 -d '-'
    There are many ways. Gotta love *nix.

  8. #8
    Just Joined!
    Join Date
    Jun 2005
    Location
    Canada, Halifax
    Posts
    86

    Oops

    Oops, replace my pound with your dash. The slackware box I tried the command with produced slightly different output than yours. I apologize for any confusion caused by my oversight.

  9. #9
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    thanks guys
    anyway i went around this problem in a totally different manner
    it was like i wanted to include a $(kdir) in a makefile and this variable should change for FC3 and FC4

    /usr/src/redhat/BUILD/kernel-2.6.9/linux-2.6.9 for FC3

    /usr/src/redhat/BUILD/kernel-2.6.9/linux-2.6.9 for FC4

    so i thought of giving the variabel in a generic manner by getting the 2.6.9 part and then concatenating it

    anyway i went about it in another manner by creating a symbolic link and making the link in a .sh file from which the makefile was called

    thanks for the help

  10. #10
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    i tried the same thing out with the substr command

    Code:
    a=`uname -r`
    path=`expr substr $a 2 6`.
    This wil also get u the string provided the starting position and the ending position is given correctly

    Note
    ****
    the starting position is 1 and not 0

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
  •