Add file's date at beginning of every line in file


 
Thread Tools Search this Thread
Operating Systems Linux Add file's date at beginning of every line in file
# 1  
Old 09-30-2010
Add file's date at beginning of every line in file

How would I do that?

/Rutger
# 2  
Old 09-30-2010
Code:
$ ruby -ne 'BEGIN{s=File.mtime("file").to_s.gsub(/\s+/,"")};print "#{s} #{$_}" ' file

This User Gave Thanks to kurumi For This Post:
# 3  
Old 09-30-2010
Unfortunately I'm not having access to Ruby. I was more thinking of a solution that uses sed, awk, cat, or grep.

/Rutger
# 4  
Old 10-03-2010
something along the lines of
Code:
 sed -e s/^/"`date` "/g FILE

Note: You will need the -i parameter for sed to change the file in place. The code above is non disruptive and non destructive to the original file.
This User Gave Thanks to mark54g For This Post:
# 5  
Old 10-03-2010
Quote:
Originally Posted by kurumi
Code:
$ ruby -ne 'BEGIN{s=File.mtime("file").to_s.gsub(/\s+/,"")};print "#{s} #{$_}" ' file

FYI, on OSX:

Code:
apple:~ neo$ vi aaa
apple:~ neo$ ruby -ne 'BEGIN{s=File.mtime("file").to_s.gsub(/\s+/,"")};print "#{s} #{$_}" ' aaa
-e:1:in `mtime': No such file or directory - file (Errno::ENOENT)
        from -e:1

# 6  
Old 10-03-2010
I worked it out myself. I actually needed to do this operation on a couple of hundred files. The quick-and-dirty script looks like this:

Code:
#!/bin/bash
for i in *
do
awk -v string1=`ls $i |cut -c -14` '{ print string1";" $0 }' $i>> merged
done

FYI: the first 14 characters of the filename contained the date and time the file was created so I used that as the source for getting the file's date. The ";" character is the delimiter.

Rutger
# 7  
Old 10-03-2010
Quote:
Originally Posted by Neo
FYI, on OSX:
I presume "aaa" is an empty file? FYI, on Linux and using 1.9.1,
Code:
$ wc -l < aaa
0
$ ruby -ne 'BEGIN{s=File.mtime("file").to_s.gsub(/\s+/,"")};print "#{s} #{$_}" ' aaa

It doesn't spew any errors. Thanks for this info on OSX
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add one line in the beginning of the file?

Hi gurus, I need add one new line in the begining of current file. current file abc cde add xyz output file newline abc cde add xyz (6 Replies)
Discussion started by: ken6503
6 Replies

2. Shell Programming and Scripting

Add new line at beginning and end of a file

Hi, I have a specific requirement to add text at the beginning and end of a plain text file. I tried to use "sed" with '1i' and '$a' flags but these required two separate "sed" commands separated with "|". I am looking for some command/option to join these two in single command parameter. ... (6 Replies)
Discussion started by: bhupinder08
6 Replies

3. Shell Programming and Scripting

Append Multiple files with file name in the beginning of line

Hi, I have multiple files having many lines like as bvelow: file Name a.txt abc def def xyz 123 5678 file Name b.txt abc def def xyz 123 5678 I would like to append files in the below format to a new file: file Name c.txt (7 Replies)
Discussion started by: rramkrishnas
7 Replies

4. UNIX for Dummies Questions & Answers

Add word/value at the beginning of each line in a file

how to add value/word at the beginning of each line in a file ? i have file number.txt and the output is below 1000 1001 1002 1003 1004 i want to add 000 at the beginning of each line, desire output is below 0001000 0001001 0001002 0001003 0001004 and so on please advise how... (5 Replies)
Discussion started by: jason6247
5 Replies

5. Shell Programming and Scripting

Append file name to the beginning of each line

I want to append file names at the beginning of a line for each row file content abc.txt.gz 123|654|987 bcd.txt.gz 876|trf|kjh I want a single output file with below format abc.txt.gz|123|654|987 bcd.txt.gz|876|trf|kjh This one is working but only with unzip files,need to have... (3 Replies)
Discussion started by: rakesh5300
3 Replies

6. UNIX for Dummies Questions & Answers

Adding one string at the beginning of each line in a file

Hi, I have file a.txt as below. I want to add one string root beginning of each line. Sample file a.txt aaa bbb ccc Sample output Root aaa Root bbb Root ccc Can any one help me on this? (6 Replies)
Discussion started by: siba.s.nayak
6 Replies

7. UNIX for Dummies Questions & Answers

write new line at the beginning of an existing file

I was trying to find out the easiest way to write new line to the beginning of an exisiting file. I am using KSH. (5 Replies)
Discussion started by: sailussr
5 Replies

8. Shell Programming and Scripting

Add text to beginning of file

Hi I need to add text to the beginning of a file in the same way that cat will put file contents at the end of a file. I want to do this with many files eg cat newtext >> /usr/home/*/*.bat Any ideas? (2 Replies)
Discussion started by: donkey
2 Replies

9. Shell Programming and Scripting

input a line at the beginning of every file in a directory?

if need to input a word or anything at the beginning of every file in a directory. how do i accomplish this? say the file is named hyperten. how do i make hyperten the first line of every file in a given directory? thanks (6 Replies)
Discussion started by: Terrible
6 Replies

10. Shell Programming and Scripting

Adding a character in the beginning of every line in a .dat file

How can i add a character(#) in the beginning of every line in a .dat file (2 Replies)
Discussion started by: Cool Coder
2 Replies
Login or Register to Ask a Question