Change the i-th character of the j-th line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change the i-th character of the j-th line
# 1  
Old 09-23-2015
Change the i-th character of the j-th line

Hi,
I have a file like


Code:
#-----------------------------------
3                ! number of parameters of the polynome
0.00000e+00   0  ! fix=0) free=1 
1.03916e-03   1  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1 
3                ! number of parameters
2.16595e-05   0  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1

Is there a way to change the i-th character of the j-th line to a value that I specifiy?

In the case above I would like to change from 1 to 0 the character in the 15th column; but it can be anywhere in principle

Any help is appreciated,
Sarah
# 2  
Old 09-23-2015
Code:
$  sed "/.\{14\}1/s/./0/15" file
#-----------------------------------
3                ! number of parameters of the polynome
0.00000e+00   0  ! fix=0) free=1
1.03916e-03   0  ! fix=0) free=1
0.00000e+00   0  ! fix=0) free=1
3                ! number of parameters
2.16595e-05   0  ! fix=0) free=1
0.00000e+00   0  ! fix=0) free=1
0.00000e+00   0  ! fix=0) free=1

This User Gave Thanks to anbu23 For This Post:
# 3  
Old 09-23-2015
is there a way to specify the row number? I would like to change only one row+column
# 4  
Old 09-23-2015
Try an adaption of anbu's code:
Code:
sed "4s/^\(.\{14\}\)1/\10/" file

Please note that anbu's regex patterns should be anchored to target exactly the one location intended.

Last edited by RudiC; 09-23-2015 at 09:19 AM.. Reason: typo
# 5  
Old 09-23-2015
Let's say that I want to change the character in row 3, column 15 to X

Input
Code:
#-----------------------------------
3                ! number of parameters of the polynome
0.00000e+00   0  ! fix=0) free=1 
1.03916e-03   1  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1 
3                ! number of parameters
2.16595e-05   0  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1

output
Code:
#-----------------------------------
3                ! number of parameters of the polynome
0.00000e+00   0  ! fix=0) free=1 
1.03916e-03   X  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1 
3                ! number of parameters
2.16595e-05   0  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1

# 6  
Old 09-23-2015
Hi, in bash:
Code:
$ ROW=3 && COL=15 && sed -e "$((ROW+1))s/./X/${COL}" file
#-----------------------------------
3                ! number of parameters of the polynome
0.00000e+00   0  ! fix=0) free=1
1.03916e-03   X  ! fix=0) free=1
0.00000e+00   0  ! fix=0) free=1
3                ! number of parameters
2.16595e-05   0  ! fix=0) free=1
0.00000e+00   0  ! fix=0) free=1
0.00000e+00   0  ! fix=0) free=1

Regards
This User Gave Thanks to disedorgue For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

2. Shell Programming and Scripting

Sed: delete on each line before a character and after a character

Hi there, A total sed noob here. Is there a way using sed to delete everything before a character AND after another character on each line in a file? The deletion should also delete the indicating characters(here: an opening and a closing parenthesis). The original file would look like... (3 Replies)
Discussion started by: bnbsd
3 Replies

3. UNIX for Dummies Questions & Answers

Need help removing last character of every line if certain character

I need help removing the last character of every line if it is a certain character. For example I need to get rid of a % character if it is in the last position. Input: aaa% %bbb ccc d%dd% Output should be: aaa %bbb ccc d%dd I tried this but it gets rid of all of the % characters.... (5 Replies)
Discussion started by: raptor25
5 Replies

4. 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

5. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

6. Shell Programming and Scripting

Change last Character

Hi, Someone can help me please, i have text in file like this: (a,b,c,d,e), (f,g,h,i,j), (k,l,m,n,o), and i want to change the last character to like this: (a,b,c,d,e), (f,g,h,i,j), (k,l,m,n,o); last array character "," i want change to ";" anyone can help me please,, thanks (2 Replies)
Discussion started by: ryo
2 Replies

7. Shell Programming and Scripting

Reverse a change of character

Good morning, i have two chaines of characters to be reversed in shell script (red with blue). It was only one line. Can you help me to write a script in awk or sed? ... (2 Replies)
Discussion started by: khalidou13
2 Replies

8. Solaris

help me to change the character set

dears i am using solaris 10 i am facing a problem when i make setup for solaris i choose the country egypt and i select the language north america but i forget to do that the i found the date Jun written in arabic i want to change character set to written in english -rw-r--r-- 1 root ... (4 Replies)
Discussion started by: hosney00ux
4 Replies

9. Shell Programming and Scripting

Vi single character change

I know this is strictly a programming forum - but I consider vi a programming enabler and the question relates to regex you'd use with awk/sed anyway.... I have a file which is 50,000+ lines long and need to change many many instances of word_word_word to be word+word+word where... (6 Replies)
Discussion started by: Brett Taylor
6 Replies

10. UNIX for Dummies Questions & Answers

change character case

i need to change character case inside a script file. How i can do that? any fucntion? thanks (3 Replies)
Discussion started by: ajaya
3 Replies
Login or Register to Ask a Question