Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sed

Posted on 2000-05-11
5
Medium Priority
?
1,029 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 120 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Screencast - Getting to Know the Pipeline
Suggested Courses

971 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