appending data to last line of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting appending data to last line of file
# 1  
Old 06-21-2010
appending data to last line of file

A friend contacted me recently with an interesting question. We got something worked out, but I'm curious what answers you all can come up with.

Given a shell script (in bash) that processes a bunch of data and appends it to a file, how would you append the date, time, and a filename to the last line of the output?

Example:

output file:

Code:
355.355 234.123. 229.812
355.355 234.153. 229.815
355.335 234.123. 229.812

Desired output
Code:
355.355 234.123. 229.812
355.355 234.153. 229.815
355.335 234.123. 229.812 06/21/10 00:12:43 data.txt

Assume further that this has to happen separately from the part of the script that actually produces the data. It's a function that someone else wrote, and can't be changed. For what it's worth, I first suggested that some sort of time/date/file block be added at the start of the data file. But the requirements above are what my friend wanted, so that's the question.
# 2  
Old 06-21-2010
Like this?
Code:
sed '$s/.*/& here comes your text/' infile
355.355 234.123. 229.812
355.355 234.153. 229.815
355.335 234.123. 229.812 here comes your text

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 06-21-2010
Ahh, nice. I should have said "end up with it appended to the same data file that you're writing in the first place.

So, in pseudocode:

Code:
while (still have some input to handle)
     generate_data() # writes to data1.txt

# ok we're done, add the date stamp
(some sort of sed magic) >> data1.txt

# 4  
Old 06-21-2010
Something like this? Keeping the original date of the file:
Code:
[ "$1" != "" ] || exit 1
timestamp=$(ls -l "$1" | awk '{print $6,$7}' )
sed '$s|.*|& '"$timestamp ${1##*/}|" "$1" > "$1.$$" && touch -d "$timestamp" "$1.$$" && mv -f "$1.$$" "$1"

More precise with GNU versions:
Code:
[ "$1" != "" ] || exit 1
timestamp=$(ls -l --full-time "$1" | awk '{print $6,$7}' )
sed -i '$s|.*|& '"$timestamp ${1##*/}|" "$1" && touch -d "$timestamp" "$1"


Last edited by Scrutinizer; 06-21-2010 at 04:11 PM..
# 5  
Old 06-22-2010
If you have GNU sed you could use -i to edit the file directly.
# 6  
Old 06-22-2010
Very nice. Thanks to all who contributed.

I had forgotten (if I ever knew) that you could use $ as a line marker.
# 7  
Old 06-22-2010
Code:
#!/bin/sh

f=$1

ed -s "$f" <<EOED
a
 $(date +'%D %T') $f
.
-,.j
w
q
EOED


If you prefer a less readable but more compact (on screen) approach, the following is equivalent:
Code:
printf 'a\n %s %s\n.\n-,.j\nw\nq\n' "$(date +'%D %T')" "$f" | ed -s "$f"

Regards,
Alister

Last edited by alister; 06-22-2010 at 01:55 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delimiter appending in a data file if we receive a less columns than expected

Required No.of field = 12 Let say you got a “~” delimited input file and this file has 6 input fields and now I want to add 12-5=7 number of “~” into this input file in order to make it 12 fields datafile can have n number of records ex., a~b~c~d~12~r a~b~c~d~12~r a~b~c~d~12~r... (19 Replies)
Discussion started by: LJJ
19 Replies

2. Shell Programming and Scripting

Bash - Appending to specific line in file

I'm working on a personal project, a multiplication quiz script for my kids. In it, the user's performance will be recorded and written to a file. After they've played it a little while, it will start to focus more on the ones that give them the most trouble-- that take a long time to answer or... (4 Replies)
Discussion started by: treesloth
4 Replies

3. Shell Programming and Scripting

Help with data appending to a file

Hi I have a file called text.txt contains x y z when i run a command i will get output like below x 20 z 30 i want to insert x, z value in text.txt file and should be like this x 20 y 0 z 30 can anyone help me please? (1 Reply)
Discussion started by: siva kumar
1 Replies

4. UNIX for Dummies Questions & Answers

Appending Date at the end ONLY in first line of file

Hi, My requirement is to append a date in format DDMMYYYYHHMISS at the end of first line of file which is HEADER. I am trying command sed -i '1s/.*/&<date_format>/' <file_name> Where <date_format>=`date +%m%d%Y%H%M%S` I am somehow misisng the right quotes ti get this added in above... (2 Replies)
Discussion started by: sanjaydubey2006
2 Replies

5. Shell Programming and Scripting

Appending data to the end of a line

I have searched the forms and I can not find info on appending each line of one file to the same line of another file. I know that I can cat one file to another or append the 2nd file to the end of the 1st but not quite sure how to append one line of data to another. For example File 1 has ... (2 Replies)
Discussion started by: scw132
2 Replies

6. Shell Programming and Scripting

Appending a column in one file to the corresponding line in a second

It appears that this has been asked and answered in similar fashions previously, but I am still unsure how to approach this. I have two files containing user information: fileA ttim:/home/ttim:Tiny Tim:632 ppinto:/home/ppinto:Pam Pinto:633 fileB ttim:xkfgjkd*&#^jhdfh... (3 Replies)
Discussion started by: suzannef
3 Replies

7. Shell Programming and Scripting

appending data to file

Hi. I wrote a very simple script and it doesn't work :( It is supposed to go to a certain directory, execute some command and append the output to the file "expo.dat" what it does is that it writes to the file only one entery. I dont know if Im using the write synthax for "append". Here is... (3 Replies)
Discussion started by: Enigma08
3 Replies

8. Shell Programming and Scripting

Appending a line in a file after a particular line

Hello, I have got a C file in which I would like to add an include statement of my own. There are already a few include statements and mine should come right after the last existing one (to be neat). With grep I can get the lines containing the word 'include' and I guess I should feed the... (7 Replies)
Discussion started by: maxvirrozeito
7 Replies

9. Shell Programming and Scripting

Appending the line number and a seperator to each line of a file ?

Hi, I am a newb as far as shell scripting and SED goes so bear with me on this one. I want to basically append to each line in a file a delimiter character and the line's line number e.g Change the file from :- aaaaaa bbbbbb cccccc to:- aaaaaa;1 bbbbbb;2 cccccc;3 I have worked... (4 Replies)
Discussion started by: pjcwhite
4 Replies

10. Shell Programming and Scripting

Appending data at the first and last line of a file

Hi, Am trying to write a shell script which will append a header and a footer to an existing file. Header will contain details like the current date while the footer will contain the no: of records listed in the file. I know we can use the CAT command, but i have no clue abt the syntax to... (4 Replies)
Discussion started by: brainstormer
4 Replies
Login or Register to Ask a Question