LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 05-13-2008, 10:17 AM   #1
xiawinter
LQ Newbie
 
Registered: Aug 2007
Posts: 23
Thanked: 0
how to delete nth character in a text file?


[Log in to get rid of this advertisement]
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
xiawinter is offline  
Tag This Post , ,
Reply With Quote
Old 05-13-2008, 10:38 AM   #2
hro
LQ Newbie
 
Registered: Jan 2008
Distribution: OpenSuse 10.3, SLED 10 SP2, Ubuntu 8.04 and 9.04
Posts: 23
Thanked: 0
You can try something like this
vi -es file_to_be_edited < file_with_vi_commands
hro is offline     Reply With Quote
Old 05-13-2008, 11:05 AM   #3
druuna
Senior Member
 
Registered: Sep 2003
Location: netherlands
Distribution: lfs
Posts: 3,269
Thanked: 78
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.
druuna is offline     Reply With Quote
Old 05-13-2008, 11:50 AM   #4
colucix
Guru
 
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,130
Thanked: 464
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.
colucix is online now     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
View Chinese character in text file using vi ufmale Red Hat 1 02-17-2008 02:05 PM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 08:08 AM
Can VI Do This?? Delete before a character in text - Maybe advanced. tbeehler Linux - Software 9 05-07-2007 06:02 PM
Insert after every nth character using sed DoDo1975 Linux - Software 2 01-17-2007 07:59 PM
find a Nth line in text file mahabooba Programming 2 07-25-2006 12:56 AM


All times are GMT -5. The time now is 05:14 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration