Help to replace character strings


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help to replace character strings
# 1  
Old 09-22-2004
Question Help to replace character strings

Hello Can Any1 help me.

I want to replace a specific character string inside a file at a specific location with a particular character with the help of a command or a shell script.
The tr command replaces a specific character with another for all the occurences of that character in the file.
I don't know wat to do.
# 2  
Old 09-22-2004
This replaces "test" with "old" in line 13
Code:
awk -v NEWSTRING="test" -v OLDSTRING="old" -v LINENUM=13 '{ 
{if (NR == LINENUM) sub(OLDSTRING,NEWSTRING)} print $0 }'  filename

# 3  
Old 09-22-2004
Thanx for help but one more doubt

Thanx for ur solution.
The code u provided only replaces the first occurence of the string on that line not the other occurences. how is that possible.
Please reply soon.
# 4  
Old 09-24-2004
This will replace text matching "edcba" at position 45 of each line (if there are multiple records) with text "abcde" and output to a second file called "newfile".
------------
awk ' BEGIN{
while (getline i < "filename"){

string=substr(i,45,5)

if (string == "edcba"){

print substr(i,1,44) "abcde" substr(i,50,length(i)-49) > "newfile"

}
}
} ' filename
----------
I don't know if that works for your application. If not, do research on "sed".
# 5  
Old 09-27-2004
Quote:
Originally posted by jim mcnamara
This replaces "test" with "old" in line 13
Code:
awk -v NEWSTRING="test" -v OLDSTRING="old" -v LINENUM=13 '{ 
{if (NR == LINENUM) sub(OLDSTRING,NEWSTRING)} print $0 }'  filename

modify sub to gsub to replace all occurences.
# 6  
Old 12-18-2008
Line number iwll not be available..

Hi I want to replace the string Additional Text to rawText in my file...But not sure which linenum it will come everytime....
But it is one tim eoccurence in the file..I such cases how to replace the string without line number..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove certain character strings with awk?

Hi all, I need to remove DBPATH= and /db from the string below using awk (or sed, as it also exists on the machine). Input: DBPATH=/some/path/database/db Desired output: /some/path/database Thank you! (8 Replies)
Discussion started by: ejianu
8 Replies

2. UNIX for Advanced & Expert Users

Replace certain character at specific place with related character

hello i have file with 100k records and each one has certain value that starts at 28th column and certain value that starts at 88th column e.g. 1st file <25>1234567 ..... <88> 8573785485 i have aditional file with values which are related to value that starts at 88th column of the... (1 Reply)
Discussion started by: dell1520
1 Replies

3. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

4. Shell Programming and Scripting

Selecting strings with - as first character in bash

I have a variable containing a list of strings, and want to create a string with the first character being -. Example: var="-name fred paul -surname winnett dimech" I want a string, namely errStr="-name -surname" (2 Replies)
Discussion started by: kristinu
2 Replies

5. Shell Programming and Scripting

Replace multiple occurances of same character with a single character.

Hi all, Greetings, I have the following scenario, The contents of main file are like : Unix|||||forum|||||||||||||||is||||||the||best so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it And i need the output in the following form: Unix=forum=is=the=best so=be=on=it ... (3 Replies)
Discussion started by: dipanchandra
3 Replies

6. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

7. UNIX for Dummies Questions & Answers

character-by-character comparison of strings

This might be a dummy question, but is there a command in UNIX that compare two strings character-by-character and display the difference? ---------- Post updated at 11:25 AM ---------- Previous update was at 10:32 AM ---------- Or probably what I'm looking is how to break a string into... (3 Replies)
Discussion started by: Orbix
3 Replies

8. Shell Programming and Scripting

replace two character strings by two variables with sed command

Hello, I want to writte a script that replace two character strings by two variables with the command sed butmy solution doesn't work. I'm written this: sed "s/TTFactivevent/$TTFav/g && s/switchSLL/$SLL/g" templatefile. I want to replace TTFactivevent by the variable $TTFav, that is a... (4 Replies)
Discussion started by: POPO10
4 Replies

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

10. UNIX for Dummies Questions & Answers

Getting strings before and after a character

OK This one has me stumped. I have the following line, program name - the program description that can also contain a hyphen - character. I'm need to separate the "program name" from the program description. I've tried using an array function with the - as delimiter, but I ran into a... (2 Replies)
Discussion started by: ricksj
2 Replies
Login or Register to Ask a Question