Solved

sed

Posted on 2000-05-11
5
992 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now