sed will not write to same file

I am a scripting noob, and having much trouble getting this script to run correctly. Upon start it should read the home directory and sort the files into a text file. Then have user enter a user name and insert it into line 1 of the same text file. Then have the date insert into the second line of the text file. Followed by a conformation message. So far it writes the the text file but creates separate files for the inputs. I am so lost... help anyone?:confused::confused:

ls --sort=extension > directory.txt
echo "Enter you user name..."
read input
sed -n "1w a\ '$input'" directory.txt
date=`date`
sed -n "2w a\ '$date'" directory.txt
echo "The operation completed successfully."

I find your description confusing.

What OS are you using? Do you care what shell is used to do this? (If so, which shell?)

What (described in English) do you want to want the output to be?

Do you want that output to be written to your terminal, or do you want it to be saved in a file? (If in a file, what is the name of that file?)

Is the following an accurate description of what you want:
Create an output file named directory.text that contains the following data:

  1. A user's name (the script is to ask the user to supply a name and save the response as the first line in the output file).
  2. The current date date and time should be saved as the second line in the output file.
  3. The remainder of the output file should be a sorted list of files in the current directory sorted with the primary sort key being the file name extension.

Hi bbaumg02,

Here you go, with classic ed,

ls --sort=extension > directory.txt
echo "Enter you user name..."
read input
printf "1i\n$input\n.\nw\nq" | ed -s directory.txt

date=`date`
printf "2i\n$date\n.\nw\nq" | ed -s directory.txt
echo "The operation completed successfully."

ed works where sed won't
Enjoy , Have fun!.

---------- Post updated 06-04-13 at 03:44 AM ---------- Previous update was 06-03-13 at 11:06 PM ----------

bbaumg02,
Here how it ll look like:

$./script
Enter you user name...
JOHN
The operation completed successfully.
$ ls -lrt
total 20
-rwxrwxr-x 1 adms adms 236 Jun  4 00:39 script
-rw-rw-r-- 1 adms adms  98 Jun  4 00:39 test1
-rw-rw-r-- 1 adms adms 144 Jun  4 00:39 test3
-rw-rw-r-- 1 adms adms 144 Jun  4 00:39 test2
-rw-rw-r-- 1 adms adms   0 Jun  4 00:40 unix.com
-rw-rw-r-- 1 adms adms  79 Jun  4 00:40 directory.txt
$
$ cat directory.txt
JOHN
Tue Jun  4 00:40:42 PDT 2013
script
test1
test2
test3
unix.com
directory.txt
$

Hope this is what you were looking for.

1 Like

Hi rveri,
If that is what is wanted, why make it so complex? Why not just use something like:

printf "Enter you user name: "
read input
{      echo "$input"
       date
       ls --sort=extension
} > directory.txt
[ $? -eq 0 ] && echo "The operation completed successfully."

I don't see the need for ed or sed if this is the desired output.

Thank you rveri, this worked perfect. Appreciate everyone's input and help.

To summarize Don Cragun's post, I'm amazed how often people do ls -l then just throw away all that extra output. The -l is optional! You're making yourself do extra work!

I have met several Linux users who -for some reason- always use ls -ltr , no matter what.

Some of them even think that without "ltr" they're not properly using it.

I don't know what book or Linux course teaches that practice or where does the habit come from.

ls -l to looks it good/better, so the visualization effect ll make a difference than normal command output in column(1), that mixes up. Some time extra output is better. : )

Not when you throw that away, it doesn't! People use ls -l then go through strange contortions to make it look like ordinary ls. It's a very strange habit.