LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > 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

Reply
 
Search this Thread
Old 05-13-2008, 10:17 AM   #1
xiawinter
LQ Newbie
 
Registered: Aug 2007
Posts: 27

Rep: Reputation: 15
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
 
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

Rep: Reputation: 15
You can try something like this
vi -es file_to_be_edited < file_with_vi_commands
 
Old 05-13-2008, 11:05 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 05-13-2008, 11:50 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
  


Reply

Tags
delete, sed, vi


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 10:42 PM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration