multiple text edits inone pass


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers multiple text edits inone pass
# 1  
Old 04-20-2012
multiple text edits inone pass

File_1 looks like:

Code:
bunch of text
Untitled Placemark
bunch of text
bunch of text
Untitled Placemark
bunch of text
bunch of text
Untitled Placemark
bunch of text

File_2 looks like:

Code:
Title_001
Title_002
Title_003

First:
I need to replace the 1st occurence of "Untitled Placemark" in File_1 with the contents of the 1st record in File_2 "Title_001".
Then:
I need to replace the 2nd occurence of "Untitled Placemark" in File_1 with the contents of the 2nd record in File_2 "Title_002".
Then:
I need to replace the 3rd occurence of "Untitled Placemark" in File_1 with the contents of the 3rd record in File_2 "Title_003".

Thanks,
Kenny.

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Scrutinizer; 04-20-2012 at 05:13 PM..
# 2  
Old 04-20-2012
Here's a stab at it. I'm sure some of the awk experts will come up with a slick way to do it in one line, but this works, is easy to follow and sets a framework for possible future line by line processing requirements. It assumes the titles will be taken in order as you describe. You could load the titles in an array too. Not sure about the performance trade-off between calling sed multiple times vs using an array.
Code:
$ cat x
#!/bin/ksh

## Define variables.
typeset -i ctr=1                              # Integer counter
typeset -r TEXT_TO_MATCH="Untitled Placemark" # Constants
typeset -r TITLE_FILE="file_2"
typeset -r SOURCE_FILE="file_1"

##
## Process SOURCE_FILE.
##
while read line
do
  if [[ "$line" != "$TEXT_TO_MATCH" ]]; then
    print $line
  else
    # Print the matching line from the TITLE_FILE instead.
    sed "${ctr},${ctr}!d" $TITLE_FILE
    (( ctr=ctr+1 ))
  fi

done < $SOURCE_FILE

exit 0

$ x
bunch of text
Title_001
bunch of text
bunch of text
Title_002
bunch of text
bunch of text
Title_003
bunch of text
$

For the sake of argument, here's a version that gets the titles from an array:
Code:
$ cat x1
#!/bin/ksh

## Define variables.
set -A title_array                            # Array to hold titles
typeset -i ctr=0                              # Integer counter
typeset -r TEXT_TO_MATCH="Untitled Placemark" # Constants
typeset -r TITLE_FILE="file_2"
typeset -r SOURCE_FILE="file_1"

##
## Load the title array from $TITLE_FILE.
##
while read title_text
do
  title_array[${ctr}]=${title_text}
  (( ctr=ctr+1 ))
done < $TITLE_FILE

## Reset the counter.
ctr=0

##
## Process SOURCE_FILE.
##
while read line
do
  if [[ "$line" != "$TEXT_TO_MATCH" ]]; then
    print $line
  else
    # Print the matching line from the title_array instead.
    print ${title_array[$ctr]}
    (( ctr=ctr+1 ))
  fi

done < $SOURCE_FILE


exit 0
$ x1
bunch of text
Title_001
bunch of text
bunch of text
Title_002
bunch of text
bunch of text
Title_003
bunch of text
$

FYI - I ran both several times using the time command, and the sed version runs a smidge slower. Granted, the test files are tiny and your performance may vary!
# 3  
Old 04-20-2012
Code:
awk '/^Untitled Placemark$/ { $0="Title_" sprintf("%03d", ++N) } 1' file_in > file_out

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Pass Multiple Commands and Open Multiple Xterms via PSS

Hello, I'm attempting to open multiple xterms and run a command as an SAP user via sudo using PSSH. So far, I'm able to run PSSH to a file of servers with no check for keys, open every xterm in to the servers in the file list, and SUDO to the SAP(ADM) user, but it won't do anything else... (11 Replies)
Discussion started by: icemanj
11 Replies

2. Shell Programming and Scripting

Is is possible to pass multiple entries in PS3 select ?

PS3="Enter corresponding number and hit enter:" select DIR in `cat mylist` QUIT do if then echo "INVALID INPUT" else if ; then my commands ..... else break fi fi REPLY='' done The above will return something like below : Select from the list of... (4 Replies)
Discussion started by: ningy
4 Replies

3. UNIX for Dummies Questions & Answers

To pass multiple arguments from file in to an sql query

Hi all , I want to pass contents from a file say f1 as arguments to a sql query which has In statement using a script example select * from table_1 where login in ( `cat f1`) ; will this work or is there any other way to do it. (1 Reply)
Discussion started by: zozoo
1 Replies

4. Shell Programming and Scripting

How to automatically pass 'multiple' user inputs

Hi Everyone, 1) I really cannot figure out how to pass multiple user inputs in a script. really need your help re this. below is the script. ----------- #!/bin/sh # script name: ask.sh echo "Enter name: \c" read NAME echo "Your name is $NAME\n" echo "Enter age: \c" read AGE echo... (5 Replies)
Discussion started by: mcoblefias
5 Replies

5. Homework & Coursework Questions

K&R C code edits

1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: We have to do this using 'unix tools' and not use the script as if it were C. Meaning, he wants more uses of grep, sed, awk, cut, etc... than he does while, for, do's and done's.... (23 Replies)
Discussion started by: theexitwound
23 Replies

6. Shell Programming and Scripting

Shell script to pass multiple stdin to prorgam?

Running on AIX 5.3L. I have a program "foo" written in Fortran that requires 3 levels of inputs from stdin (command prompt). > foo Enter Input 1: a Enter Input 2: b Enter Input 3: c running foo success! > How do I get a shell script to run this automatically? > echo "a" | foo... (2 Replies)
Discussion started by: discoganya
2 Replies

7. Shell Programming and Scripting

How do we pass multiple arguments into awk

How do we pass multiple arguments into awk : name=john age=12 now i have to pass both age and name into awk.. how to do it? like : awk -v var=... (4 Replies)
Discussion started by: abhinav192
4 Replies

8. Shell Programming and Scripting

Multiple edits to a bunch of html files

I'm trying to upgrade a whole bunch of pages on my site to a new design. I thought one way of doing it would be to enclose the content in special comment tags and then use some form of script to wrap the new html around it. Like this: <!-- content start --> <h1>Blah blah blah</h1> yada yada... (9 Replies)
Discussion started by: dheian
9 Replies

9. Shell Programming and Scripting

Need help with scripting mass file edits..

Hello, I am wanting to know a way to shell (ksh)script-edit a file by having a script that searches for a specific string, and then input lines of text in the file after that specific string. Please help, as I will be up all night if I can't figure this out. (16 Replies)
Discussion started by: LinuxRacr
16 Replies

10. Shell Programming and Scripting

Pass multiple variables to SQL script

I am trying to close of multiple users in an Oracle database. Each users has records in multiple tables what I need to do is use a script that call each SQL seperately passing either CLI arguments or gathered arguments from the users during run time. ## Accept variable(s) from the command line... (1 Reply)
Discussion started by: jagannatha
1 Replies
Login or Register to Ask a Question