Results 1 to 5 of 5

Thread: Command to replace tab characters in a file?

  1. #1
    Join Date
    Nov 2005
    Location
    Uppsala, Sweden
    Beans
    2,180
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Command to replace tab characters in a file?

    I'm trying to make patches to make the old nvidia soundstorm drivers work on current kernels (see my howto here).
    One of the problems right now is that one of the files that needs to be patched contains leading tab characters in stead of spaces on some lines, which makes the patch command fail.

    Does anyone know any way to do of any of these:
    1. A way to get the patch command to work with lines containing tab characters?
    2. A way to replace tab characters with spaces on specific lines of a file?
    3. A way to replace ALL tab characters (or all leading tab characters) in a file with spaces?
    4. Any other way to replace the text I need to change (some sed command I can include in a script)?

    Specifically, I need a command to change this (but with tabs instead of "<tab>"):
    Code:
    <tab><tab><tab><tab><tab><tab><tab><tab><tab>:"=m"(currenttask->thread.i387.fxsave) );
    Into this (tabs or spaces doesn't really matter, as long as the actual code in red gets changed):
    Code:
                                    :"=m"(currenttask->thread.xstate->fxsave) );

  2. #2
    Join Date
    Jan 2006
    Location
    Virginia
    Beans
    1,870

    Re: Command to replace tab characters in a file?

    Okay, well, to do that replace, I would so something like this:

    put this in a command for sed to use (too tedious to let bash see all the extraneous characters that you might otherwise have to escape), lets call this file 'scriptforsed'.

    Code:
    s/:"=m"(currenttask->thread.i387.fxsave) );/ :"=m"(currenttask->thread.xstate->fxsave) );/
    Now, lets say you want to do the replacement in a file called '[color=blue]somefile[/code]' We'll need to use a command like this one to do it:
    Code:
     sed -f scriptforsed -i somefile
    For the replacing of the tabs, I would use tr ... an example of its use might be ...
    Code:
    cat somefile | tr '\t' ' ' > somefile
    Don't know about making things more specific, but this should work for at least a couple of your needs.
    "I refuse to be part of a society that encourages the rampant abuse of its own language." ~ The Black Mage

  3. #3
    Join Date
    Nov 2005
    Location
    Uppsala, Sweden
    Beans
    2,180
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Command to replace tab characters in a file?

    Quote Originally Posted by kuja View Post
    Okay, well, to do that replace, I would so something like this:

    put this in a command for sed to use (too tedious to let bash see all the extraneous characters that you might otherwise have to escape), lets call this file 'scriptforsed'.

    Code:
    s/:"=m"(currenttask->thread.i387.fxsave) );/ :"=m"(currenttask->thread.xstate->fxsave) );/
    Now, lets say you want to do the replacement in a file called '[color=blue]somefile[/code]' We'll need to use a command like this one to do it:
    Code:
     sed -f scriptforsed -i somefile
    For the replacing of the tabs, I would use tr ... an example of its use might be ...
    Code:
    cat somefile | tr '\t' ' ' > somefile
    Don't know about making things more specific, but this should work for at least a couple of your needs.
    Thanks, that looks promising. I will try it when I get back from work.

  4. #4
    Join Date
    Oct 2006
    Location
    Tucson, AZ
    Beans
    1,420
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: Command to replace tab characters in a file?

    Quote Originally Posted by jocko View Post
    I'm trying to make patches to make the old nvidia soundstorm drivers work on current kernels (see my howto here).
    One of the problems right now is that one of the files that needs to be patched contains leading tab characters in stead of spaces on some lines, which makes the patch command fail.

    Does anyone know any way to do of any of these:
    1. A way to get the patch command to work with lines containing tab characters?
    2. A way to replace tab characters with spaces on specific lines of a file?
    3. A way to replace ALL tab characters (or all leading tab characters) in a file with spaces?
    4. Any other way to replace the text I need to change (some sed command I can include in a script)?

    Specifically, I need a command to change this (but with tabs instead of "<tab>"):
    Code:
    <tab><tab><tab><tab><tab><tab><tab><tab><tab>:"=m"(currenttask->thread.i387.fxsave) );
    Into this (tabs or spaces doesn't really matter, as long as the actual code in red gets changed):
    Code:
                                    :"=m"(currenttask->thread.xstate->fxsave) );
    Have you tried running "patch" with the "-l" option? This tells patch to use a very loose matching algorithm as far as whitespace characters (tabs, spaces) are concerned.

    Lloyd B.
    Don't tell me to get a life.
    I had one once.
    It sucked.

  5. #5
    Join Date
    Nov 2005
    Location
    Uppsala, Sweden
    Beans
    2,180
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Command to replace tab characters in a file?

    Quote Originally Posted by lloyd_b View Post
    Have you tried running "patch" with the "-l" option? This tells patch to use a very loose matching algorithm as far as whitespace characters (tabs, spaces) are concerned.

    Lloyd B.
    That seems to have done it! So simple. Looks like I didn't read the patch manpage properly.

    Thank you!

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
  •