Change nth Char in a file..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change nth Char in a file..
# 1  
Old 06-20-2007
Change nth Char in a file..

I wanted to Replace if 20th Char is space then Replace by X ..



12345678901234567890

AAAA HEXW PROGRM01 (Ended by 3 Spaces ) Followed by junk(can be spaces als)

BBBB HEXW PROGRM01 A0121225001 (Ended by 3 Spaces)Followed by junk

I have Tired some thing of this sort ...

cat temp |sed 's/\(.\{19\}\)\([ ]\)\(.*$\)/\1X\3/'

was searching for any 19 Chars follwed by space followed by any chars till end of file...

but the out put is

EN33 HEXW ENIPOS01 X
EN33 HEXW ENIPOS01 A0121225001X

where as i wanted

only

EN33 HEXW ENIPOS01 X
EN33 HEXW ENIPOS01 A0121225001
# 2  
Old 06-20-2007
If I try for 20 th Char as "A" it works...
But if i put Space instead of "A" It behaves Differently

cat temp |sed 's/\(.\{19\}\)\(A\)\(.*$\)/\1X\3/' --- Works..

out put
AAAA HEXW PROGRM01
BBBB HEXW PROGRM01 X0121225001

cat temp |sed 's/\(.\{19\}\)\([ ]\)\(.*$\)/\1X\3/' --- Works..

out put
AAAA HEXW PROGRM01 X
BBBB HEXW PROGRM01 A0121225001X
# 3  
Old 06-20-2007
Quote:
Originally Posted by pbsrinivas
If I try for 20 th Char as "A" it works...
But if i put Space instead of "A" It behaves Differently

cat temp |sed 's/\(.\{19\}\)\(A\)\(.*$\)/\1X\3/' --- Works..

out put
AAAA HEXW PROGRM01
BBBB HEXW PROGRM01 X0121225001

cat temp |sed 's/\(.\{19\}\)\([ ]\)\(.*$\)/\1X\3/' --- Works..

out put
AAAA HEXW PROGRM01 X
BBBB HEXW PROGRM01 A0121225001X
It is working fine for me.
Code:
$ cat file
EN33 HEXW ENIPOS01  
EN33 HEXW ENIPOS01 A0121225001
$ sed 's/\(.\{19\}\)\([ ]\)\(.*$\)/\1X\3/' file
EN33 HEXW ENIPOS01 X
EN33 HEXW ENIPOS01 A0121225001
$ sed 's/\(.\{19\}\) /\1X/' file
EN33 HEXW ENIPOS01 X
EN33 HEXW ENIPOS01 A0121225001
$ sed 's/.\{19\} /&X/' file
EN33 HEXW ENIPOS01  Xanbu
EN33 HEXW ENIPOS01 A0121225001

# 4  
Old 06-20-2007
Code:
sed 's/^\(.\{19\}\) /\1X/' temp

# 5  
Old 06-20-2007
Quote:
Originally Posted by aigles
Code:
sed 's/^\(.\{19\}\) /\1X/' temp

Thanks aigles

It Works...

I should have searched for starting with... Smilie

Anbu...

If We have Space Appended to both the Lines it doesnt work...
I have tried with ^\(.\{19\}\ and its fine now...

Thanks again..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change ? char from files and directory

Hello, I must change files and dirs name which contains che "?" char, I try this: rename 's/?/-/' *.* nothing, what's the problem? thanks (14 Replies)
Discussion started by: ionral
14 Replies

2. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

3. Shell Programming and Scripting

Change nth depending on matiching a pattern in Y line

Hi, I have below file, each line that starts with /* marks the beginning of the a new job. /* ----------------- cmdsMlyMoveTPMPLANTJ ----------------- UNIX_JOB CMMM002J CMDNAME /home2/proddata/bin/moveTPMPLANT.sh AGENT CMDSHP USER proddata AFTER CMMU001J /* "DDRG monthly... (13 Replies)
Discussion started by: varun22486
13 Replies

4. Shell Programming and Scripting

Count char, sum and change

Hello, I have some problem in counting char of word, sum and change. I'm not sure shell script can do this. Input data: Sam1 BB BB AA AA BB BB BB Sam2 BB BB AA AA AB AB AB Sam3 BB BB BB AA BB BB BB Sam4 AB AB AB AB AB AB AA Sam5 BB BB AA AA BB BB -- If I count in column 2, B is 9... (3 Replies)
Discussion started by: awil
3 Replies

5. UNIX for Dummies Questions & Answers

change field separator only from nth field until NF

Hi ! input: 111|222|333|aaa|bbb|ccc 999|888|777|nnn|kkk 444|666|555|eee|ttt|ooo|ppp With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records). In order to get: 111|222|333|aaa; bbb; ccc 999|888|777|nnn; kkk... (1 Reply)
Discussion started by: beca123456
1 Replies

6. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

7. Shell Programming and Scripting

Using AWK to find top Nth values in Nth column

I have an awk script to find the maximum value of the 2nd column of a 2 column datafile, but I need to find the top 5 maximum values of the 2nd column. Here is the script that works for the maximum value. awk 'BEGIN { subjectmax=$1 ; max=0} $2 >= max {subjectmax=$1 ; max=$2} END {print... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

8. UNIX for Dummies Questions & Answers

How to change the second to nth string in lower case

I have a file which contains something like this REIGN ANGEL i want to change REIGN to Reign and ANGEL to Angel. (6 Replies)
Discussion started by: reignangel2003
6 Replies

9. Shell Programming and Scripting

Append char to the end of string from Nth column

I'm sure this is easy to do but I can't find a one line command with awk or sed to append a char to the end of the string from Nth column. Any sugestion please? Thanks (2 Replies)
Discussion started by: cabrao
2 Replies

10. Shell Programming and Scripting

Change a Char Multiple line records

Hi All i have a file that is to big for vi and is a multiple line record 3999||20090127163547796|196.46.162.250|1028|196.207.40.112|2152|00:0C:31:BB:25:5 4|00:00:0C:07:AC:06|655016000575511|05||3C65|0D029C1D|||00644B5A|||||||||||inter... (5 Replies)
Discussion started by: gseptember
5 Replies
Login or Register to Ask a Question