Search & Replacing text within a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search & Replacing text within a file
# 1  
Old 10-28-2011
Search & Replacing text within a file

Hi all!
I'm a newbie and I'm writing a script which will ask a user for data to search. I will then search for this data using grep and displaying this data back to the screen for the user to see. The user will then enter new data to replace the data searched.

Now how do I replace this data within the file? SED, AWK???
grep search results for "JOE S SMITH":
#'s - Indicate spaces
730000146104218006JOE#S#SMITH#########5230 BRUNSWICK LN MACUNGIE

Replace with "TIM KIM":
730000146104218006TIM#KIM#############5230 BRUNSWICK LN MACUNGIE
or
Replace with "JEROMANIA K KALAMAZOO"
730000146104218006JEROMANIA#K#KALAMAZO5230 BRUNSWICK LN MACUNGIE

***** Data for Name starts at position 19 and ends at position 38 of this file. Replacement data CAN NOT go past position 38 ******

Last edited by macastor; 10-28-2011 at 06:43 PM..
# 2  
Old 10-28-2011
Is this homework?
# 3  
Old 10-28-2011
What have you tried?

seems like a script with the following basics

Code:
read input1
grep input1 < myfile
read input2
sed s/input1/input2 <myfile >mynewfile

# 4  
Old 10-28-2011
Yes I am somewhat familiar with this command, but the data that I am replacing can't shift right or left. This file must stay as a fixed format to be read by other processes. So if i search for data in a file such as the name which starts at position 19 and ends at position 38 of this file. Replacement data CAN NOT go past position 38. example:

grep search results for "JOE S SMITH":
#'s - Indicate spaces
730000146104218006JOE#S#SMITH#########5230 BRUNSWICK LN MACUNGIE

Replace with "TIM KIM":
730000146104218006TIM#KIM#############5230 BRUNSWICK LN MACUNGIE
or
Replace with "JEROMANIA K KALAMAZOO"
730000146104218006JEROMANIA#K#KALAMAZO5230 BRUNSWICK LN MACUNGIE

Last edited by macastor; 10-28-2011 at 06:37 PM..
# 5  
Old 10-28-2011
Code:
search="JOE S SMITH"
replace1="TIM KIM"
replace2="JEROMANIA K KALAMAZOO"

awk -v s="$search" -v r="$replace1" 'function convert(str) {return (length(str)>20)?substr(str,1,20):sprintf("%-20s",str)}
        {if ($0~s) sub(convert(s),convert(r))}1' infile

730000146104218006TIM KIM             5230 BRUNSWICK LN MACUNGIE

awk -v s="$search" -v r="$replace2" 'function convert(str) {return (length(str)>20)?substr(str,1,20):sprintf("%-20s",str)}
        {if ($0~s) sub(convert(s),convert(r))}1' infile

730000146104218006JEROMANIA K KALAMAZO5230 BRUNSWICK LN MACUNGIE

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

2. Shell Programming and Scripting

Finding a text in files & replacing it with unique strings

Hallo Everyone. I have to admit I'm shell scripting illiterate . I need to find certain strings in several text files and replace each of the string by unique & corresponding text. I prepared a csv file with 3 columns: <filename>;<old_pattern>;<new_pattern> ... (5 Replies)
Discussion started by: gordom
5 Replies

3. Shell Programming and Scripting

search & select text

hi people; from my file:... ... ... 101221-18:45:17 192.168.1.1 1.0 PortNodeModel ========================================================= Object Attribute Value ========================================================= SectorPort=3 switchport 20 SectorPort=1 switchport 10 SectorPort=2... (12 Replies)
Discussion started by: gc_sw
12 Replies

4. Homework & Coursework Questions

[Scripting]Find & replace using user input then replacing text after

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: (o) Checkout an auto part: should prompt the user for the name of the auto part and borrower's name: Name:... (2 Replies)
Discussion started by: SlapnutsGT
2 Replies

5. Shell Programming and Scripting

In PHP replacing text between TAGS & URI information in Title Tag

Hi, what I am trying to do in PHP, is to replace the Title. I need some of the URL information inside aswell depending on the domain. The title is always different so I need to store it in a variable, put the url info like described below in front of it. Here is an example how it should... (0 Replies)
Discussion started by: lowmaster
0 Replies

6. UNIX for Dummies Questions & Answers

Search for & edit rows & columns in data file and pipe

Dear unix gurus, I have a data file with header information about a subject and also 3 columns of n rows of data on various items he owns. The data file looks something like this: adam peter blah blah blah blah blah blah car 01 30 200 02 31 400 03 57 121 .. .. .. .. .. .. n y... (8 Replies)
Discussion started by: tintin72
8 Replies

7. Shell Programming and Scripting

How do I search first&second string & copy all content between them to other file?

Hi All, How do I search first string & second string and copy all content between them from one file to another file? Please help me.. Thanks In Advance. Regards, Pankaj (12 Replies)
Discussion started by: pankajp
12 Replies

8. Shell Programming and Scripting

Advanced Search & Delete Text File

I have a file in which email messages are stored in. Every email is separated by by a ^Z character (Control-Z). I need to extract all emails after the 65,00th one to another file and delete them from the original file. Any suggests on accomplishing this? (2 Replies)
Discussion started by: maxcell
2 Replies

9. Shell Programming and Scripting

help with finding & replacing pattern in a file

Hi everyone. Could u be so kind and help me with on "simple" shell script? 1. i need to search a file line by line for a pattern. example of a lines in that file 2947 domain = feD,id = 00 0A 02 48 17 1E 1D 39 DE 00 0E 00,Name Values:snNo = f10 Add AttFlag = 0 2. i need to find... (0 Replies)
Discussion started by: dusoo
0 Replies

10. Solaris

finding & replacing blank rows/spaces in a file

Can anyone help me find and replace blank rows in a file with a numeric value (ie blankrow=someTxtOrNumValue), the file is over 500,000 rows long so it would need to be the quickest way as I'll need to do this for multiple files...I would be greatfull for any suggestions....thanks sample file:... (2 Replies)
Discussion started by: Gerry405
2 Replies
Login or Register to Ask a Question