insert filename into each line of multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting insert filename into each line of multiple files
# 1  
Old 11-22-2011
insert filename into each line of multiple files

I need to insert <filename + comma> into each line of multiple files.

Any idea how to script that?

Regards,
Manu
# 2  
Old 11-22-2011
Can u give some examples please??
# 3  
Old 11-22-2011
Sources filenames:
Code:
<DeviceName>.Inventory.<Date>.txt
<DeviceName>.Inventory.<Date>.txt
<DeviceName>.Inventory.<Date>.txt

File content:
Code:
PID: XX, S/N: YY
PID: XX, S/N: YY
PID: XX, S/N: YY

I would like to add DeviceName in the content:
Code:
DeviceName, PID: XX, S/N: YY
DeviceName, PID: XX, S/N: YY
DeviceName, PID: XX, S/N: YY


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-22-2011 at 06:37 AM.. Reason: Please use code tags for data and code samples.
# 4  
Old 11-22-2011
Code:
[skrynesaver: ~/tmp]$ cat dev.test.today.txt
PID: XX, S/N: YY
PID: XX, S/N: YY
PID: XX, S/N: YY
[skrynesaver: ~/tmp]$ perl -e 'for $file (@ARGV){open (device , "<" , $file);open (temp ,">", "temp.dev");($dev)=$file=~/^([^.]+)./; while(<device>){print temp "$dev, $_";}close temp; close(device);rename("temp.dev" , "$file");}' *.txt
[skrynesaver: ~/tmp]$ cat dev.test.today.txt
dev, PID: XX, S/N: YY
dev, PID: XX, S/N: YY
dev, PID: XX, S/N: YY
[skrynesaver: ~/tmp]$

# 5  
Old 11-22-2011
MySQL

Dear Skrynesaver, Just Great, work as expected

How to adapt $dev because i forget the country name in the source filenames:

<Country>.<DeviceName>.Inventory.<Date>.txt

I would like:
country, dev, PID: XX, S/N: YY

or if it is easier:
country.dev, PID: XX, S/N: YY
# 6  
Old 11-22-2011
Try this...
Code:
awk '{split(FILENAME,arr,"."); print arr[1]", "arr[2]", " $0 > FILENAME}' *.txt

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert the line number from text file to filename output

Hi everyone :) I have a file "words.txt" containing hundreds of lines of text. Each line contains a slogan. Using the code below i am able to generate an image with the slogan text from each line. The image filename is saved matching the last word on each line. Example: Line 1: We do... (2 Replies)
Discussion started by: martinsmith
2 Replies

2. Shell Programming and Scripting

Join multiple files with filename

Please help, I want to join multiple files based on column 1, and put the missing values as 0. Also the colname in the output should say which file the values came from. FILE1 1 11 2 12 3 13 FILE2 2 22 3 23 4 24 FILE3 1 31 3 33 4 34 FILE1 FILE2 FILE3 1 11 0 31 (1 Reply)
Discussion started by: newbie83
1 Replies

3. Shell Programming and Scripting

Creating Multiple data files with spaces in the filename

Hi, I have a list of filenames in the format with Spaces in the filename. As an example : Sample File 1.txt Sample File 2.txt Sample File 3.txt.....I have about 100 files like this. I am trying to create a block of code or use an available command to a) Create a file b) Put in some... (2 Replies)
Discussion started by: ban3rj33
2 Replies

4. Shell Programming and Scripting

Renaming multiple files at once (discarding suffix of the filename after a character)

Hi, I have a lot of files similar to the below order. I want to rename all the files .discrading the time stamp/numbers after cnf. Existing files id_info_20130405.cnf_20130801 in_info_20130405.cnf_20130891 iz_info_20130405.cnf_20130821 in_info_20130405.cnf_20130818... (2 Replies)
Discussion started by: saravanapandi
2 Replies

5. Shell Programming and Scripting

Insert a header record (tab delimited) in multiple files

Hi Forum. I'm struggling to find a solution for the following issue. I have multiple files a1.txt, a2.txt, a3.txt, etc. and I would like to insert a tab-delimited header record at the beginning of each of the files. This is my code so far but it's not working as expected. for i in... (2 Replies)
Discussion started by: pchang
2 Replies

6. UNIX for Dummies Questions & Answers

Insert a line into multiple files

HI All, I want to know if it is possible to print the same message but into 2 different files in the same command? Something like . .. ... echo "Text" >> file1 && file2 this is because i creating a script which i use a log but i don't want to duplicate lines of command just to... (5 Replies)
Discussion started by: lordseiya
5 Replies

7. Shell Programming and Scripting

Adding filename and line number from multiple files to final file

Hi all, I have 20 files (file001.txt upto file020.txt) and I want to read them from 3rd line upto end of file (line 1002). But in the final file they should appear to start from line 1. I need following kind of output in a single file: Filename Line number 2ndcolumn 4thcolumn I... (14 Replies)
Discussion started by: bioinfo
14 Replies

8. Shell Programming and Scripting

Insert Filename into Multiple Files

Hi, We have a folder that has files in the following structure abc.sql def.sql efg.sql . . . xyz.sql I have to find a certain string (say "test") in each file and replace it with the name of the file. For eg. if "test" is present in abc.sql, I want to replace it with "test abc". If... (8 Replies)
Discussion started by: jerome_rajan
8 Replies

9. Shell Programming and Scripting

joining multiple files into one while putting the filename in the file

Hello, I know how to join multiple files using the cat function. I want to do something a little more advanced. Basically I want to put the filename in the first column... One thing to note is that the file is tab delimited. e.g. file1.txt joe 1 4 5 6 7 3 manny 2 3 4 5 6 7 ... (4 Replies)
Discussion started by: phil_heath
4 Replies

10. Shell Programming and Scripting

IBM Informix Load and Insert with multiple files

Hi , Can you guys please help as I have list of files xaa, xab, xac.........xza for eg in which to perform load the 1st (xaa) and insert into table, then only proceed for the 2nd , 3rd and so forth. In other words, before 1st one finished, 2nd one shall not load and insert to table, and so... (0 Replies)
Discussion started by: rauphelhunter
0 Replies
Login or Register to Ask a Question