find and replace pattren in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find and replace pattren in file
# 1  
Old 08-09-2007
find and replace pattren in file

Hi,

I have the input file having data as follow:
file1.txt
001 aaa_1:abcd
002 bbb_2:abcd

I want output as,
001xabcd
002xabcd

Here iam trying to replace "{1 space}{alphanumeric string with underscore}{:}" with characrter "x".
I tried to achieve this using sed;but Iam not getting this correctly.

Thanks in advance. Smilie
# 2  
Old 08-09-2007
check this

Code:
sed 's/^\(.*\) \(.*:\)\(.*\)$/\1x\3/g' testfile

# 3  
Old 08-09-2007
Code:
sed 's/ .*:/x/' file1.txt

# 4  
Old 08-09-2007
perl -i.bak -pe 's/THIS/THAT/g' file ...
# 5  
Old 08-09-2007
It's working!!!

Thanks. Smilie
# 6  
Old 08-09-2007
Agreeing with your request, the {alphanumeric string with underscore} corresponds to "[0-9a-zA-Z_][0-9a-zA-Z_]*". So:

Code:
sed "s/ [0-9a-zA-Z_][0-9a-zA-Z_]*:/x/g" file1.txt

But I think this is better:

Code:
sed "s/ .*:/x/g" file1.txt

Bye Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to find and replace in a file

Hi All, I am having below sample data in a file. I need to find all the line form this file with word ABC and i need to replace the characters at position 120 which is "CO:BOGFDUI"(30chars) in the lines with blank space. I have tried using grep to find the word with ABC (grep ABC filename),... (3 Replies)
Discussion started by: abhi_123
3 Replies

2. Shell Programming and Scripting

Replace a string on specific lines which match a pattren

Hi Experts, I have a file which contains a pattern multiple times i.e. matchthispattren. If a line is matched with this pattern. I want a number in 1234567890 to 123456789 in that line. (Basically remove the last digit from that number. Please help. Thanks, Varun (1 Reply)
Discussion started by: varun22486
1 Replies

3. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

4. Shell Programming and Scripting

Find a variable in a file and replace its value

HI , I can't find a solution to the following: In a simple menu script I want to capture the input from the user with "read" and use it as a value in a variable Rempages="some_value" which is in a different script file. So I have to perform a search and replace for this variable and only... (4 Replies)
Discussion started by: svetoslav_sj
4 Replies

5. Shell Programming and Scripting

Find and Replace in File

Legends, I have a file /tmp/list.txt I want to find "/bin/" and replace it with "/log/" I tried the follwoing but no luck Sandy: /tmp> perl -pi -e 's/\/bin\/\/log\/' /tmp/list.txt >> /tmp/try Substitution pattern not terminated at -e line 1. AND, Sandy: /tmp> perl -pi -e... (2 Replies)
Discussion started by: sdosanjh
2 Replies

6. UNIX for Dummies Questions & Answers

Find and Replace then rename file

Hi, This is probably quite simple for an expert, but I keep getting confused about the best approach, grep, awk, sed. What I have is a range of files numbered 1 to 100. They go file1.txt file2.txt and so on In each file I need to find and replace a couple of items and rename add a... (5 Replies)
Discussion started by: chickenhouse
5 Replies

7. Shell Programming and Scripting

How to find a certain string in a file and replace it with a value from another file using sed/awk?

Hi Everyone, I am new to this forum and new to sed/awk programming too !! I need to find particular string in file1(text file) and replace it with a value from another text file(file2) the file2 has only one line and the value to be replaced with is in the second column. file 1: (assert (=... (21 Replies)
Discussion started by: paramad
21 Replies

8. Shell Programming and Scripting

Find and replace in a gz file

Is there a way to do a find and replace in a .gz file in a single script ? I can always unzip, find and replace and then zip it again but would hate to do this everytime. Thanks ! Vivek (1 Reply)
Discussion started by: vashah
1 Replies

9. Shell Programming and Scripting

Find and replace in a file

Hi everyone, I am new to the world of shell script programming. I have a file named Fnd1.txt which has the contents as below. I need to replace the \t with the tab space. Can any one help me to write a perl scipt for this. USA45V1\tG\t341029 USAV1T1\tG\t450545 USAREJ1\tG\t572645... (5 Replies)
Discussion started by: vinay123
5 Replies

10. UNIX for Dummies Questions & Answers

Find replace within a file?

I build several files by using the cut command to grab select fields(columns) from a really bid csv file. Each file is one column of data. I then put them together using paste command. Here is the code built in tcsh: cut -d , -f 1 some.csv > 1.csv cut -d , -f 10 some.csv > 10.csv paste 1.csv... (2 Replies)
Discussion started by: yankee428
2 Replies
Login or Register to Ask a Question