Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > Replace field with awk/sed
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 22nd September 06:51
External User
 
Posts: 1
Default Replace field with awk/sed



Hi Gurus,
I need your help again...

I want to replace second col of each line of passwd file with word
TEST. I tried it in for loop but takes too long for a file with 10000
or more lines.

Here is my test file:

test1:x:Readres Test:/tmp:/bin/false

What i am trying is:

/usr/xpg4/bin/sed 's/:x:/:TEST:/g' passwd > $DEST_DIR/passwd.new

but there might me more instances of :x: so i want to use combination
of awk and sed but not in for loop.
Thanks
  Reply With Quote


 


2 22nd September 06:51
jürgen_kahrs
External User
 
Posts: 1
Default Replace field with awk/sed



awk -F: '{$2="TEST"}1' /etc/passwd
  Reply With Quote
3 22nd September 06:51
chris f.a. johnson
External User
 
Posts: 1
Default Replace field with awk/sed


awk -F: -v OFS=: '{$2 = "TEST"; print }' /etc/passwd --
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Reply With Quote
4 22nd September 06:51
External User
 
Posts: 1
Default Replace field with awk/sed


Thanks a lot Chris. It worked. The only problem was root. I don't want
root user to be changed. How do i skip root for replacing this?
  Reply With Quote
5 22nd September 06:52
gazelle
External User
 
Posts: 1
Default Replace field with awk/sed


!/^root/ { $2 = "TEST" }
19
  Reply With Quote
6 22nd September 06:52
External User
 
Posts: 1
Default Replace field with awk/sed


Thanks but it completely removes root entry. I don't want to replace
root's second col. but still want to print as is.
  Reply With Quote
7 22nd September 06:52
jürgen_kahrs
External User
 
Posts: 1
Default Replace field with awk/sed


awk -F: -v OFS=: '!/^root/{$2 = "TEST"}1' /etc/passwd
  Reply With Quote
8 22nd September 06:52
chris f.a. johnson
External User
 
Posts: 1
Default Replace field with awk/sed


[ Please don't top post ]

awk -F: -v OFS=: '!/^root:/ {$2 = "TEST"}{ print }' /etc/passwd --
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Reply With Quote
9 22nd September 06:52
External User
 
Posts: 1
Default Replace field with awk/sed


Thanks Chris It worked. Thanks a lot
  Reply With Quote
10 22nd September 06:52
gazelle
External User
 
Posts: 1
Default Replace field with awk/sed


No, it doesn't. Look more carefully.

BTW, it should be:

!/^root:/ { $2 = "TEST" }
19
  Reply With Quote
Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Development, Ada, Apple script, Assembler, Awk, Beos, Basic, C, C++, C#, C# .net, .net, .net frameworks, Asp .net, Clarion, Clipper, Clos, Clu, Cobol, Coldfusion, Delphi, Dylan, Eiffel, Forth, Fortran, Haskell, Hermes, Icon, Idl, Java, Java script, Jscript .net, Jcl, Linoleum, Lisp, Lotus, Limbo, Logo, Ml, Mumps, Oberon, Postscript, Pop, Pl1, Prolog, Python, Ruby, Pascal, Perl, Php, Rebol, Rexx, Sed, Sather, Scheme, Smalltalk, Tcl, Vhdl, Vrml, Visual basic, Visual basic .net, Yorick, Mysql, Omnis, Postgresql, Xbase, Access, Oracle, Adabas, Berkeley, Btrieve, Filemaker, Gupta, Db2, Informix, Ingres, Mssql server, Object, Olap, Paradox, Rdb, Revelation, Sybase, Theory, Dbase, Html, Java script, Css, Flash, Photoshop, Corel script, Xml, Tech, Beos, Gem, Hp48, Hpux, Linux, Mac, Ms-dos, Os2, Palm, Solaris, Ti99, Windows, Xenix, Aos, Chorus, Geos, Inferno, Lantastic, Lynx, Mach, Minix, Netware, Os9, Parix, Plan9, Psos, Qnx, Xinu, Sco, Unix, Aix, Aux, 386bsd, Bsdi, Freebsd, Netbsd, Openbsd, Ultrix, Amd, Intel, Aptiva, Buz, Deals, Homebuilt, Overclocking, Programming, Extra forums


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666