character substitution

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting character substitution
# 1  
Old 01-21-2008
character substitution

Hi ,
I have a problem , I need to devlope a script where in the user inputs file name , line number , and character position , and a substitution variable , the character at that character position should be substituted by the substitution value
for ex
say i have a file
which has a line 1 as "vivek is a good boy"
If i input line number 1
character podition 7-8
substitution value : ok
the output should be "vivek ok a good boy "

need help
# 2  
Old 01-21-2008
$ cat abc.txt
vivek is a good boy
shell programming forum

Replace character position 7-8 with "ok" in 1st line of abc.txt, here is the way using sed:

$ sed -r "1 ~ s/^(.{6})(.{2})/\1ok/" abc.txt > abc.txt.tmp; mv abc.txt.tmp abc.txt

$ cat abc.txt
vivek ok a good boy
shell programming forum

# 3  
Old 01-21-2008


Try follow one. There is five input, first is the file to be dealed with. Second is the line number of the file. Third is the begin position of string to be replaced. Forth is the length of the string to be replaced. Fifth is the value used to replace old one.

nawk -v l="$line" -v c="$char" -v len="$len" -v v="$val" '{
	print substr($0,1,c-1) v substr($0,c+len)
	print $0
}' $file > ${file}.bak
rm $file
mv ${file}.bak $file

# 4  
Old 01-21-2008
awk 'BEGIN{
   printf "Enter line number: "
   getline linenum < "-"
   printf "Character position eg 2-3: "
   getline chpos < "-"
   printf "Substituion  value eg :" 
   getline subval < "-"
   if ( chpos ~ /-/  ) {
    n=split( chpos, charpos,"-")
    startpos = charpos[1]
   }else {
   sub( substr($0,startpos,endpos) , subval)   
1' "file"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. UNIX for Beginners Questions & Answers

(g)awk conditional substitution issues when attempting to delete character

A portion of my input is as follows: 1087 IKON01,49 A WA- -1 . -1 . 0 W WA- -1 . -1 . 0 . -1 . -1 -1 -1 -1 -1 -1 W 1088 IKON01,49 A J.@QU80MW. 2... (2 Replies)
Discussion started by: jvoot
2 Replies

3. Shell Programming and Scripting

ksh - Get last character from string - Bad Substitution error

I want to get the last character from my machine name using the following code, the default shell is bash, the script runs in ksh. I get 'bad' substitution error on running the script, but works fine if run using dot and space. Why? $ echo $0 bash $ cat -n 1 ... (8 Replies)
Discussion started by: ysrini
8 Replies

4. Shell Programming and Scripting

KSH: substitution character, AWK or SED?

Hi Gurus, I am working with a korn shell script. I should replace in a very great file the character ";" with a space. Example: 2750;~ 2734;~ 2778;~ 2751;~ 2751;~ 2752;~ what the fastest method is? Sed? Awk? Speed is dead main point, Seen the dimensions of the files Thanks (6 Replies)
Discussion started by: GERMANICO
6 Replies

5. UNIX for Advanced & Expert Users

if 4th and 5th character of sting -ge 45 then add 1 to 3rd character

I want to know how to, given a string like W87151WR71C, if the 4th and 5th character (in this case 15) are greater than 45, then to add 1 to the 3rd character (in this case 7) and assign the revised string the variable name MODSTRING. Thanks in advance. This is ultimately to grab info from... (6 Replies)
Discussion started by: glev2005
6 Replies

6. Shell Programming and Scripting

Deleting all characters from 350th character to 450th character from the log file

Hi All, I have a big log file i want to delete all characters (between 350th to 450th characters) starting at 350th character position to 450th character position. please advice or sample code. (6 Replies)
Discussion started by: rajeshorpu
6 Replies

7. Shell Programming and Scripting

Sed character substitution

Hi I have to replace in around 60 files a word an replcae it by another Suppose all the files have a word intelligent i want to replace it by idiot I am planning to use sed for executing this job sed 's/\intelligent/idiot/g' I plan to have a file (test.txt) which contains... (1 Reply)
Discussion started by: ultimatix
1 Replies

8. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

9. Shell Programming and Scripting

sed global substitution for ' character

Hi all, I have a text file containing sql commands. I want to use sed to replace the " character with the ' character. But when i run the command below it just replaces it with a space? Do i have the escape the ' character if i want to use it? cat sql.txt update customer set custid =... (1 Reply)
Discussion started by: borderblaster
1 Replies

10. Shell Programming and Scripting

Difference between "Command substitution" and "Process substitution"

Hi, What is the actual difference between these two? Why the following code works for process substitution and fails for command substitution? while IFS= read -r line; do echo $line; done < <(cat file)executes successfully and display the contents of the file But, while IFS='\n' read -r... (3 Replies)
Discussion started by: royalibrahim
3 Replies
Login or Register to Ask a Question