Output file name and file contents of multiple files to a single file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Output file name and file contents of multiple files to a single file
# 1  
Old 02-04-2019
Output file name and file contents of multiple files to a single file

I am trying to consolidate multiple information files (<hostname>.Linux.nfslist) into one file so that I can import it into Excel. I can get the file contents with cat *Linux.nfslist >> nfslist.txt. I need each line prefaced with the hostname. I am unsure how to do this.

--- Post updated at 06:33 PM ---

If I could even just get the filename printed before the contents, that would even be better.
# 2  
Old 02-04-2019
Hello Kentlee65. Try so
Code:
cat *Linux.nfslist | sed 's/.*/<hostname>&/' >> nfslist.txt

but sed can read by mask himself

Last edited by nezabudka; 02-04-2019 at 02:43 PM..
# 3  
Old 02-04-2019
I need it to pull the filename not <hostname>. Here is the output from your command and an example of one of the filename I'm pulling information from.

<hostname>.Linux.nfslist

<hostname>aix_admin_server:/tools/HourlyProcess nfs 25G 10G 16G 40% /tools/HourlyProcess
<hostname>aix_admin_server:/tools/HourlyProcess nfs 25G 10G 16G 40% /tools/HourlyProcess
<hostname>aix_admin_server:/tools/HourlyProcess nfs 25G 10G 16G 40% /tools/HourlyProcess
<hostname>aix_admin_server:/tools/HourlyProcess nfs 25G 10G 16G 40% /tools/HourlyProcess
<hostname>aix_admin_server:/tools/HourlyProcess nfs 25G 10G 16G 40% /tools/HourlyProcess
<hostname>aix_admin_server:/tools/HourlyProcess nfs 25G 10G 16G 40% /tools/HourlyProcess
<hostname>aix_admin_server:/tools/HourlyProcess nfs 25G 10G 16G 40% /tools/HourlyProcess
# 4  
Old 02-04-2019
Try
Code:
awk '{print FILENAME, $0}' *Linux.nfslist

and redirect as needed. If you want but the hostname part of the file name, try
Code:
awk 'FNR==1 {split (FILENAME, HOST, ".")} {print HOST[1], $0}' *Linux.nfslist

These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 02-04-2019
and so
Code:
awk 'FNR == 1 {print FILENAME} 1' *Linux.nfslist >> nfslist.txt

# 6  
Old 02-04-2019
RudiC

The first line with an append to my text file worked perfectly. Had to clean up the data with a tr but it imported beautifully.

Thank you
This User Gave Thanks to Kentlee65 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

How to merge the multiple data files as a single file?

Hi Experts, I have created multiple scripts and send the output to new file, getting this output to my mailbox on daily basis. I would like to send the all outputs to a single file, need to merge all file outputs on a single file. For example, Created script for df -h > df.doc grep... (7 Replies)
Discussion started by: seenuvasan1985
7 Replies

3. Shell Programming and Scripting

Splitting a single file to multiple files

Hi Friends , Please guide me with the code to extract multiple files from one file . The File Looks like ( Suppose a file has 2 tables list ,column length may vary ) H..- > File Header.... H....- >Table 1 Header.... D....- > Table 1 Data.... T....- >Table 1 Trailer.... H..-> Table 2... (1 Reply)
Discussion started by: AspiringD
1 Replies

4. Shell Programming and Scripting

Using a single "find" cmd to search for multiple file types and output individual files

Hi All, I am new here but I have a scripting question that I can't seem to figure out with the "find" cmd. What I am trying to do is to only have to run a single find cmd parsing the directories and output the different file types to induvidual files and I have been running into problems.... (3 Replies)
Discussion started by: swaters
3 Replies

5. Shell Programming and Scripting

Combining columns from multiple files into one single output file

Hi, I have 3 files with one column value as shown File: a.txt ------------ Data_a1 Data_a2 File2: b.txt ------------ Data_b1 Data_b2 Data_b3 Data_b4 File3: c.txt ------------ Data_c1 Data_c2 Data_c3 Data_c4 Data_c5 (6 Replies)
Discussion started by: vfrg
6 Replies

6. Shell Programming and Scripting

Combine Multiple Files into Single One File One after other

I am trying to combine 4 .dat files into one single Output file Inputs are:- file123.dat, file256.dat, file378.dat & file490 Expected Output:- FileName=file1 {text from file1} EOF {blank line} FileName=file2 {text from file2} EOF {blank line} FileName=file3 {text from file3} EOF... (4 Replies)
Discussion started by: lancesunny
4 Replies

7. Shell Programming and Scripting

Redirect all logs files contents into a single log file

Hi , I have a Data cleansing process which creates different log file for each step , when the process runs it creates following log files in below order: p1_tranfrmr_log.txt p1_tranfrmr_stats.txt p2_globrtr_log.txt p2_globrtr_stats.txt p3_cusparse_log.txt p3_cusparse_stats.txt ' '... (8 Replies)
Discussion started by: sonu_pal
8 Replies

8. UNIX for Dummies Questions & Answers

Redirecting the multiple commands output to single file

Hi, I am new to shell scripting and have a question. I would like to redirect the output of multple commands to single file, From what I read from the bash manpage and from some searching it seems it cannot be done within the shell except setting up a loop. Is it? I am running all clearcase... (1 Reply)
Discussion started by: saku
1 Replies

9. Shell Programming and Scripting

Grep a pattern given in one file at other file and display its corresponding contents as output.

***************************************** Right now i have this current system. I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which... (7 Replies)
Discussion started by: abinash
7 Replies

10. Shell Programming and Scripting

Create Multiple files by reading a input file and changing the contents

Being new to this area .I have been assigned a task which i am unable to do . Can any one please help me . Hi I have requirement where i have input file XYZ_111_999_YYYYMMDD_1.TXT and with header and series of Numbers and Footer. I want to create a mutiple output files with each file having a... (2 Replies)
Discussion started by: bhargavkr
2 Replies
Login or Register to Ask a Question