Search and replace text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replace text
# 1  
Old 10-06-2010
Search and replace text

Hello,

I am trying to search and replace but I don't know how to do it. My simple knowlegde in search and replace using sed is not helping me at all.

File:
Code:
its a cause value #22: dfg ggg
Cause value #1: aasfa fasdf
asfa value #22: affg gggg

Basically i want to replace the colon(Smilie with hash (#) while retaining the rest of the words on that line.

this is the format:
Code:
value #[can be any number]:

The logic would be like:
1. Find the line containing the word "value #22:" (22 can be any number)
2. Replace the colon with hash, "value #22#"

I need to change the : to # because my script that will further process this is not working on line with colon. Smilie

Appreciate your help.
# 2  
Old 10-06-2010
Code:
$ ruby -pne '$_.gsub!(/value #(\d+):/,"value #\\1#")' file
its a cause value #22# dfg ggg
Cause value #1# aasfa fasdf
asfa value #22# affg gggg

# 3  
Old 10-06-2010
hi,

try this:
Code:
sed -e '/value #[0-9]*:/{ s/value #\([0-9]*\):/value #\1#/ }' inputfile

# 4  
Old 10-06-2010
Quote:
Originally Posted by dragon.1431
hi,

try this:
Code:
sed -e '/value #[0-9]*:/{ s/value #\([0-9]*\):/value #\1#/ }' inputfile

it says "command garbled"
# 5  
Old 10-06-2010
Code:
perl -i -pe 's/(value #\d+):/$1#/g' input_file

# 6  
Old 10-06-2010
Or maybe with:
Code:
sed 's/\(#[0-9]*\):/\1#/' file

# 7  
Old 10-06-2010
dragon.1431's sed command is working in my linux box. Anyway pls try the below code..

Code:
sed 's/value #\([0-9][0-9]*\):/value #\1#/g' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search text and replace line next to it

I have a file which requires modification via a shell script. Need to do the following: 0. take input from user for new text. 1. search for a keyword in the file. 2. replace the line next to this to this keyword with user supplied input. for e.g., my file has the following text: (some... (7 Replies)
Discussion started by: chingupt
7 Replies

2. Shell Programming and Scripting

Search a string in a text and replace

i am having text file below System will display value URGENT and proceed System will display value URGENT and proceed System will display value URGENT and proceed .................................................................. (1 Reply)
Discussion started by: suryanarayana
1 Replies

3. Shell Programming and Scripting

Keyword search/replace for two text files?

What is the best way (bash/awk/sed?) to read in two text files and do a keyword search/replace? file1.txt: San Francisco Los Angeles Seattle Dallas file2.txt: I love Los Angeles. Coming to Dallas was the right choice. San Francisco is fun. Go to Seattle in the summer. ... (3 Replies)
Discussion started by: pxalpine
3 Replies

4. Emergency UNIX and Linux Support

Search and replace in text file

Hi, I have gigabytes of text files that I need to search for "&" and replace with "&amp". Is there a way to do this efficiently (like sed command)? Hope you could help. Thanks. (17 Replies)
Discussion started by: daytripper1021
17 Replies

5. UNIX for Advanced & Expert Users

Search and replace text

HI I have property files having content QA_server_name=10.232.54.7 QA_port_number=18000 DEV_server_name=10.235.60.73 DEV_port_number=18000 and a .jason file having content like this { "server":"localhost" "port":"17000" ------ } I will get the parameter... (1 Reply)
Discussion started by: mdtausifsh
1 Replies

6. Shell Programming and Scripting

Search and replace text in many files

Hello, I am very new to Linux and am trying to find a way for following problem. I have a number of files in a folder as Export000.dat, Export001.dat ..and so on. Each file has a string field 'Absolute velocity'. I want it to be replaced by 'Pixel shift' in all the files. I tried something like... (4 Replies)
Discussion started by: chirag.joshi
4 Replies

7. Shell Programming and Scripting

search and replace a text in a file

Hi all, I have a requirement where i have to search data between strings 'SELECT' and ';' and replace this text as "SELECT.....;" so that i can export this extracted string into a excel cell. Please suggest on this. (5 Replies)
Discussion started by: goutam_igate
5 Replies

8. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies

9. Shell Programming and Scripting

How to search and replace text in same file

script is as below v_process_run=5 typeset -i p_cnt=0 pdata=/home/proc_data.log while do # execute script in background dummy_test.sh "a1" "a2" & p_cnt=$p_cnt+1 echo "data : $p_cnt : Y" >> $pdata done file created with following data in... (1 Reply)
Discussion started by: Vrgurav
1 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question