Find the answer to your Linux question:
Results 1 to 6 of 6
Hello Everyone, I need your help again. I can't seem to figure out how to using either sed or awk to delete a space before and after a tab. I ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75

    Delete space before and after tab

    Hello Everyone,

    I need your help again. I can't seem to figure out how to using either sed or awk to delete a space before and after a tab. I have tried various lines that I could find and I either end up taking out all the spaces or all the tabs.

    Here is the file right now. I used sed to find all the tabs and replace with the word TAB since I know the forums won't show it.

    Code:
    1 TAB ACCT11111111 TAB SERVER1\Backup of files on C$,System State and Services Database\admin01 TAB 22606 TAB 6551
    2 TAB ACCT11111111 TAB SERVER1\Backup of Microsoft SQL Server\admin01 TAB 5138 TAB 229
    3 TAB ACCT11111111 TAB SERVER2\Backup of files on C$,System State and Services Database\admin01 TAB 5990 TAB 1549
    4 TAB ACCT11111111 TAB SERVER2\Backup of Microsoft SQL Server\admin01 TAB 19 TAB 1
    5 TAB ACCT11111111 TAB SQL1\192.168.100.10\Backup of  information_schema, lava, SQL1\admin01 TAB 15 TAB 1
    6 TAB ACCT11111111 TAB UNIX-SSH\172.16.0.3\Backup of  /\admin01 TAB 12948 TAB 987
    7 TAB ACCT11111111 TAB UNIX-SSH\192.168.2.5\Backup of  /\admin01 TAB 12 TAB 8
    8 TAB ACCT11111111 TAB TECHIT1\Backup of files on C$, System State  and Services Database\admin01 TAB 12772 TAB 3988
    9 TAB ACCT11111111 TAB SERVER3\Backup of files on C$,System State and Services Database\admin01 TAB 5765 TAB 1399
    What I would like to see is

    Code:
    ACCT11111111TABSERVER1\Backup of files on C$,System State and Services Database\admin01
    ACCT11111111TABSERVER1\Backup of Microsoft SQL Server\admin01
    ACCT11111111TABSERVER2\Backup of files on C$,System State and Services Database\admin01
    ACCT11111111TABSERVER2\Backup of Microsoft SQL Server\admin01
    ACCT11111111TABSQL1\192.168.100.10\Backup of  information_schema, lava, SQL1\admin01
    ACCT11111111TABUNIX-SSH\172.16.0.3\Backup of  /\admin01
    ACCT11111111TABUNIX-SSH\192.168.2.5\Backup of  /\admin01
    ACCT11111111TABTECHIT1\Backup of files on C$, System State  and Services Database\admin01
    ACCT11111111TABSERVER3\Backup of files on C$,System State and Services Database\admin01

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by Korelis View Post
    Hello Everyone,

    I need your help again. I can't seem to figure out how to using either sed or awk to delete a space before and after a tab. I have tried various lines that I could find and I either end up taking out all the spaces or all the tabs.

    Here is the file right now. I used sed to find all the tabs and replace with the word TAB since I know the forums won't show it.

    Code:
    1 TAB ACCT11111111 TAB SERVER1\Backup of files on C$,System State and Services Database\admin01 TAB 22606 TAB 6551
    2 TAB ACCT11111111 TAB SERVER1\Backup of Microsoft SQL Server\admin01 TAB 5138 TAB 229
    3 TAB ACCT11111111 TAB SERVER2\Backup of files on C$,System State and Services Database\admin01 TAB 5990 TAB 1549
    4 TAB ACCT11111111 TAB SERVER2\Backup of Microsoft SQL Server\admin01 TAB 19 TAB 1
    5 TAB ACCT11111111 TAB SQL1\192.168.100.10\Backup of  information_schema, lava, SQL1\admin01 TAB 15 TAB 1
    6 TAB ACCT11111111 TAB UNIX-SSH\172.16.0.3\Backup of  /\admin01 TAB 12948 TAB 987
    7 TAB ACCT11111111 TAB UNIX-SSH\192.168.2.5\Backup of  /\admin01 TAB 12 TAB 8
    8 TAB ACCT11111111 TAB TECHIT1\Backup of files on C$, System State  and Services Database\admin01 TAB 12772 TAB 3988
    9 TAB ACCT11111111 TAB SERVER3\Backup of files on C$,System State and Services Database\admin01 TAB 5765 TAB 1399
    What I would like to see is

    Code:
    ACCT11111111TABSERVER1\Backup of files on C$,System State and Services Database\admin01
    ACCT11111111TABSERVER1\Backup of Microsoft SQL Server\admin01
    ACCT11111111TABSERVER2\Backup of files on C$,System State and Services Database\admin01
    ACCT11111111TABSERVER2\Backup of Microsoft SQL Server\admin01
    ACCT11111111TABSQL1\192.168.100.10\Backup of  information_schema, lava, SQL1\admin01
    ACCT11111111TABUNIX-SSH\172.16.0.3\Backup of  /\admin01
    ACCT11111111TABUNIX-SSH\192.168.2.5\Backup of  /\admin01
    ACCT11111111TABTECHIT1\Backup of files on C$, System State  and Services Database\admin01
    ACCT11111111TABSERVER3\Backup of files on C$,System State and Services Database\admin01
    If we call "x" that data file, then something in the lines of the following, should work (in bash):

    #!/bin/bash
    cat x | \
    while read line
    do
    var=${line/[1234567890] /}
    var=${var/ TAB /TAB}
    echo ${var/TAB /TAB}
    done
    If you want, you can use sh instead of bash, but then the ${//} stuff will not work, and you are bound to use sed (which will be uglier, but more portable).

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75
    Thank you for the response. I was testing out the code and it is not reporting a TAB instead it changes it to a single whitespace. It also is removing out the \ characters. I do need those .

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by Korelis View Post
    Thank you for the response. I was testing out the code and it is not reporting a TAB instead it changes it to a single whitespace. It also is removing out the \ characters. I do need those .
    Umm, I understood "TAB" as a literal string hehe. I think that what you want is this:

    Code:
    #!/bin/bash
    cat x | \
    while read line
    do
    var=${line/[1234567890] /}
    var=${var/ \\t /\\t}
    echo "${var}"
    done

  5. #5
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75
    Quote Originally Posted by i92guboj View Post
    Umm, I understood "TAB" as a literal string hehe. I think that what you want is this:

    Code:
    #!/bin/bash
    cat x | \
    while read line
    do
    var=${line/[1234567890] /}
    var=${var/ \\t /\\t}
    echo "${var}"
    done
    That is still showing the space tab space when I run it. Basically I want the file to look like

    ACCT11111111<TAB>SERVER1\Backup of files on C$,System State and Services Database\admin01<TAB>

    I do not want a space after that last number before <TAB> and I do not want a space before the > in <TAB> on the first column. I do want the tab. I hope this helps.

  6. #6
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Try this using sed. \t signifies the TAB character in sed:

    Code:
    sed 's/ *\t */\t/g' <file>
    This will delete 1 or more spaces before and/or after a TAB.

Posting Permissions

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