Find the answer to your Linux question:
Results 1 to 5 of 5
I have a file like in following format Hello:World universe Hello world universe hello Hello world universe:hello hello universe hello world hello universe hello world now I want to print ...
  1. #1
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114

    How print Last String of the Line in Shell

    I have a file like in following format

    Hello:World universe
    Hello world universe hello
    Hello world universe:hello
    hello universe hello world hello
    universe hello world

    now I want to print last <string> of all lines, is there any way??
    universe
    hello
    universe:hello
    hello
    world


    thanks
    Switched to Scripting

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    Yes, it's quite simple using awk:

    Code:
    awk '{ print $NF }' <filename>
    Where <filename> is replaced with your file
    Linux User #453176

  3. #3
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114
    Thanks, now I am facing some problem, if u can help me.

    i am getting trailing line, how can I remove that, see below

    $test = ssh hostname cat /proc/cpuinfo |grep -i 'model name'
    echo "$test"

    (standard input):model name : Intel(R) XEON(TM) CPU 2.40GHz
    <trailing line>

    And when I am checking value in variable, nothing is there

    if [ -n $test ]; then
    echo "$test"
    else
    echo "Error"
    fi

    but nothing is happening, even else is also not executing
    Switched to Scripting

  4. #4
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    You need to use some quotes. Try:

    Code:
    $test = `ssh hostname cat /proc/cpuinfo |grep -i 'model name'`
    echo "$test"
    The quotes I've used can be found by using shift and the key above the tab key.
    Linux User #453176

  5. #5
    Just Joined! chochem's Avatar
    Join Date
    Feb 2008
    Posts
    7
    Quote Originally Posted by jaigs_27 View Post
    I have a file like in following format

    Hello:World universe
    Hello world universe hello
    Hello world universe:hello
    hello universe hello world hello
    universe hello world

    now I want to print last <string> of all lines, is there any way??
    universe
    hello
    universe:hello
    hello
    world


    thanks
    I believe grep could do it with:
    Code:
     grep -o -E -e '[^ ]+$' <filename>
    Not that it's any easier to read, mind you...

Posting Permissions

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