sed will not write to same file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed will not write to same file
# 1  
Old 06-03-2013
Linux 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?SmilieSmilie

Code:
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."


Last edited by Scott; 06-04-2013 at 01:29 AM.. Reason: Please use code tags
# 2  
Old 06-04-2013
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.
# 3  
Old 06-04-2013
Hi bbaumg02,

Here you go, with classic ed,

Code:
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:

Code:
$./script
Enter you user name...
JOHN
The operation completed successfully.

Code:
$ 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.
This User Gave Thanks to rveri For This Post:
# 4  
Old 06-04-2013
Quote:
Originally Posted by rveri
Hi bbaumg02,

Here you go, with classic ed,

Code:
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:

Code:
$./script
Enter you user name...
JOHN
The operation completed successfully.

Code:
$ 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.
Hi rveri,
If that is what is wanted, why make it so complex? Why not just use something like:
Code:
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.
# 5  
Old 06-04-2013
Thank you rveri, this worked perfect. Appreciate everyone's input and help.
# 6  
Old 06-04-2013
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!
# 7  
Old 06-04-2013
Quote:
Originally Posted by Corona688
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a space with sed?

Hi, I wish insert a line into a file, I created it with : #-------------------------------------------------------------- # The loopback network interface #-------------------------------------------------------------- auto lo iface lo inet loopback ... (7 Replies)
Discussion started by: Arnaudh78
7 Replies

2. UNIX for Advanced & Expert Users

Sed: couldn't write 1378 items to stdout: No space left on device

Hi , In file first line start with "",when trying to remove using sed i am getting the below error .Please advise sed -e 's///g' -e 's/$]//g' segment1.txt >>segment_m1 sed: couldn't write 1378 items to stdout: No space left on device Thanks, Mohan (1 Reply)
Discussion started by: mohan705
1 Replies

3. Shell Programming and Scripting

Awk/sed problem to write Db insertion statement

Hi There, I am trying to load data from a csv file into a DB during our DB migration phase. I am successfully able export all data into a .csv file but those have to rewritten in terms insert statement which will allow for further population of same data in different DB My exiting csv record... (6 Replies)
Discussion started by: bhaskar_m
6 Replies

4. Shell Programming and Scripting

sed command works from cmd line to standard output but will not write to file

Hi all .... vexing problem here ... I am using sed to replace some special characters in a .txt file: sed -e 's/_<ED>_/_355_/g;s/_<F3>_/_363_/g;s/_<E1>_/_341_/g' filename.txt This command replaces <ED> with í , <F3> with ó and <E1> with á. When I run the command to standard output, it works... (1 Reply)
Discussion started by: crumplecrap
1 Replies

5. Shell Programming and Scripting

sed - write remaining input and quit?

I'm an infrequent user of sed. I searched this and other forums and read quite a bit of the O'Reilly book for an answer without luck. In my script I want sed to write the remainder of the input file and quit if it matches a pattern. e.g /Google/ Does anyone have an answer? thank. (5 Replies)
Discussion started by: lyledp
5 Replies

6. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

7. UNIX for Dummies Questions & Answers

using sed to write horizontally & sequentially in a file

Hey there I have two commands to get exactly the data i want but.. i want to write them into a file side by side and in the same order so that they always match. So what i'm hoping to learn from this thread is some of the different ways one could take the output of grepped data and write them in... (7 Replies)
Discussion started by: phpfreak
7 Replies

8. Shell Programming and Scripting

SED command for read and write to different files

Hi I need some help on SED command I am writing a shell script which does the following: 1. Read one line at a time from a file abc.txt which has millions of lines 2. Prefix each line read with some text " 3. Post fix each line read with a quote " 4. Write the new modified... (11 Replies)
Discussion started by: gaurav_1711
11 Replies

9. Shell Programming and Scripting

How To Write Sed Function

hi iam facing problem regarding sed function. give me reply with example of sed function. and what to meanings of $# and @ in scripting please as reply as soon as possible (1 Reply)
Discussion started by: naveeng.81
1 Replies

10. Shell Programming and Scripting

sed to read and write to very same file

This is likely to be a dumb one. How can I use sed to substitute string occurances having it read from an input file and write to this very same file ? I have a file with lots of occurances of '2006', I want to change it to '2007', but I'd like these changes to be saved on the input file. ... (5 Replies)
Discussion started by: 435 Gavea
5 Replies
Login or Register to Ask a Question