Visit Our UNIX and Linux User Community


editing files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting editing files
# 15  
Old 07-22-2009
Quote:
Originally Posted by vgersh99
sorry:
Code:
#!/bin/sh

for i in *.txt 
do
  echo $i
  printf '1,$s/see/saw/g\nwq!\n' | ex -s "${i}" 
done

Thank you so much.. This is working fine.

One more request please could you explain the command line
printf '1,$s/see/saw/g\nwq!\n' | ex -s "${i}" as i am not much familiar with this. Is it possible to execute this line at the command prompt as i am getting an error like "bash: ./r1.txt: Permission denied"

Thanks a lot
# 16  
Old 07-22-2009
Quote:
Originally Posted by cobroraj
Thank you so much.. This is working fine.

One more request please could you explain the command line
printf '1,$s/see/saw/g\nwq!\n' | ex -s "${i}" as i am not much familiar with this. Is it possible to execute this line at the command prompt as i am getting an error like "bash: ./r1.txt: Permission denied"

Thanks a lot
Most likely you don't have 'write' access write for ./r1.txt file - check its permissions.
'ex' is very similar to 'sed' in terms of the command options.
If I do:
Code:
printf '1,$s/see/saw/g\nwq!\n'

I get:
Code:
1,$s/see/saw/g
wq!

The first line looks familiar, doesn't it? Very similar to sed's.
The second line simply instructs 'ex' to write (w) the file and quit (q) 'ex'.
The loop takes care of iterating through the files.
'man ex' for more details.
# 17  
Old 07-22-2009
Quote:
Originally Posted by vgersh99
Most likely you don't have 'write' access write for ./r1.txt file - check its permissions.
'ex' is very similar to 'sed' in terms of the command options.
If I do:
Code:
printf '1,$s/see/saw/g\nwq!\n'

I get:
Code:
1,$s/see/saw/g
wq!

The first line looks familiar, doesn't it? Very similar to sed's.
The second line simply instructs 'ex' to write (w) the file and quit (q) 'ex'.
The loop takes care of iterating through the files.
'man ex' for more details.
Got most of it. Thanks a lot. Only one which i did not get is the meaning of 1,$ after the printf. s/see/saw/g is same as we use for stream ed.

Thanks and regards
# 18  
Old 07-23-2009
Quote:
Originally Posted by cobroraj
whats wrong with this code..please

Code:
#!/bin/sh
allone=`ls *.txt`
echo $allone
for i in ${allone}
do
echo $i
sed -s /${i} <<EOF
g/see/s/see/saw/g
w
q
EOF
done

i have two text files in which i have 'see' and i want to make it 'saw'

the scripts executes fine but i dont see any replacement done.

Thanks
What I see is that you're using 'sed' instead of 'ed'. I haven't tested your specific code, but otherwise is looks ok.

Also, the 1,$ question. This just means perform the search/replace from the beginning (1) to the end ($) in terms of line numbers. Actually, though functionally I'm sure this definition is probably accurate enough, but I've been wrong plenty of times and it may not tell the whole story.

What I really like about this thread is that it demonstrates so well that the question is not whether I can do something via command line/script, but more of which solution do I want to use.

Previous Thread | Next Thread
Test Your Knowledge in Computers #149
Difficulty: Easy
In a 1999, Dennis Ritchie stated that Linux and the BSD operating systems were a continuation of the basis of the Unix design approach and were Unix derivatives.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Editing files

This is a smallpart of my input file.I want to change the ID values of entries having CMW as an entry. Cont_1.266 . CMW 2958 3269 . - 0 PARENT=t:UM06506T0;ID=UM06506P0;rank=6 Cont_1.266 . CMW 3394 3505 . - 0 ... (3 Replies)
Discussion started by: sasdf
3 Replies

2. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

3. Shell Programming and Scripting

editing files.

hello, i have a problem. suppose file.txt i want to add lines over those lines in a file if it starts and ends with how and "?" respectively. i want output like output file.txt thanks (4 Replies)
Discussion started by: yashwantkumar
4 Replies

4. Programming

Editing binary files

I am working in C and need a solution for below problem: I have a binary file, which needs to be edited in such a way no data is loss. For example i have to insert 3 bytes of data at some position without changing the contents of the file. if file has data as:... (2 Replies)
Discussion started by: junaid.nehvi
2 Replies

5. Shell Programming and Scripting

Editing files to add some thing in all the files in a folder

Hi All, I have a folder that contains 100's of files and each file have a similar content like the following format: ((STBJa:200.0,((STBTz:200.0,(STSwe:200.0,(STDUw:200.0,(ST4Bu:200.0,STL2b:200.0):127.0):86.0):80.0):120.0, STAHr:200.0):134.0):200.0,STuNg:200.0);What I need is to do is add "#1"... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

6. Solaris

editing files with script

hi guys, We have to implement new local (/etc/default/login) USER security policy on almost 50 stations. so editing /etc/default/login and /etc/default/passwd will be way too long work. Can we do the same using some script, I mean editing the above files and putting variables as RETRIES=3, ... (5 Replies)
Discussion started by: Asteroid
5 Replies

7. Solaris

changing IP thru editing files

I attempted to change the IP of my Solaris 9 box by changing these files: /etc/hosts /etc/hostname.dmfe0 And when I rebooted my system, the changes did not come up through the reboots. I have searched high and low and this is how you change the IP address permanently. Can anyone tell me what... (2 Replies)
Discussion started by: bluridge
2 Replies

8. OS X (Apple)

the best way of editing .plist files

:( what is the best way of editing the various .plist files without adding third party files? Or is the easiest way is to add third party applications, which ones? please kept in mind that i am not a programmer. mike (1 Reply)
Discussion started by: mehow
1 Replies

9. UNIX for Dummies Questions & Answers

Editing files

hi i would like to know whether i can delete a part of a file in C for eg. if my file contained 1234567890 and i want to delete 456 so that it becomes 1237890 is there a way i can do this. well, one way i can achieve this is by creating a new file, copy whatever i want, then... (2 Replies)
Discussion started by: sameersbn
2 Replies

10. UNIX for Dummies Questions & Answers

editing unix files on NT

i currently am using a unix server and NT pc. i have downloaded a ziped file that should explode into 3 seperate unix based files, however when i unzip it using Alading Expander it displays only 1. This exploded version contains all 3 files ( you can scroll down when viewing the file and see the... (1 Reply)
Discussion started by: pixelmonkey
1 Replies

Featured Tech Videos