Need to replace last field in a file,if first field matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to replace last field in a file,if first field matches
# 1  
Old 07-04-2016
Need to replace last field in a file,if first field matches

Hi,

Need to replace last field in a file(/etc/passwd) ,if first filed matches with particular username.

Scenario:

Code:
cat testfor1
deekshi:x:7082:7082::/home/deekshi:/bin/bash
harini1:x:7083:7083::/home/harini1:/bin/bash

Here,if first field contains "deekshi", then i should replace /bin/bash with /bin/nologin

Expected Output:

Code:
cat testfor1
deekshi:x:7082:7082::/home/deekshi:/bin/nologin
harini1:x:7083:7083::/home/harini1:/bin/bash

As of now i tried like below,

Code:
sed '/deekshi/s|$|/bin/nologin|' testfor1

But its not working


TIA
# 2  
Old 07-04-2016
DON'T mess around manually with important system files (like /etc/passwd), as this thread may teach. And even less so do automatic mass changes with scripts... Use the respective system provided tools, e.g. usermod.

If you do, you should know EXACTLY what you are doing and how to reverse possibly wrong-going modifications.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-04-2016
Howsoever, "But its not working" is not an utmost good starting point for an analysis. You should describe exactly what in the current behaviour doesn't fulfill your needs or what the error message(s) be.

I guess the target string is appended to the entry, not replacing the last field's value? So you should provide that value, so the substitute has something to replace:
Code:
sed '/^deekshi/s|[^:]*$|/bin/nologin|' file
deekshi:x:7082:7082::/home/deekshi:/bin/nologin
harini1:x:7083:7083::/home/harini1:/bin/bash

On top, it is good practice to anchor the search pattern (if possible), here to the start-of-line, to be safe of false positives.
# 4  
Old 07-04-2016
Do not use sed to modify /etc/passwd or any important system file. Why? Because other files you do not know about may also need changes.

In Linux to lock an acount:
Code:
usermod -L  username
passwd -l  username

# 5  
Old 07-04-2016
Quote:
Originally Posted by Sumanthsv
Hi,

Need to replace last field in a file(/etc/passwd) ,if first filed matches with particular username.

Scenario:

Code:
cat testfor1
deekshi:x:7082:7082::/home/deekshi:/bin/bash
harini1:x:7083:7083::/home/harini1:/bin/bash

Here,if first field contains "deekshi", then i should replace /bin/bash with /bin/nologin

Expected Output:

Code:
cat testfor1
deekshi:x:7082:7082::/home/deekshi:/bin/nologin
harini1:x:7083:7083::/home/harini1:/bin/bash

As of now i tried like below,

Code:
sed '/deekshi/s|$|/bin/nologin|' testfor1

But its not working


TIA
For /etc/password, please use chsh or usermod

i.e
Code:
usermod -s /sbin/nologin deekshi

Code:
chsh -s /sbin/nologin deekshi

Notice: In Linux the path is, quite often, /sbin/nologin. You can check the shells available with:
Code:
chsh -l

For testfor1
Code:
perl -palF':' -e '/deekshi/ and s|$F[-1]|/bin/nologin|' testfor1

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 remove lines in file if specific field matches

I am trying to remove lines in the target.txt file if $5 before the - in that file matches sorted_list. I have tried grep and awk. Thank you :). grep grep -v -F -f targets.bed sort_list grep -vFf sort_list targets awk awk -F, ' > FILENAME == ARGV {to_remove=1; next} > ! ($5 in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

3. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

4. Shell Programming and Scripting

Replace a field with a character as per the field length

Hi all, I have a requirement to replace a field with a character as per the length of the field. Suppose i have a file where second field is of 20 character length. I want to replace second field with 20 stars (*). like ******************** As the field is not a fixed one, i want to do the... (2 Replies)
Discussion started by: gani_85
2 Replies

5. Shell Programming and Scripting

Displaying the first field if the second field matches the pattern using Perl

Hi, I am trying with the below Perl command to print the first field when the second field matches the given pattern: perl -lane 'open F, "< myfile"; for $i (<F>) {chomp $i; if ($F =~ /patt$/) {my $f = (split(" ", $i)); print "$f";}} close F' dummy_file I know I can achieve the same with the... (7 Replies)
Discussion started by: royalibrahim
7 Replies

6. Shell Programming and Scripting

Compare two files Field by field and output the result in another file

Hi Friends, Need Help. I have file1.txt as File1.txt |123|A|7267|Hyder|Cross|Sell|7801 |995|A|7051|2008|Lunar|New|Year|Promotion|7801 |996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801 |997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801 File2.txt... (7 Replies)
Discussion started by: i150371485
7 Replies

7. Shell Programming and Scripting

sed to replace a field from a line with another field

i have something like this, cat filename.txt hui this si s"dfgdfg" omeone ipaddress="10.19.123.104" wel hope this works i want to replace only 10.19.123.104 with different ip say 10.19.123.103 i tried this sed -i "s/'ipaddress'/'ipaddress=10.19.123.103'/g" filename.txt ... (1 Reply)
Discussion started by: vivek d r
1 Replies

8. Shell Programming and Scripting

adding field values if field matches

hi i have file as below , i want to add duplicate records like bell_bb to one record with valuve as 15 ( addition of both ) any oneline awk script to achive this ? header 0 CAMPAIGN_NAME 1 Bell_BB 14 Bell_MONTHLY 803 SOLO_UNBEATABLE 644 Bell_BB 1 Bell_MONTHLY 25 SOLO_UNBEATABLE... (4 Replies)
Discussion started by: raghavendra.cse
4 Replies

9. Shell Programming and Scripting

Displaying lines of a file where the second field matches a pattern

Howdy. I know this is most likely possible using sed or awk or grep, most likely a combination of them together, but how would one go about running a grep like command on a file where you only try to match your pattern to the second field in a line, space delimited? Example: You are... (3 Replies)
Discussion started by: LordJezoX
3 Replies

10. Shell Programming and Scripting

Help to replace a field in one file with a field from another file

I have to replace a field in one file with a field from other file. I came across this awk command to replace a field with one string nawk -F'|' -v OFS='|' '$2="replace"' temp2 > temp3 I need to have something like cut -f2 -d "|" temp1 (a field from other file) instead of 'replace' Is... (8 Replies)
Discussion started by: savant
8 Replies
Login or Register to Ask a Question