user2994493
user2994493

Reputation: 13

how can I add number lines to a file with in this script

So I have this script file that automatically saves the date and time to a file when the terminal is open. But i'm having a hard time putting number lines in it. I tried cat -n, grep -n, ls -l, but I either get errors or doesn't increment. As you can see:

My Script

echo $(date) >> .test

I would like to see something like this:

0 : Tue Feb 15 13:10 EST 2014

1 : Tue Feb 18 12:10 EST 2014

2 : Tue Feb 18 10:10 EST 2014

3 : Tue Feb 19 13:22 EST 2014

Upvotes: 1

Views: 58

Answers (3)

Evan Grim
Evan Grim

Reputation: 5225

Here's a solution:

echo $(wc -l < filename) : $(date) >> filename

Upvotes: 1

Zhehao Mao
Zhehao Mao

Reputation: 1789

Try saving the last number in a file. Then, in your script, read in the number, increment it, and write it back to the file. If you are using bash, something like this should work.

num=$(cat number.txt)
num=$(($num+1))
echo $num > number.txt

Upvotes: 0

Drewness
Drewness

Reputation: 5072

If you pipe the result into cat, you can use the -n option to number each line like so:

ls | grep "whatever" | cat -n

Upvotes: 2

Related Questions