Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

sed

Posted on 2000-05-11
5
1,002 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

789 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