create new column for filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting create new column for filename
# 1  
Old 05-06-2011
create new column for filename

Hi,
I created a list with 2 columns.
Each line is from a different file. I am getting these with a loop in Perl. I would like to add a 3rd column with the name of the file that the line is coming from.
I usually use pr to print the filename but this is not working here ... I was wondering if there is a way to do this with awk?
E.g.

House Mouse File1
Tiger Cat File2
Elephant Shoe File1
etc....

Thank you! Smilie
# 2  
Old 05-06-2011
using in-built variable "FILENAME" of awk
# 3  
Old 05-06-2011
I have tried that before but it doesn't work.
I am getting following error message:
awk: syntax error at source line 1
context is
{print >>> 9_test. <<< txt FILENAME}
awk: illegal statement at source line 1
# 4  
Old 05-06-2011
Code:
cat file1
aaa

cat file2
bbb

awk '{print $0,FILENAME}' file1 file2
aaa file1
bbb file2

# 5  
Old 05-06-2011
try this
for multiple list
Code:
for list in list1 list2 ; do for file in file*; do while read -r listsfrom; do 
awk '$0 ~ /'"$listsfrom"'/{print $0 " " FILENAME}' $file >>${list}_listnew; done<$list; done ; done

for single list
Code:
for file in file*; do while read -r listsfrom; do awk '$0 ~ /'"$listsfrom"'/{print $0 " " FILENAME}' $file >>list1_new; done<list1; done

Code:
# cat file1
10 10
11 11
14 16
17 19
20 122

Code:
# cat file2
14 12
12 13
15 17
19 243

Code:
# cat list1
11 11
10 10
12 12
12 13
14 15
16 17

Code:
# for file in file*; do while read -r listsfrom; do awk '$0 ~ /'"$listsfrom"'/{print $0 " " FILENAME}' $file >>list1_new; 
done<list1; done ; more list1_new
11 11 file1
10 10 file1
12 13 file2


regards
ygemici

Last edited by ygemici; 05-06-2011 at 04:37 PM..
# 6  
Old 05-06-2011
let me try to explain again what I am trying to do.
I am creating one single file with its input lines coming from multiple file.
E.g.
file 1
aaa
file 2
bbb

these 2 files are joined into one single file
aaa
bbb

so now I want the latter file to get a new column that marks from which file the lines came from:
aaa file1
bbb file 2
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create 2 Columns from filename

Hi, I have a directory of files with filenames all in the format of xxx_yyy_zzz.pdf Where xxx and yyy changes each filename but zzz stays the same. For example: File 1: Gold_Car_Vehicle.pdf; File n: Red_Truck_Vehicle.pdf. I need to cycle through each file and output a Text File with 2 columns.... (2 Replies)
Discussion started by: ndnkyd
2 Replies

2. UNIX for Dummies Questions & Answers

Create file with last month in filename

Hi, I have a bash script which outputs a file with current year and month in the filename. Last line is: cat result_* > output.$(date +%y%m) So for Feb 2013 this gives me output.1302 Now I am requested to run the script every first day of the month, but with the previous month in... (4 Replies)
Discussion started by: viscacha
4 Replies

3. Shell Programming and Scripting

How to create a cron job to take an uploaded filename and move it?

OK, So complete newbie here. I would normally do this in PHP or through my FTP program script but I can't in this case (the files are not coming from me, coming from a third party FTP upload). I have an FTP server (Linux) accepting files coming in from a standard FTP program. Each file... (2 Replies)
Discussion started by: bull_frog
2 Replies

4. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

5. Shell Programming and Scripting

Merge CSV files and create a column with the filename from the original file

Hello everyone!! I am not completely new to shell script but I havent been able to find the answer to my problem and I'm sure there are some smart brains here up for the challenge :D. I have several CSV files that I need to combine into one, but I also need to know where each row came from.... (7 Replies)
Discussion started by: fransanchezoria
7 Replies

6. UNIX for Dummies Questions & Answers

shell scripts - create a filename with the date appended

I am looking to do something where if I created a file named backup,or whatever it would print a name like “backup_Apr_11_2011”. Thanks citizencro (1 Reply)
Discussion started by: citizencro
1 Replies

7. Shell Programming and Scripting

Extract date from filename and create a new file

Hi, i have a filename CRED20102009.txt in a server 20102009 is the date of the file ddmmaaaa format the complete route is /dprod/informatica/Fuentes/CRED20102009.csv i want to extract the date to create a new file named Parameters.txt I need to create Parameters.txt with this... (6 Replies)
Discussion started by: angel1001
6 Replies

8. Shell Programming and Scripting

how to create variables in loop and assign filename after set command?

Hi, does anybody knows how to manage, that the filenames are assigned to a variable in a loop afer getting them with set command in a ksh, like: set B*.txt i=1 c=$# x=$((c+1)) echo "$x" while ] ; do _ftpfile$i="$"$i echo "$_ftpfile$i" i=$((i+1)) done The first echo returns,... (2 Replies)
Discussion started by: spidermike
2 Replies

9. Shell Programming and Scripting

read mp3 filename and create one XML for each file

Hi: I have a collection of mp3s and I need to create 1 xml file per mp3. I have: recording1.mp3 recording2.mp3 etc and I want to generate this kind of files. recording1.xml recording2.xml and inside each xml file I need to add a url prefix and then the filename at the end. ... (4 Replies)
Discussion started by: jason7
4 Replies

10. Shell Programming and Scripting

create filename with 'DD/MM/YYYY' date format

Hi, I can use the following command to create a file with some name then underscore and then date appended to it in the format 'DD-MM-YYYY': touch "newfile_`date '+%d-%m-%Y'`" But it gives me error when I try with the similar command to create a file with the date format 'DD/MM/YYYY'. I... (4 Replies)
Discussion started by: royalibrahim
4 Replies
Login or Register to Ask a Question