LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   how to delete nth character in a text file? (https://www.linuxquestions.org/questions/linux-software-2/how-to-delete-nth-character-in-a-text-file-641888/)

xiawinter 05-13-2008 09:17 AM

how to delete nth character in a text file?
 
is there any method to delete nth character in a text file? I tried searching sed helps but without any result on this. vi might help, but it works in an interactive method, which prevents from processing it automatically.

Any ideas on this will be appreciated.

--
Samuel Wu

hro 05-13-2008 09:38 AM

You can try something like this
vi -es file_to_be_edited < file_with_vi_commands

druuna 05-13-2008 10:05 AM

Hi,

Using sed:

sed 's/^\(.\{7\}\).\(.*\)/\1\2/' infile

Change the 7 into the character position that needs to be deleted.

Example:
Code:

$ cat infile
123456 A 7890
123456 B 7890
123456 C 7890
123456 D 7890
123456 E 7890

$ sed 's/^\(.\{7\}\).\(.*\)/\1\2/' infile
123456  7890
123456  7890
123456  7890
123456  7890
123456  7890

Hope this helps.

colucix 05-13-2008 10:50 AM

This awk code will delete character at position specified by NUM
Code:

BEGIN { NUM = 13 }
{
  if ( NUM > length($0) ) {
    NUM = NUM - length($0)
    print
  }
  else {
    printf "%s",substr($0,1,NUM-1)
    print substr($0,NUM+1)
    NUM = 1e+15
  }
}

It considers the character position as absolute inside the whole text (not the position on each line) including blank spaces and tabs in the count and excluding newline characters \n.


All times are GMT -5. The time now is 01:27 AM.