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
1 22nd September 04: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 04: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 04: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 04: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 04:52
gazelle
External User
 
Posts: 1
Default Replace field with awk/sed


!/^root/ { $2 = "TEST" }
19
  Reply With Quote
6 22nd September 04: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 04: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 04: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 04:52
External User
 
Posts: 1
Default Replace field with awk/sed


Thanks Chris It worked. Thanks a lot
  Reply With Quote
10 22nd September 04: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




666