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?
# 8  
Old 08-06-2013
Note that awk never modifies the content of your input file, you have to redirect the output to another file to save those changes.

Here is another approach based on some assumptions:
Code:
awk '
        NR == FNR {
                n = match ( $0, / [0-9]+:/ )
                P = substr ( $0, RSTART + 1, RLENGTH - 2 )
                next
        }
        {
                sub ( /[^ ]*PORT[^:]*/, P )
                print
        }
' file1 file2

# 9  
Old 08-08-2013
It seems its not working

file1 = 30002
file2 = __PORT__

@Yoda any idea why ?
# 10  
Old 08-08-2013
This is what I get:
Code:
$ cat file1
ssh -Nf -i id_logs -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=5 -R 30002:127.0.0.1:22 user@hostname

Code:
$ cat file2
ssh -Nf -i id_logs -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=5 -R __PORT__:192.0.0.1:22 user@hostname

Code:
$ awk '
         NR == FNR {
                 n = match ( $0, / [0-9]+:/ )
                 P = substr ( $0, RSTART + 1, RLENGTH - 2 )
                 next
         }
         {
                sub ( /[^ ]*PORT[^:]*/, P )
                sub ( /[^ ]*PORT[^:]*/, P )
                print
         }
' file1 file2
ssh -Nf -i id_logs -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=5 -R 30002:192.0.0.1:22 user@hostname

As I said earlier this code is based on some assumptions, if your input file has data in a different format it will not work. You might have to modify the code as per the input.
# 11  
Old 08-09-2013
I found a working script

Code:
NEW_PORT=$(sed -n 's/^.*\-R\s\([0-9]*\)\:.*/\1/p' file1)
sed -i s/__PORT__/$NEW_PORT/g file2

@yoda thanks for your help
 
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