Unix Shell Script for modifying a file

Unix Shell Script for modifying a file

Post by Sreeniv » Tue, 17 Sep 2002 21:21:34



Can any one help me in writing a shell script to open a file , insert a
number after the given position .

For Example : my file file.sh contains

1:H001:10:400:500:20:30
2:H002:30:600:700:40:50
3:H003:40:800:900:50:60

I want to insert zero after the position 4th in all the
rows , the final file should be like this .

file.sh

1:H001:10:400:0:500:20:30
2:H002:30:600:0:700:40:50
3:H003:40:800:0:900:50:60

 
 
 

Unix Shell Script for modifying a file

Post by Nils O. Sel?sd » Tue, 17 Sep 2002 21:30:42



> Can any one help me in writing a shell script to open a file , insert a
> number after the given position .

> For Example : my file file.sh contains

> 1:H001:10:400:500:20:30
> 2:H002:30:600:700:40:50
> 3:H003:40:800:900:50:60

> I want to insert zero after the position 4th in all the
> rows , the final file should be like this .

> file.sh

> 1:H001:10:400:0:500:20:30
> 2:H002:30:600:0:700:40:50
> 3:H003:40:800:0:900:50:60

cat text  |awk -F ':' '{print $1":"$2":"$3":"$4":"0":"$5":"$6":"$7}'
bah.. only thing I could think of now..

 
 
 

Unix Shell Script for modifying a file

Post by Chris F.A. Johnso » Tue, 17 Sep 2002 22:15:07



> Can any one help me in writing a shell script to open a file , insert a
> number after the given position .

> For Example : my file file.sh contains

> 1:H001:10:400:500:20:30
> 2:H002:30:600:700:40:50
> 3:H003:40:800:900:50:60

> I want to insert zero after the position 4th in all the
> rows , the final file should be like this .

> file.sh

> 1:H001:10:400:0:500:20:30
> 2:H002:30:600:0:700:40:50
> 3:H003:40:800:0:900:50:60

       while IFS=: read a b c d e f g
       do
         echo "$a:$b:$c:$d:0:$e:$f:$g"
       done < file.sh

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Unix Shell Script for modifying a file

Post by John W. Krah » Wed, 18 Sep 2002 08:30:41



> Can any one help me in writing a shell script to open a file , insert a
> number after the given position .

> For Example : my file file.sh contains

> 1:H001:10:400:500:20:30
> 2:H002:30:600:700:40:50
> 3:H003:40:800:900:50:60

> I want to insert zero after the position 4th in all the
> rows , the final file should be like this .

> file.sh

> 1:H001:10:400:0:500:20:30
> 2:H002:30:600:0:700:40:50
> 3:H003:40:800:0:900:50:60

perl -i~ -pe's/(([^:]+:){4})/${1}0:/' yourfile

John
--
use Perl;
program
fulfillment

 
 
 

1. Need advice for modifying passwd file in shell script

I'm writing a shell script that locks out users by putting a *LK* in the
/etc/passwd file.  I know this is a potentially dangerous thing and I want
to know how to handle it right.

What I'll probably do is copy the password file to a temp file, to all the
modifications there and then replace the passwd file.  I need to know what
else needs to be done so that things go right.  I think I read something
about using a makepasswd command to rebuild the password database?

This script will run on SunOS and there is a shadow file although I'm not
modifying it.

Any advice?

Thanks,

Stacy

2. Accessing timeofday value without using system call

3. Copy files using filenames from text files with shell script or bash script

4. WebCam

5. Help W/Unix Shell Script - File Splitting Based on Text String

6. EQL load balancing

7. Converting unix shell scripts to dos batch files

8. bonded interface in linux

9. Unix shell script for folders and files moving

10. unix shell script to access a file line by line

11. How to run a exe file in Unix shell script?

12. Q: How to use sh shell script to modify parent process env variables?

13. modifying the printcap with shell- or perl-scripts