Adding filename to each line of the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding filename to each line of the file
# 1  
Old 08-01-2010
Data Adding filename to each line of the file

Hi,
I am a relative new bee in scripting. I need to develop a script such that the code would iterate through each file in a source directory and append every line of the file with '|' and the corresponding file filename.

eg
INPUT file IF927_1.dat -
H|abc
D|xyz|MARKETING_CAMPAIGN|1-D65XE|1|1|1||||
F|1

after using my shell script should look like
H|abc|IF927_1.dat
D|xyz|MARKETING_CAMPAIGN|1-D65XE|1|1|1|||||IF927_1.dat
F|1|IF927_1.dat


So far, reffering to similar post in this forum I have developed the below code:
Code:
SRC_DIR=$1  
DEST_DIR=$2

#Get the list of files in descending order 
C_C_FILE=  `ls $SRC_DIR/IF927_*.dat`

#Append each line in the file and copy to the dest directory
for FILE in C_C_FILE
do
    BASEFILENAME=`basename $FILE`
    APPENDING="|"$BASEFILENAME    
    sed 's/$/|$APPENDING/g' $SRC_DIR/$BASEFILENAME >  $DEST_DIR/$BASEFILENAME   
done

But the output of this code is :

H|abc|IF927_1.dat
|$APPENDING
D|xyz|MARKETING_CAMPAIGN|1-D65XE|1|1|1|||||IF927_1.dat
|$APPENDING
F|1|IF927_1.dat
|$APPENDING

There are two issues here:
1) The sed command is not translating the APPENDING variable
2) The sed command is appending the text in the new line and not at the end of the corresponding line.

Any quick response would be highly appreciated.

regards
# 2  
Old 08-02-2010
Code:
$ cat details.txt 
2424|asdsd|121
1212|adad|9898
12|6767

$ awk 'BEGIN {OFS="|"} {print $0,FILENAME}' details.txt 
2424|asdsd|121|details.txt
1212|adad|9898|details.txt
12|6767|details.txt

Another hint:

$ fname='details.txt'; while read line; do echo -n "$line|"; echo $fname; done < details.txt

2424|asdsd|121|details.txt
1212|adad|9898|details.txt
12|6767|details.txt

Regarding your sed question # 1, you should use double quotes for sed to expand the variable.
This User Gave Thanks to jaduks For This Post:
# 3  
Old 08-02-2010
Thanks

Thanks a lot Jaduks:
I found the problem to my code as well:

Code:
SRC_DIR=$1  
DEST_DIR=$2

#Get the list of files in descending order 
C_C_FILE=  `ls $SRC_DIR/_IF927*.dat`

#Iterate through all the files in the src directory
for FILE in C_C_FILE
do
    BASEFILENAME=`basename $FILE`

    APPENDING="|"$BASEFILENAME    

#Append each line in the file and copy to the dest directory
    sed "s/$/"`echo $APPENDING`"/g" $SRC_DIR/$BASEFILENAME >  $DEST_DIR/$BASEFILENAME     

done

The reason why my code would not show on 1 single line is coz the length of the file name is too long and hence showing in 2 lines.

Anyways thanks a lot for your alternative solution which looks quite more elegant and short than mine Smilie

regards
# 4  
Old 08-02-2010
Hello, you can replace
Code:
"`echo $APPENDING`"

with
Code:
$APPENDING

The reason it wasn't working before, were the single quotes...
# 5  
Old 08-02-2010
MySQL

Code:
sed "s/$/"`echo $APPENDING`"/g" $SRC_DIR/$BASEFILENAME >  $DEST_DIR/$BASEFILENAME

change to
Code:
 
eval sed 's/$/"`echo $APPENDING`"/g' $SRC_DIR/$BASEFILENAME >  $DEST_DIR/$BASEFILENAME

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding line in a file using info from previous line

I have a shell script that looks something like the following: mysql -uroot db1 < db1.sql mysql -uroot db2 < db2.sql mysql -uroot db3 < db3.sql mysql -uroot db4 < db4.sql .... different db names in more than 160 lines. I want to run this script with nohup and have a status later. So,... (6 Replies)
Discussion started by: MKH
6 Replies

2. Shell Programming and Scripting

Adding new line to file

Hi everyone, currently I writing a script for comparing 2 variable in 2 line then output the line with equal value to new file. However, the new file only contain last line only, the earlier line was delete. I do google my problem but still not find the way out. Sorry for my English. Thank you... (10 Replies)
Discussion started by: lazy_bear
10 Replies

3. 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

4. Shell Programming and Scripting

Adding tab/new line at the end of each line of a file

Hello Everyone, I need a help from experts of this community regarding one of the issue that I am facing with shell scripting. My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line. The... (20 Replies)
Discussion started by: Sourav Das
20 Replies

5. UNIX for Dummies Questions & Answers

Replacing first line of file by >filename

Hi All, I have a set of files named S5_SK1.chr01 S5_SK1.chr02 S5_SK1.chr03 ..... and the first line of these files is >SK1.chr01 >SK1.chr02 >SK1.chr03 ..... Can anyone suggest how I can change the first line of all these files with the filename itself? So my expected output for the first lines of... (14 Replies)
Discussion started by: pawannoel
14 Replies

6. UNIX for Dummies Questions & Answers

Extract first line of a file and use as filename

I am trying to find a way to create a script which will extract the first line of a file and then rename the file (or create a new file with the same content as the old file) using the first line as the name. The first line being a single word, that is. I am hopeless at programming, if anyone can... (5 Replies)
Discussion started by: s.plumb
5 Replies

7. Shell Programming and Scripting

Grabing Date from filename and adding to the end of each line in the file.

Hi, I have 24 .dat files something like below. The file name starts with “abc” followed by two digit month and two digit year. Is there a way to grab the month and year from each filename and append it to the end of each line. Once this is done I want to combine all the files into file... (1 Reply)
Discussion started by: rkumar28
1 Replies

8. Shell Programming and Scripting

adding a line to a file

I want to add a line at the beginning and at the end of a file.. e.g. echo "at the beginning.." > tmp_file && cat file >> tmp_file && echo "last line" >> tmp_file && mv tmp_file file is there a nice way for doing that?? Thx (2 Replies)
Discussion started by: andy2000
2 Replies

9. Shell Programming and Scripting

Adding filename into file content

Dear Experts, Please help to teach me how to add the filename into the file content. Actually the file name are EVENTS-20050912. ***************New output that I want*************** EVENTS-20050912 03:33:37 ALARM: BTSSPAN-277-1 30-18013 EVENTS-20050912 12:10:28 ALARM: BTSSPAN-297-2... (1 Reply)
Discussion started by: missutoomuch
1 Replies

10. Shell Programming and Scripting

How to adding the filename into file contents

Dear Experts, Please help to teach me how to add the filename into the file content so that i can get the output below:- Actually the file name ***************New output that I want*************** =====2005-11-12===== EVENTS-20050912 03:33:37 ALARM: BTSSPAN-277-1 30-18013... (2 Replies)
Discussion started by: missutoomuch
2 Replies
Login or Register to Ask a Question