Using awk (or whatever) to pull and append data in a new file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using awk (or whatever) to pull and append data in a new file
# 1  
Old 01-27-2009
Using awk (or whatever) to pull and append data in a new file

One of the fortunate things about posting in a "Dummies" forum is you probably aren't expecting a lot of out me...

I'm trying to pull fields from two lines in the same file(s), and then append them together in a new file.

So...I get a nice line-by-line of the first bit of data I'm looking for, by doing this:

Command:
awk '/hostname/ {print $3}' */*.cfg

Output:
host1
host2
host3

And I can get the other data like this (basically the same thing):

Command:
awk '/domain/ {print $3}' */*.cfg

Output:
va.domain.net
ga.domain.net
fl.domain.net


What I'd like to do is end up with a file that has:

Output:
host1.va.domain.net
host2.ga.domain.net
host3.fl.domain.net


I've searched and found various options, but my overall skills are fairly lacking, so I'm hoping so of you guys can help guide me. BTW...this is just one part of a script I'm attempting that will create a delimited file...so you might be hearing from me again! Smilie

Thanks!
# 2  
Old 01-27-2009
Is there a common field in the lines containing '/hostname/' and '/domain/' correlating the two?
How do you know which hostname goes with what domain?
# 3  
Old 01-27-2009
first awk | sed -e 's/$/./' > a
second awk > b

paste -d'' a b > c

file c should contain what you want now.

Last edited by quirkasaurus; 01-27-2009 at 03:16 PM.. Reason: didn't see the extra dot.
# 4  
Old 01-27-2009
They are two separate lines in the same config file. Being in the same config file is really the only common point. For example, if I do a simple grep for what I'm looking for, I get the following:

Command 1:
grep 'hostname' */*.cfg

Output:
1.2.3.4/1.2.3.4.cfg:set hostname host1
2.3.4.5/2.3.4.5.cfg:set hostname host2
3.4.5.6/3.4.5.6.cfg:set hostname host3


Command 2:
grep 'set domain' */*.cfg

Output:
1.2.3.4/1.2.3.4.cfg:set domain va.domain.net
2.3.4.5/2.3.4.5.cfg:set domain ga.domain.net
3.4.5.6/3.4.5.6.cfg:set domain fl.domain.net


Note the structure is as such because we have a backup script for our devices... Also there is the possibility that the device config might not have the domain set...which might throw off the results. But that will be an issue for another day.

I don't know if that helps...

Thanks!
# 5  
Old 01-27-2009
something along these lines....
Code:
nawk '

/domain/ { dom[FILENAME]=$NF }
/hostname/ { host[FILENAME]=$NF }

END {
  for( i in host)
     print host[i] "." (i in dom) ? dom[i] : "<unknown>"
}' */*.cfg

# 6  
Old 01-27-2009
quirkasaurus -

That's one way that works. Needs some tweaking on my end, but it can be done.


vgersh99 -

That's very nice...and it's almost there for me. One of my problems may be that nawk is not installed. I'll play around and see if I can get it to work.

Thanks for your insight thus far!
# 7  
Old 01-27-2009
use 'awk' or 'gawk' instead.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append data with substring of nth column fields using awk

Hi guys, I have problem to append new data at the end of each line of the files where it takes whole value of the nth column. My expected result i just want to take a specific value only. This new data is based on substring of 11th, 12th 13th column that has comma seperated value. My code: awk... (4 Replies)
Discussion started by: null7
4 Replies

2. Shell Programming and Scripting

awk to append data to a file

Hi I search for certain values in a file across many directories using the following awk code awk '/Sl.*thickness/ {Sl=$3;Tt=$NF}END{print Sl, Tt}' DILAT.DAT What I would like to do is write out Sl and Tt obtained from these files from many directories to a single file. So for example if... (2 Replies)
Discussion started by: lost.identity
2 Replies

3. UNIX for Dummies Questions & Answers

pull date from header and append to all records

I did some searches, but couldn't really find what I'm looking for. I have a file formatted as below: BOF ABC CO - XYZ COMM DATA OF 07/05/2011 EBA00000001 sdfa rtyus uyml EBB00000001 54682 984w3 EBA00000002 mkiyuasdf 98234 I want to pull the date from the header record and add it... (4 Replies)
Discussion started by: keeferb
4 Replies

4. Shell Programming and Scripting

I want to append data to same .csv file.

I have a script which has to be scheduled to run 3 times a day. My script picks the required fields from logfile and stores the data in a.csv file. Sample data. my logfile contain: 0097A,0374D,100903,1519,00000606191 0097A,C88RA,100903,0724,00000606105 So the output of first execution... (3 Replies)
Discussion started by: shrima.pratima
3 Replies

5. Shell Programming and Scripting

want to append the data in one file to the another

Hi , i have two log files, i need to combine this as a one log file. i need to do this by SED , test1.log sadadadaadfsaf test2.log adadadadadada i need this in a single file from test 1 to test2.log test2.log(expected result) adadadadadada (7 Replies)
Discussion started by: mhdmehraj
7 Replies

6. Shell Programming and Scripting

How pull the Data from the file and should be mailed

Hi All, I need to pull the data from a file from 2 set of directories & should be mailed. I have a code , it is failing for some reason. Do somebody correct me where my script is going wrong. Directories: 1st Set of Directory /176_PS/Transactions/**/syslog.log ** : prd1 prd2... (2 Replies)
Discussion started by: raghunsi
2 Replies

7. Shell Programming and Scripting

How to pull info under headers in file(awk,grep,while loop)

below is an extract from my file and I am trying to use Awk and grep and a while loop to pull infomation from under neath "HBA WWN=".HBA WWN=" reoccurs all over the file but the 100000c.....number are unique and I want to be able to pull and reference specifi information under this header ever time... (2 Replies)
Discussion started by: kieranfoley
2 Replies

8. Shell Programming and Scripting

how to append line of of data to file

hai..i am new to unix..and i've currently learn shell script.. i have this small problem where i would like to save every data from log file into user directory if the data is equal to the name of the user.. i manage to do that with below script.. i would like to ask if there is any solutions so... (1 Reply)
Discussion started by: meggae
1 Replies

9. UNIX for Dummies Questions & Answers

append data to file

i want to develop a script newdata that writes new data to a file called items the file items has the following headings columns separated by tabs: channel date time programe if i type executable file newdata on the command line with parameters, it should append it to the items files the... (1 Reply)
Discussion started by: fletcher
1 Replies

10. Shell Programming and Scripting

get the data from sybase and append to the file

How to get the data from the sybase database and append the data obtained into the file. For example, I will get the filename 'temp' from the database. This filename is associated with the number 1.6... This number 1.6 needs to be copied into the file that matches the filename in the... (1 Reply)
Discussion started by: vinay123
1 Replies
Login or Register to Ask a Question