Solved

sed

Posted on 2000-05-11
5
998 Views
Last Modified: 2013-12-26
I have a text file that may have an improper character at a specific character position.  If this improper character is found, then it needs to be converted to a space.

Example: IF character 45 = 0 THEN
              character 45 should be " "

Can this be done with sed/awk?
0
Comment
Question by:afpcos
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:geotiger
ID: 2801858
The following code will do it:

sed -e 's/your_chr/ /g' your_file > newfile
0
 
LVL 2

Author Comment

by:afpcos
ID: 2801884
The problem is that the same character that is in character position 45 could be in several other character positions on the same line.  I only want to replace the one in character position 45.
0
 
LVL 2

Author Comment

by:afpcos
ID: 2801885
The problem is that the same character that is in character position 45 could be in several other character positions on the same line.  I only want to replace the one in character position 45.
0
 
LVL 12

Accepted Solution

by:
geotiger earned 30 total points
ID: 2801924
Did not read your question carefully in the last comment.

You may need to use awk instead of sed. Here is the solution: you can specify the location (pos), from character (fc), and to character (tc).


$ more tst_cc.awk

#!/bin/nawk -f
BEGIN{}
{ len = length($0)
  c1 = substr($0, 1, pos -1)
  c2 = substr($0, pos, 1)
  c3 = substr($0, pos+1, len - pos)
  if (c2==fc) { c2 = tc }
  print c1 c2 c3
}
END {}

$ more tst03.txt
  01/01/1999|01:20|0011|12|678...
  01/01/1999|03:20|0621||4...
  01/01/1999|05:41|0001|1|77...
$ ./tst_cc.awk pos=6 fc=0 tc=x tst03.txt
  01/x1/1999|01:20|0011|12|678...
  01/x1/1999|03:20|0621||4...
  01/x1/1999|05:41|0001|1|77...





0
 
LVL 2

Author Comment

by:afpcos
ID: 2804409
Thanks!!!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question