How to get 1 word from 1 file and print to another file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to get 1 word from 1 file and print to another file?
# 1  
Old 08-06-2013
How to get 1 word from 1 file and print to another file?

Hey
I have 2 scripts, first one with this line:

Code:
ssh -Nf -i id_logs -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=5 -R 30002:127.0.0.1:22 user@hostname

the second script is almost the same:
Code:
ssh -Nf -i id_logs -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=5 -R __PORT__:127.0.0.1:22 user@hostname

I need to copy the port (30002) from the first file and replace it with the word __PORT__ in the second file

What is the best way to do it ?


Thanks!

Last edited by Scott; 08-06-2013 at 12:52 PM.. Reason: Please use code tags
# 2  
Old 08-06-2013
Please use code tags as required by forum rules!

Try
Code:
awk     'NR==FNR        {for (i=1; i<=NF; i++) {if ($i=="-R") {K=$(i+1); break}}; next}
                        {for (i=1; i<=NF; i++) {if ($i=="-R") {$(i+1)=K; break}}; print}
        ' file1 file2

or
Code:
awk     'NR==FNR        {for (i=1; i<=NF; i++) {if ($i=="-R") {K=$(i+1); break}}; next}
                        {sub (/_PORT_[^ \t]*/, K); print}
        ' file1 file2
ssh -Nf -i id_logs -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=5 -R 30002:127.0.0.1:22 user@hostname

# 3  
Old 08-06-2013
Sorry for not using code tags, still new here

I tried running both of them but its not replacing 30002 with the __PORT__

any idea maybe ?
# 4  
Old 08-06-2013
Not sure I understand. You want to keep the _PORT_ and get rid of the 30002? so the result for the first file should be ...._PORT_.... as well as in the second file?
# 5  
Old 08-06-2013
No
I want to replace the __PORT__ of the first file
with 30002 from the second file

so both files will have the same port (30002)
# 6  
Old 08-06-2013
So make file1 the file with the 30002 and file2 the one with the _PORT_ in it. The script will read and keep the 30002 from file1 and overwrite _PORT_ in file2 with that value.
# 7  
Old 08-06-2013
I did try both scripts
but the port in the second file (__PORT__) is not being replaced to "30002"

file1 is the file with 30002
file2 is the file with __PORT__
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

2. Shell Programming and Scripting

Extract the word from the file and print it

I have a file which I am reading and then I need to extract a particualr word and if it matches the line. 2015-01-22 07:30:17,814000 +0900 /INFO: - <ns2:virtualServerid="PH11PK" /> Means if the line contain Virtual server I need to extract the id . Code I wrote#!/usr/bin/perl ... (19 Replies)
Discussion started by: karan8810
19 Replies

3. UNIX for Advanced & Expert Users

Find 2 occurrences of a word and print file names

I was thinking something like this but it always gets rid of the file location. grep -roh base. | wc -l find . -type f -exec grep -o base {} \; | wc -l Would this be a job for awk? Would I need to store the file locations in an array? (3 Replies)
Discussion started by: cokedude
3 Replies

4. UNIX for Dummies Questions & Answers

How to print the specific word in a file.

Hi , My input file is below like that :- $cat abc.txt Service name: test_taf Service is enabled Server pool: test_tac Cardinality: 2 Disconnect: false Service role: PRIMARY Management policy: AUTOMATIC DTP transaction: false AQ HA notifications: true Failover type: SESSION... (3 Replies)
Discussion started by: sp001
3 Replies

5. Shell Programming and Scripting

print lines from a file containing key word

i have a file containing over 1 million records,and i want to print about 300,000 line containing a some specific words. file has content. eg 1,rrt,234 3,fgt,678 4,crf,456 5,cde,drt 6,cfg,123 and i want to print the line with the word fgt,crf this is just an example,my file is so... (2 Replies)
Discussion started by: tomjones
2 Replies

6. Shell Programming and Scripting

How to find and print the last word of each line from a text file

Can any one help us in finding the the last word of each line from a text file and print it. eg: 1st --> aaa bbbb cccc dddd eeee ffff ee 2nd --> aab ered er fdf ere ww ww f the o/p should be a below. ee f (1 Reply)
Discussion started by: naveen_sangam
1 Replies

7. Shell Programming and Scripting

Counts a number of unique word contained in the file and print them in alphabetical order

What should be the Shell script that counts a number of unique word contained in a file and print them in alphabetical order line by line? (7 Replies)
Discussion started by: proactiveaditya
7 Replies

8. Shell Programming and Scripting

search a word in a xml file and print the out put

hi , i m having a html file and this file looks like this <ssl> <name>PIA</name> <enabled>true</enabled> <listen-port>39370</listen-port> </ssl> <log> <name>PIA</name> </log> <execute-queue> <name>weblogic.kernel.Default</name> ... (7 Replies)
Discussion started by: becksram123
7 Replies

9. Shell Programming and Scripting

Finding word in file then print the preceding....

Hi, I am looking for a way to find a particular word in a file then print a line that precedes this line, as well as this line. Sometimes in a log file there is only one word per line and I need to print one of the lines leading up to the single worded line. Example - I can grep for ouch... (5 Replies)
Discussion started by: g_jumpin
5 Replies

10. Shell Programming and Scripting

Print out just a word from the file

Hi, Would like to find a more suitable solution for the following. I have a file eg test.log. In this file, i have to find the line that has "Final rating" which is the starting of the line. I need to print out only 5.75 instead of the whole line using "grep". May I know what suitable command... (8 Replies)
Discussion started by: Kinki
8 Replies
Login or Register to Ask a Question