match a character in a line and replace


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers match a character in a line and replace
# 1  
Old 08-08-2008
match a character in a line and replace

Hi,

I have a file with large number of records. Sample below:

123456789QWERT2U 2 erter
987123678ZXCVB6Y 5 7689
934567123GHJKUI4O 7
-
--
--

I want the 16th character in each record to be replaced with the below as follows;so 2 will become K, 6 will become O and 4 will become M.
0 }
1 J
2 K
3 L
4 M
5 N
6 O
7 P
8 Q
9 R

So, the records should look like:


123456789QWERTKU
987123678ZXCVBOY
934567123GHJKUIMO

---

How can i do that sort of patterns matching and replacement.

Thanks.
# 2  
Old 08-08-2008
What have you tried so far?
# 3  
Old 08-10-2008
One of your examples is different of the others.

934567123GHJKUI4O 7 -> "4" is the 16th char.
987123678ZXCVB6Y 5 7689 -> "6" is the 15th char.

I made it replace the 15th char, but if you want to replace the 16th just change substr's param from 14 to 15.

Code:
redoubtable@Tsunami ~ $ echo -e "987123678ZXCVB6Y 5 7689\n934567123GHJKUI4O 7\n123456789QWERT2U 2 erter"|perl -ne '@a = ("}","J","K","L","M","N","O","P","Q","R"); substr($_, 14, 1, $a[substr($_, 14, 1)]); print'
987123678ZXCVBOY 5 7689
934567123GHJKU}4O 7
123456789QWERTKU 2 erter
redoubtable@Tsunami ~ $

# 4  
Old 08-10-2008
Assuming it is the 16th character you want to replace, here is one way of doing it using awk i.e. awk -f awkscriptfile file

Code:
BEGIN { split("} J K L M N O P Q R", lookup) }
{ print substr($0,1,15) lookup[substr($0,16,1) + 1] substr($0,17) }

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies

2. Shell Programming and Scripting

Remove/replace the very first character/symbol match

cat file.txt file 1123.x July 23:222 /cd/hh2/k39/ss2/f7d8d9d8e6r5t4s/dd2/e/s7a/s7a2afa5017d8b975-1.7-1395610245-b22e19bbc477b134 i wish to only extract out the 1.7 (anything within the first - -) i try to look for the sed command under match the first occurence of pattern but out of luck, my... (6 Replies)
Discussion started by: ctphua
6 Replies

3. Shell Programming and Scripting

sed Character match and replace

Hello All I am struck in the issue which I want to share with all of you. What I am trying to do is For every line in a file I have to replace a particular character from the given character in a file For Example Suppose the data is 1111x2222 1111x2222 2222y3333 1111x2222 I... (4 Replies)
Discussion started by: adisky123
4 Replies

4. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

5. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

6. Shell Programming and Scripting

Replace last character of every second line

Hi all. I wonder if this possible.... any help advice is very much appreciated.. I n a shell script I create a latex file that looks like this \documentclass{article} \usepackage{graphics} \begin{document} \begin{figure} \begin{center} \begin{tabular}{cc} ... (10 Replies)
Discussion started by: malandisa
10 Replies

7. Shell Programming and Scripting

how to replace character in the line

I have unix text file which has the following data aadjdfad;fa;fjjd;lakd;lkaslkd;k;k;lk;k;lk;l;lk;lkj;lj;lkj;k;lkj;lj;lkj;lkj;lkj;j sdkadk;adlf;lajf;akdjf;lkdjf;lkadjf;lkajsd;lfkj;lkj;lkj;lk;lk;lk;lk;k;lkj;k;lkm... (2 Replies)
Discussion started by: Raju Datla
2 Replies

8. Shell Programming and Scripting

to replace a new line character

sample I/p: S12J LLL H77K PPP J25O LOP I73S lOP K99O PLO Required O/p: S12J LLL H77K PPP J25O LOP I73S lOP K99O PLO how to replace a new line character with space using sed command only Cheers, Chan (2 Replies)
Discussion started by: chan
2 Replies

9. Shell Programming and Scripting

Replace a character in last line

Hello Sed Experts, I have got a file which contain entries as below pmNoOfSwDownHsCong, pmUlUpswitchAttemptHigh, pmUlUpswitchAttemptLow, pmUlUpswitchSuccessHigh, pmUlUpswitchSuccessLow, pmUpswitchFachHsAttempt, ... (6 Replies)
Discussion started by: Mohammed
6 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question