Get a value from a file and insert it in a different file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Get a value from a file and insert it in a different file
# 1  
Old 05-03-2013
Get a value from a file and insert it in a different file

I have a file that contains attribute values, for example, name.out has
Code:
Alice
Bob
Cathy
etc.

I also have a template file for each of name values. I have already created all the files for each name, for sample,
Code:
add.Alice.ldif
add.Bob.ldif
add.Cathy.ldif
etc.

Each of the ldif file has an entry like
Code:
Name=FFFFF

I will need to read the names from the name.out file, and replace dummy name FFFFF with the actual name. At the completion, I should have add.Alice.ldif contains Name=Alice, add.Bob.ldif contains Name=Bob.

So I created the following sh file.
Code:
file="/tmp/name.out"
while IFS= read -r line
do
echo "---------------Change $line:"
sed -i 's/FFFFF/$line/g' /tmp/add.$line.ldif
read -p "Press [Enter] key to continue..."
done <"$file"

When I run the sh file, the names are displayed correctly on the screen, however, they are not written to the ldif files correctly. The ldif files has
Code:
Name=$line (expect Alice)

I probably have missed something. How can I fix it? Thanks.

Last edited by learnix; 05-03-2013 at 11:46 AM..
# 2  
Old 05-03-2013
Try using double quotes for the sed command -

Code:
file="/tmp/name.out"
while IFS= read -r line
do
echo "---------------Change $line:"
sed -i "s/FFFFF/$line/g" /tmp/loadfilt/load.$line.ldif
read -p "Press [Enter] key to continue..."
done <"$file"

The double quotes should evaluate the variable to its actual value instead of using it as is.

Thanks
Bhushan Pathak

Last edited by BhushanPathak; 05-03-2013 at 11:56 AM.. Reason: Not sure how FFFFF got changed to some other text
# 3  
Old 05-03-2013
Thank you. Double quotes solved the problem. read -p "Press [Enter] key to continue..." doesn't work. It does not pause the script and wait for me to enter the "Enter" key to continue.
# 4  
Old 05-03-2013
Not sure why read command is not working for you. It works fine for me. Is it necessary that the script should wait for you to hit enter key every time [in each loop]?

Thanks

Bhushan Pathak
# 5  
Old 05-03-2013
Both read's are reading from standard input, which is being "fed" by <"$file" in your script.

Here's a couple of options:
Code:
file="/tmp/name.out"
exec 3<"$file"
while read line <&3    # same as "while read -u3 line"
do
  echo "---------------Change $line:"
  sed -i "s/FFFFF/$line/g" /tmp/loadfilt/load.$line.ldif
  printf "Press [Enter] key to continue..."
  read
done

Code:
file="/tmp/name.out"
exec 3</dev/tty
while read line
do
  echo "---------------Change $line:"
  sed -i "s/FFFFF/$line/g" /tmp/loadfilt/load.$line.ldif
  printf "Press [Enter] key to continue..."
  read -u3
done < "$file"

# 6  
Old 05-03-2013
I tried the second option and it works (haven't tried the first one). I don't understand what
Code:
exec 3</dev/tty

, and
Code:
read -u3

mean. Is it possible for me to get some explanations? Thanks.

(I don't have to write "Enter" in this specific case, but think later I may need to expand the code to include more functions, so it is better for me to learn now.)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert content from file 1 to file 2 in specific criteria meet

Hi , I'm looking for some code that can copy and paste form file1 to file2 with 2 criterial meet. file1: test "sp-j1" test "sp-j2" test "sp-j3" test "sp-j4" file2: sub Pre_Shorts1 (Status_Code, Message$) global Status !if Message$ <> "" then print... (3 Replies)
Discussion started by: kttan
3 Replies

2. Shell Programming and Scripting

Insert content of file before the first occurrence of a line starts with a pattern in another file

Hi all, I'm new to scripting.. facing some problems while inserting content of a file into another file... I want to insert content of a file (file2) into file1, before first occurrence of "line starts with pattern" in file1 file1 ====== working on linux its unix world working on... (14 Replies)
Discussion started by: Jagadeesh Kumar
14 Replies

3. Shell Programming and Scripting

Insert content of a file to another file at a line number which is given by third file

Hi friends, here is my problem. I have three files like this.. cat file1.txt ======= unix is best unix is best linux is best unix is best linux is best linux is best unix is best unix is best cat file2.txt ======== Windows performs better Mac OS performs better Windows... (4 Replies)
Discussion started by: Jagadeesh Kumar
4 Replies

4. Shell Programming and Scripting

Want to Insert few lines which are stored in some file before a pattern in another file

Hello, I have few lines to be inserted in file_lines_to_insert. In another file final_file, I have to add lines from above file file_lines_to_insert before a particular pattern. e.g. $ cat file_lines_to_insert => contents are abc def lkj In another file final_file, before a... (6 Replies)
Discussion started by: nehashine
6 Replies

5. Shell Programming and Scripting

Insert a complete file into another file after certain fixed line

Hi Friends, I am writing a shell script where it is required to insert a file say file1 into file2 after certain number of fixed lines in file2.. Please help me how this could be done.. Please suggest me any useful links i need to go through to achieve this.... Thanks in advance .. (2 Replies)
Discussion started by: amr89
2 Replies

6. UNIX for Dummies Questions & Answers

Shell script find word from one file and insert in another file

Hi, I am new to shell scripting. I need a bash shell scripts which search and grep a parameter value from input.txt file and insert it in between two semicolon of second line of output.txt file. For example The shell script search an IP address as parameter value from input.txt ... (2 Replies)
Discussion started by: sunilkumarsinha
2 Replies

7. Shell Programming and Scripting

Insert file names when concatenate files into a file

Hi I found the following line would concatenate all test_01 test_02 test_03 files into "bigfile". cat test_* >> bigfile But, what I'm looking for a way to insert each file names in order when concatenated in "bigfile". Thank you samky2005 (2 Replies)
Discussion started by: samky2005
2 Replies

8. Shell Programming and Scripting

insert a header in a huge data file without using an intermediate file

I have a file with data extracted, and need to insert a header with a constant string, say: H|PayerDataExtract if i use sed, i have to redirect the output to a seperate file like sed ' sed commands' ExtractDataFile.dat > ExtractDataFileWithHeader.dat the same is true for awk and... (10 Replies)
Discussion started by: deepaktanna
10 Replies

9. Shell Programming and Scripting

How to insert text into first line of the file and middle of the file?

Script 1 Pre-requisites Create a file with x amount of lines in it, the content of your choice. Write a script that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the very top (the... (3 Replies)
Discussion started by: ali hussain
3 Replies
Login or Register to Ask a Question