SED command for read and write to different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED command for read and write to different files
# 8  
Old 11-07-2008
sed 's/^/hello gaurav "/g;s/$/"/g' abc.txt > xyz.txt
# 9  
Old 11-07-2008
Try this:

Code:
sed 's/.*/Some text "&/' file > newfile

# 10  
Old 11-07-2008
sed 's/^/hello gaurav "/g;s/$/"/g' abc.txt > xyz.txt
# 11  
Old 11-07-2008
Quote:
Originally Posted by gaurav_1711
i tried..

sed "s/^/prefix/;s/\n/sufix/" oldfile > newfile

but it is only adds the prefix...
The reason is that there is no "\n" at the end of the line as far as sed is concerned. There is a far more easy device to match line ends or line beginnings, which you have already seen used here:

"^" at the beginning of a regex means "line beginning". "x" finds any x, "^x" finds only an "x" on first position of the line.

"$" at the end of a regex means "line end". "x$" would match only a line ending with "x".

There is another metacharacter you have seen used here: "&" in a replacement string this means "everything that has been matched by the regex". Example:

echo "xxxabcxxx" | sed 's/abc/y&y/' ==> will give "xxxyabcyxxx"
echo "xxxabcxxx" | sed 's/ab/y&y/' ==> will give "xxxyabycxxx"

You should now be able to create a correct sed script yourself. In fact you have two ways of doing it shown here.

I hope this helps.

bakunin
# 12  
Old 11-07-2008
thanks everybody for their help

i am tried the following script and it works fine

sed "s/^/prefix/;s/$/suffix/" oldfile > newfile
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read and write operations on files.

Dears. kindly guide !!! I have data, which is delimited by | . it should contain 26 columns, but one column data contain | makes few row to 27 columns. I want to find rows have 27 columns and then concatenate the specific columns to single column to make it 26 columns. Kindly help, Can... (3 Replies)
Discussion started by: sadique.manzar
3 Replies

2. Shell Programming and Scripting

How to read and write last modified timestamp to files?

Need help reading file last modified date in format: Filename (relative path);YYYYMMDDHHMMSS And then write it back. My idea is to backup it to a text file to restore later. Checked this command but does not work: Getting the Last Modification Timestamp of a File with Stat $ stat -f... (5 Replies)
Discussion started by: Tribe
5 Replies

3. 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

4. Programming

read/write files

Hi all, I have a problem with some read/write functions. I have a .bin file which contains a lot of structures as follows: struct alumno { char id; char apellido1; char apellido2; char nombre; float nota1p; float nota2p; float notamedia; char photofilename; }; What I have... (3 Replies)
Discussion started by: Attenea
3 Replies

5. Shell Programming and Scripting

how to write 2 variables while using read command

Hello All i have input files contains 2 values as following 20-Oct-09 Z59408009 20-Oct-09 Z59423060 and i am using the following script cat /home/or/input.txt | awk '{print $2}' >log count=0 while read line; do count=$(( count + 1 )) echo "UPDATE SAT_JRLTRT SET AVT='X' WHERE... (6 Replies)
Discussion started by: mogabr
6 Replies

6. Shell Programming and Scripting

Find all files with group read OR group write OR user write permission

I need to find all the files that have group Read or Write permission or files that have user write permission. This is what I have so far: find . -exec ls -l {} \; | awk '/-...rw..w./ {print $1 " " $3 " " $4 " " $9}' It shows me all files where group read = true, group write = true... (5 Replies)
Discussion started by: shunter63
5 Replies

7. 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

8. UNIX for Dummies Questions & Answers

how to read or write device files

hi everybody, i am working in device drivers.As a beginner to this field ,i dont know how to read or write device files. Will copy_to_user and copy_from_user help me? I have created a device file using mknod command .Can anybody help me in this regard :confused thanks in advance sriram (1 Reply)
Discussion started by: sriram.ec
1 Replies

9. Shell Programming and Scripting

Script with read/write Files

Hello, I am a Newbie in ksh Unix Script. So I must write a ksh/sh script who read character at a position in a File. So also it must read all the lines who belongs at these characters , then write these lines in a another File. Can you help me , or give little councils to advance with my... (5 Replies)
Discussion started by: steiner
5 Replies

10. UNIX for Dummies Questions & Answers

How to read and write files one line at a time.

Hi! All! I am wirting a shell script in which i want to read one line at a time from the file and write it simultaneouly to other file one line at a time. Please let me know about some shell utility which can help me out. Thanx. If further clarifications are needed then please let me know... (2 Replies)
Discussion started by: s_chopra
2 Replies
Login or Register to Ask a Question