Insert text in file using two variables. (ksh)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert text in file using two variables. (ksh)
# 1  
Old 03-29-2012
Insert text in file using two variables. (ksh)

I want to insert text into a file using ksh script. The text is going above a closing </body> tag. In the text are two variables.

I thought I found how to do it, inexplicably still, using awk, but ran into some problems and time has gone by too fast. The problems using awk came when I added my second variable, however the problem might not be the second variable.Smilie
# 2  
Old 03-29-2012
can you post some sample text and expected output
# 3  
Old 03-29-2012
cat $reference | awk -v ref=$reference -v desc=$description '/<\/body.*>/ { print "<A HREF="ref">"desc"</A>" } { print }' $reference > tmp && mv tmp $reference

$reference = file location
$description = multi word string with spaces
-second line, I don't fully understand, maybe not enough thought, but I thought it was worth mentioning.

awk: can't open file and
source line number 1

-something odd about the space after and.

Last edited by robin_simple; 03-29-2012 at 11:49 PM..
# 4  
Old 03-30-2012
Quote:
Originally Posted by robin_simple
$description = multi word string with spaces
You should start by quoting the use of $description. Also, it cannot hurt to quote $reference to prevent issues should a file name contain spaces. You also don't need to cat the file; awk can read the file itself from disk.

Code:
awk -v ref="$reference" -v desc="$description" '/<\/body.*>/ { print "<A HREF="ref">"desc"</A>" } { print }' $reference > tmp && mv tmp $reference



The 'second line' (I assume to be && mv tmp $reference will move the temporary output file back "on top" of the original file if the awk completes successfully.
This User Gave Thanks to agama For This Post:
# 5  
Old 03-30-2012
yes. i'll try it out.

---------- Post updated at 10:28 PM ---------- Previous update was at 10:22 PM ----------

very cool.
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 insert text within a file?

Hi, I am trying to check for missing dates in a file and would want to insert the missing date into the file. Currently the script is as below #!/bin/ksh dates="dates" cat ${dates} | grep -v "^#" curr_month=`date '+%m` curr_day=`date '+%d` curr_year=`date '+%Y` #curr_month=02... (7 Replies)
Discussion started by: newbie_01
7 Replies

2. Shell Programming and Scripting

How to insert a word into a text file?

Hi, I have a text file with one line having few words separated by space and I need to insert another word on "n"th column/field so that previous word should shift right (to n+1st column). how can I do that? It seems we can do using awk but unable to figure out. Please advise, thanks! ... (11 Replies)
Discussion started by: magnus29
11 Replies

3. Shell Programming and Scripting

Insert value to db from text file

Hi, I have a single value in insertval file. I want to load that value to database with the current date. I tried the below code but it is inserting <NULL> to database and echo $c is also null. cat insertval | awk -F ' ' '{print $1}' > c echo c=$c data=`sqlplus -s user/pwd@hostname <<EOF ... (5 Replies)
Discussion started by: Neethu
5 Replies

4. Shell Programming and Scripting

insert text into empty file

I have an awk script to extract data from several files and create output in the following format as a csv file: xxxx 01/04/12 0001 0 When data is present, I have a file. When no data is available in the input files, I would still like to create a file that looks like this: xxxx... (1 Reply)
Discussion started by: banjo25
1 Replies

5. UNIX for Dummies Questions & Answers

insert comma in a text file

Hi all, I have a text file and I need to insert comma after every 2 digit. -1-1-1-1-1-1-1-1-1 0 0 0 -1-1-1 2 0 0 3 311-1 0 1 -1-1 021 0 011-1-1 033 0I'd like to have this: -1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0 -1,-1,-1, 2, 0, 0, 3, 3,11,-1, 0, 1 -1,-1, 0,21, 0, 0,11,-1,-1, 0,33, 0Thanks for... (7 Replies)
Discussion started by: GoldenFire
7 Replies

6. Shell Programming and Scripting

How to insert text in the middle of a file?

Hi, So far i've made a script that takes two argument, 1st is the contents and the 2nd is the named file. At the moment i've managed to insert new contents as a new line at the top, but i want to ask how can you insert contents in the middle of the file? Source Code #!/bin/bash #Write... (3 Replies)
Discussion started by: zen10
3 Replies

7. Shell Programming and Scripting

Insert Text On file

Hi All, Can someone pls help me to insert some text on a file. my file contains something like below.. AKBULBU, BALUMIL, BATCH,BATCH BOARROB, BOTAKAT, C57896, CAKIOZE, CHECMER, CICOFRA, CISZPAW,2194485 I want output as USER_ID, LOGIN_ID (6 Replies)
Discussion started by: harshakusam
6 Replies

8. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

9. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

10. Shell Programming and Scripting

Cannot read variables after ssh with rc file (KSH)

Greetings all, I'm currently making use of the $HOME/.ssh/rc file to launch an automated shell script immediately after the user has been verified through ssh. The current problem that I'm facing now is that I am unable to use the "read" command anymore... seems like the "read" statements are... (0 Replies)
Discussion started by: rockysfr
0 Replies
Login or Register to Ask a Question