pattern match and replace another pattern in same line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pattern match and replace another pattern in same line
# 1  
Old 11-18-2011
pattern match and replace another pattern in same line

I have a pattern
Code:
username:x:32005:32006::/usr/local/user:/bin/bash

I need to match the line containing username and replace /bin/bash with /usr/local/my/bin/noshell

So it becomes
Code:
username:x:32005:32006::/usr/local/user:/usr/local/my/bin/noshell

# 2  
Old 11-18-2011
This will print all lines, but first modify the shell field if the username matches username.

Code:
awk -v FS=: -v OFS=: -v USER="username" '$1==USER { $7="/usr/local/my/bin/noshell" } 1' < input > output

# CHECK THE OUTPUT FIRST before overwriting your input!
# You don't want to mangle /etc/passwd by accident.
# Also, overwrite it, don't do mv output input.  
# Changing /etc/passwd's ownership or permissions would be bad.
cat output > input

# 3  
Old 11-18-2011
It comes as follows.
Code:
user:x:32008:32011::/home/user:/bin/bash:/usr/local/my/bin/noshell

# 4  
Old 11-18-2011
Did you outrun my edit? I briefly posted it with $8 when it was supposed to be $7, which would do exactly that.

I also modified it so you can pass in the username more easily.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 11-18-2011
This worked

Code:
# grep username /etc/passwd | sed 's/\/bin\/bash/\/sbin\/nologin/'
username:x:32008:32011::/home/user:/sbin/nologin

---------- Post updated 11-19-11 at 12:01 AM ---------- Previous update was 11-18-11 at 11:59 PM ----------

Corona688

Just checked your modified stuff. It works well. Thanks Smilie
# 6  
Old 11-18-2011
Using sed alone...
Code:
sed -i '/^username/{s!/bin/bash!/sbin/nologin!g}' /etc/passwd

This will modify the file /etc/passwd... so be careful... You can test it out first by removing -i option...

--ahamed

Last edited by ahamed101; 11-18-2011 at 02:51 PM.. Reason: code change!
# 7  
Old 11-18-2011
Quote:
Originally Posted by anilcliff
This worked

Code:
# grep username /etc/passwd | sed 's/\/bin\/bash/\/sbin\/nologin/'
username:x:32008:32011::/home/user:/sbin/nologin

Not trustworthy. What if they just happen to have username in their comment field, home directory, or path?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

2. Shell Programming and Scripting

Pattern match a path anywhere in the line and replace it with new path

I want to pattern match only path part from below and replace them with new path string. LoadModule jk_module /fldrA/fldrBaf/fldrCaa/modules/mod_jk.so JkWorkersFile /fldrA/fldrBaf/fldrCaa/config/OHS/ohs1/workers.properties JkLogFile... (4 Replies)
Discussion started by: kchinnam
4 Replies

3. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

4. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

5. Shell Programming and Scripting

Print only next pattern in a line after a pattern match

I have 2013-06-11 23:55:14 1Umexd-0004cm-IG <= user@domain.com I need sed/awk operation on this, so that it should print the very next pattern only after the the pattern mach <= ie only print user@domain.com (7 Replies)
Discussion started by: anil510
7 Replies

6. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

7. Shell Programming and Scripting

Help with replace line based on specific pattern match

Input file data20714 7327 7366 detail data20714 7327 7366 main data250821 56532 57634 detail data250821 57527 57634 main data250821 57359 57474 main data250821 57212 57301 main data250821 57140 57159 detail data250821 56834 57082 main data250821 56708 56779 main ... (3 Replies)
Discussion started by: perl_beginner
3 Replies

8. Shell Programming and Scripting

replace pattern after the first pattern match

I need this. aaa OOOOO bbb ccc OOOOO ddd fff ggg OOOOO iii OOOOO I need all OOOOO replaced with PPPPP, but only change after the pattern ggg. So the first two OOOOO should not be changed. OUTPUT should be :- aaa (2 Replies)
Discussion started by: anilcliff
2 Replies

9. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question