How to merge variable data from another file into specific place?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to merge variable data from another file into specific place?
# 1  
Old 01-13-2015
How to merge variable data from another file into specific place?

Hello,

I'm trying to create multiple commands using a variable input from another file but am not getting any successful results.

Basically, file1.txt contains multiple lines with single words:

Code:
<file1.txt>
yellow
blue
black
white

I want to create multiple echo commands with these words in the middle of the echo:

Code:
echo "This color is $file1.txt and will be ready ASAP"

Expected result:

Code:
echo "This color is yellow and will be ready ASAP"
echo "This color is blue and will be ready ASAP"
echo "This color is black and will be ready ASAP"
echo "This color is white and will be ready ASAP"

This is the best I got so far:

Code:
for i in $(cat file1.txt);do echo "$i";echo "This color is $i and will be ready ASAP"

output is:

yellow
This color is yellow and will be ready ASAP
blue
This color is blue and will be ready ASAP
black
This color is black and will be ready ASAP
white
This color is white and will be ready ASAP

Is there any other way that is more effective in doing this task without adding an extra word before the echo? This command seems to take a lot of CPU.

Thanks!
# 2  
Old 01-13-2015
Code:
 
 while read line; do echo "echo "\"This color is $line and will be ready ASAP"\""; done < file1.txt

with awk

Code:
awk '{print "echo ""\"This  color is "$0" and will be ready ASAP\""}' file1.txt


Last edited by senhia83; 01-13-2015 at 03:33 PM..
This User Gave Thanks to senhia83 For This Post:
# 3  
Old 01-13-2015
couple of more options:

Code:
sed 's/.*/echo "This color is & and will be ready ASAP"/' file1.txt
 printf 'echo "This color is %s and will be ready ASAP"\n' $(cat file1.txt)

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 01-13-2015
printf is cool, let's put xargs on top!
Code:
 <file1.txt xargs printf 'echo "This color is %s and will be ready ASAP"\n'

Smilie
These 2 Users Gave Thanks to MadeInGermany For This Post:
# 5  
Old 01-13-2015
Quote:
Originally Posted by senhia83
Code:
 
 while read line; do echo "echo "\"This color is $line and will be ready ASAP"\""; done < file1.txt

[..]
Two quotes too many, which would leave the variable unquoted.
Code:
while read line; do
  echo "echo \"This color is $line and will be ready ASAP\""
done < file1.txt

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 01-13-2015
Quote:
Originally Posted by Scrutinizer
Two quotes too many, which would leave the variable unquoted.
Both gives me the same result though on cygwin !

Code:
$ while read line; do echo "echo "\"This color is $line and will be ready ASAP"\""; done < tmp
echo "This color is yellow and will be ready ASAP"
echo "This color is blue and will be ready ASAP"
echo "This color is black and will be ready ASAP"
echo "This color is white and will be ready ASAP"


$ while read line; do
>   echo "echo \"This color is $line and will be ready ASAP\""
> done < tmp
echo "This color is yellow and will be ready ASAP"
echo "This color is blue and will be ready ASAP"
echo "This color is black and will be ready ASAP"
echo "This color is white and will be ready ASAP"

This User Gave Thanks to senhia83 For This Post:
# 7  
Old 01-13-2015
With your extra quotes the $line is not inside quotes, giving some vulnerability.
For demonstration, add another line with a * to file1.txt
BTW, Chubler's $(cat file1.txt) is also vulnerable.
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut specific column from 2 file and merge

Hi ALL, I have two file. I need to combine these two file based on a layout. I used the below code and able to extract the record. But now able to insert that to a 3'rd file in between the extract FILE 1 CAID NUMBER 1-20 TID NUMBER 21-22 LABEL CHAR 23-44 BASE 45-60... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

2. Shell Programming and Scripting

Modifying text file records, find data in one place in the record and print it elsewhere

Hello, I have some text data that is in the form of multi-line records. Each record ends with the string $$$$ and the next record starts on the next line. RDKit 2D 15 14 0 0 0 0 0 0 0 0999 V2000 5.4596 2.1267 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 ... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

3. Shell Programming and Scripting

awk to place specific contents filename within text file

I am trying to use awk to place the contens of a filename in $1 and $2 followed by the data in the text file. Basically, put the filename within the text file. There are over 1000 files in the directory and as of now each file is saved with a unique name but it is not within the file. Thank you... (10 Replies)
Discussion started by: cmccabe
10 Replies

4. Shell Programming and Scripting

script for inserting line at specific place in file

I use zentyal for my server admin, which is great but zentyal auto-generates config file on boot and hence overwrites any changes made directly to config files. In order to allow multiple user access to a MS ACCESS database, I need to customise the smb.conf file and add the following line to the... (9 Replies)
Discussion started by: barrydocks
9 Replies

5. Shell Programming and Scripting

Read from file specific place in file using inode

Hello, I am using tcsh on AIX. I would like to write a script that does the following: 1. given an inode, how do I find exactly the name of the file? I know I could do this using ls -i | grep <inode> but it returns: <inode> <filename>. I need some string manipulation or something to... (1 Reply)
Discussion started by: lastZenMaster
1 Replies

6. Shell Programming and Scripting

Merge two file data together based on specific pattern match

My input: File_1: 2000_t g1110.b1 abb.1 2001_t g1111.b1 abb.2 abb.2 g1112.b1 abb.3 2002_t . . File_2: 2000_t Ali england 135 abb.1 Zoe british 150 2001_t Ali england 305 g1111.b1 Lucy russia 126 (6 Replies)
Discussion started by: patrick87
6 Replies

7. Solaris

What is the best way to copy data from place to another place?

Dear Gurus, I need you to advice or suggestion about the best solution to copy data around 200-300G from serverA(location A) to serverB(location B). Normally, I will share folder and then copy but it takes too long time(about 2 days). Do you have any suggestion or which way should be... (9 Replies)
Discussion started by: unitipon
9 Replies

8. Shell Programming and Scripting

Jump to a specific place in a file?

If I cat a file And want to go to the first instance of a particular value - what command would I use? And then from that point where I jumped to search for another value - but only search from that point forward not before the file? Thanks~ (2 Replies)
Discussion started by: llsmr777
2 Replies

9. Shell Programming and Scripting

Splitting av file in 2 at specific place based on textpattern

I have a file that I want to split in 2 (with Bourne shell sh) preferably. The file consists of groups of lines separated by newline. The file can vary in length, so I need to check number of groups of text. Here's an example ====EXAMPLE START==== #fruit banana #color yellow #surface smooth... (0 Replies)
Discussion started by: borgeh
0 Replies

10. Shell Programming and Scripting

insert file 1 at a specific place of file 2

Hello, I need to search in file2 for class A : public B { and insert right after that the content of file1. I am a bit lost as to which tools (which bash functions, awk...). I should use. Thanks for some directions here. Regards (1 Reply)
Discussion started by: JCR
1 Replies
Login or Register to Ask a Question