Results 1 to 7 of 7

Thread: how to get a substring extracted from a line in a variable

  1. #1
    Join Date
    Oct 2010
    Posts
    96

    how to get a substring extracted from a line in a variable

    Hello everyone I am just a beginner with linux and yesterday I have heard something about shell scripting. I really dont know how to develop scripts in linux, although I have visited many sites that shows how to do scripting but still can not make it how to do. I would want to know something about sub-string, where i can read the line from file/directory and based on the line I can put the whole thing in a variable after separating the information from that line. So any one with a knowledge of the scripting in linux can provide me with some solution. I would be very thankful for who ever comes up with perfect solution.

  2. #2
    Join Date
    Apr 2008
    Posts
    4,642

    Re: how to get a substring extracted from a line in a variable

    Can you provide me with the your scripts which you have developed or any kind of example as such so that I can show you how to carry on the operation of the substring. Its quite easy to develop this provided you have any knowledge about it. I can only help you to sort out some issue which you are having in finding out the substring , So please provide it with the scripts which you want to be substring. Just by looking at what I have done you will be able to know how to get a substring in a variable.

  3. #3
    Join Date
    Oct 2010
    Posts
    96

    Re: how to get a substring extracted from a line in a variable

    This is my subscript I have managed to develop by myself by learning from the websites but not able to substring a line into a variable.
    • !/bin/sh
    • dir=/home/user/input/test.log #(this is the input file)
    • msg=`tail -1 $dir`
    • #(it will take only the last line as log file will be updated every day)
    • echo "$msg"

    The output of the above script is something like this:
    file_name successfully ran on Mon Jul 12 23:15:00 SST 2009.
    Now, I would want to extract name, date,time,status so the output should be like this
    • name date time status
    • file_name Mon Jul 12 23:15:00 success


    So if I have to substring it into a variable then I have to perform the following task
    • !/bin/sh
    • dir=/home/user/input/test.log #(this is the input file)
    • msg=`tail -1 $dir`
    • #(it will take only the last line as log file will be updated every day)
    • sub= $msg
    • name=${msg:0:8}
    • echo "$msg"
    • echo "name

    But I am getting and error message here. So please can someone help me to correct this code so that I can get proper output.

  4. #4
    Join Date
    May 2008
    Posts
    4,570

    Re: how to get a substring extracted from a line in a variable

    I would like to inform you that the command to extract the substring which you have mentioned in your previous post is a valid one. But one thing you have to make sure that you are running the Bash command, for example consider the following code.
    • jlinkels@jlinkels-lt:~$ which sh
    • /bin/sh
    • jlinkels@jlinkels-lt:~$ file /bin/sh
    • /bin/sh: symbolic link to `bash'
    • jlinkels@jlinkels-lt:~$

    Also you can run your script using the sh command like "bash test.sh" Or you could also make use the "bash -x test.sh" to make it look even more better. This two commands which I have used will make sure that every line of code is executed and echoed correctly..

  5. #5
    Join Date
    Apr 2008
    Posts
    2,139

    Re: how to get a substring extracted from a line in a variable

    If you want to make sure that your script file is perfectly running with the bash command. You have to make sure that you dont have any space in the file_name, time or status strings. If it is there than remove it out because it might be causing the error when you are trying to substring it into the variable. The compiler would stop the cutting of the line as soon as he sees any space in the string.
    I have made a dummy script for you hope you can get a clear idea of what I am trying to say
    • name=${msg%% *} # remove 1st space add all following
    • status=${msg#* } # remove 1st space and all proceeding
    • status=${status%% *} # remove 1st space add all following
    • datetime=${msg##*ran on } # remove "ran on " and all proceeding
    • datetime=${datetime%% SST *} # remove " SST " and all following
    • time=${datetime##* } # remove last space and all proceeding
    • date=${datetime% *} # remove last space and all following

  6. #6
    Join Date
    Oct 2010
    Posts
    96

    Re: how to get a substring extracted from a line in a variable

    Hey I have done exactly as you told me to do and have got the output, thanks for the solution, but now the problem which has come up is that the position of the string always changes. I dont know what is causing this problem, but i guess that the script is no longer efficient.
    i've tried to search and did some trial by myself but the error still persist.
    • #!/bin/sh
    • dir=/home/user/input/test.log
    • #cat $dir
    • msg=`tail -1 $dir`
    • sub=`echo ${msg:6:23}`
    • echo "$sub"
    • #name=${msg:0:21}
    • echo "$msg"
    • #echo "$name"

    Can any one help me to get the script in a perfect position and no need to set it again and again.

  7. #7
    Join Date
    May 2008
    Posts
    3,316

    Re: how to get a substring extracted from a line in a variable

    I think that you are making use of big names in the string which is not allowing you to settle in one line and thus the position of the string changes while you give command to get the output. In order to solve this you must make use of small names or you can also use "awk" to required to the return field which will help to get the output only in one line.
    For example
    • awk 'END{print $1}' $dir

    This will return the file_name in only one line by adjusting the spaces in and you will definatley get the desired output

Similar Threads

  1. tar.gz file for gmote when extracted leaves me with a .sh
    By AbiCgail in forum Operating Systems
    Replies: 8
    Last Post: 10-09-2011, 11:10 PM
  2. Assign each line of a text file to a variable
    By hitman126 in forum Operating Systems
    Replies: 1
    Last Post: 22-01-2011, 07:42 AM
  3. Substring with a regex
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 02-04-2010, 11:06 AM
  4. Replies: 2
    Last Post: 28-08-2009, 07:51 PM
  5. Remove system environment variable using command line ?
    By Vilius Mockûnas in forum Windows Server Help
    Replies: 1
    Last Post: 12-06-2009, 04:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,320,863.78929 seconds with 17 queries