Find the answer to your Linux question:
Results 1 to 6 of 6
Hi iam a new user in this forum and Linux. I have a string like this "/home/test/filename.txt" and i want to delete all character after the last "/". how to ...
  1. #1
    Just Joined!
    Join Date
    Jun 2010
    Posts
    8

    How to remove the last character.

    Hi iam a new user in this forum and Linux.
    I have a string like this "/home/test/filename.txt"
    and i want to delete all character after the last "/".
    how to do that using sed or awk.
    sorry my english is bad.

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    welcome to the forum

    if your input is called A, then something like this might work:

    tr / \\n <A | sed '$d' | tr \\n / >B
    the sun is new every day (heraclitus)

  3. #3
    Just Joined!
    Join Date
    Jun 2010
    Posts
    8
    Thanks for answer, its work
    Can you explain that

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Using built-ins in shell bash allows one to avoid external programs:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate bash in-built syntax for string manipulation.
    
    echo
    t1="/home/test/filename.txt"
    echo " Original: $t1"
    
    echo
    echo " Modified: ${t1%/*}/"
    
    exit 0
    producing:
    Code:
    % ./s1
    
     Original: /home/test/filename.txt
    
     Modified: /home/test/
    See topic Parameter Expansion in man bash for details ... 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 )

  5. #5
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    Also, the dirname program does this for files specifically.

  6. #6
    Just Joined!
    Join Date
    Jun 2010
    Posts
    8
    Thank you very much.

    can anyone explain this
    tr / \\n <A | sed '$d' | tr \\n / >B

Posting Permissions

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