Solved

sed

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Whole sheet autoscrub still needed 19 48
java ^ examples 8 62
sumHeights2  challenge 7 87
Message not shown 5 30
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

911 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

19 Experts available now in Live!

Get 1:1 Help Now