search and edit in the same file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search and edit in the same file using awk
# 1  
Old 03-25-2010
Bug search and edit in the same file using awk

Hi,
I am having a user.txt contains the name of users and passwd.txt file contains as passwd.txt
Code:
$cat usr.txt
root
bin
daemon
cap

$cat passwd.txt
root:x:0:0:root:/root:/usr/bin/ksh
bin:x:1:1:bin:/bin:/sbin/csh
daemon:x:2:2:daemon:/sbin:/usr/bin/ksh
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/usr/bin/ksh

I want to change the 7th field of passwd.txt to /bin/sh when usr.txt users match with the 1st field of passwd.txt .
I have done so far ....
Code:
#!/bin/sh
file=/etc/log/user.txt
passwd_file=/var/log/passwd.txt
cat $file |while read i
do 
awk -F: -v i="$i" '/$1 ~ i/{gusb("/usr/bin/ksh", "/bin/sh", $0);print > FILENAME} ' passwd_file 
done

Code:
$cat passwd.txt
root:x:0:0:root:/root:/bin/sh
bin:x:1:1:bin:/bin:/sbin/csh
daemon:x:2:2:daemon:/sbin:/bin/sh
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/usr/bin/ksh

# 2  
Old 03-25-2010
Code:
nawk 'FNR==NR{u[$1];next}$1 in u {$7="/bin/sh"}1' FS=: OFS=: usr.txt passwd.txt

or if you want to edit 'in-place':
Code:
nawk 'FNR==NR{u[$1];next}$1 in u {$7="/bin/sh"}{l[FNR]=$0}END {close(FILENAME);for(;++i in l;) print l[i]>FILENAME}' FS=: OFS=: usr.txt passwd.txt


Last edited by vgersh99; 03-25-2010 at 01:16 PM.. Reason: in-place editing
# 3  
Old 03-25-2010
I think the first awk should be fine enough.
# 4  
Old 03-26-2010
Thanks vgersh99 for the code
Code:
nawk 'FNR==NR{u[$1];next}$1 in u {$7="/bin/sh"}{l[FNR]=$0}END {close(FILENAME);for(;++i in l;) print l[i]>FILENAME}' FS=: OFS=: usr.txt passwd.txt

But in my OS nawk is not supporting But if i go for awk this command work fine for me in SHELL But when i keep this thing in a shell script it won't works. Getting error unable to read passwd.txt file.

Thanks
Manabhanjan
# 5  
Old 03-26-2010
Quote:
Originally Posted by Manabhanjan
Thanks vgersh99 for the code
Code:
nawk 'FNR==NR{u[$1];next}$1 in u {$7="/bin/sh"}{l[FNR]=$0}END {close(FILENAME);for(;++i in l;) print l[i]>FILENAME}' FS=: OFS=: usr.txt passwd.txt

But in my OS nawk is not supporting But if i go for awk this command work fine for me in SHELL But when i keep this thing in a shell script it won't works. Getting error unable to read passwd.txt file.

Thanks
Manabhanjan
Well.... my crystal ball says that your script cannot find or read the passwd.txt file.
I'd suggest to start debugging the script.
Good luck.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep from FileA, search in FileB, edit FileC > Output

Hello, Similar question to my previous posts. I am sorry for the trouble... Just checked my old threads but I can not implement any solution into this case.. My aim is to grab each line in fileA, check it in fileB and merge with fileC (tab separated) in corresponding line as given below: FileA:... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Edit a file using awk ?

Hey guys, I'm trying to learn a bit of awk/sed and I'm using different sites to learn it from, and i think I'm starting to get confused (doesn't take much!). Anyway, say I have a csv file which has something along the lines of the following in it:"test","127.0.0.1","startup... (6 Replies)
Discussion started by: jimbob01
6 Replies

3. Shell Programming and Scripting

How to get awk to edit in place and join all lines in text file

Hi, I lack the utter fundamentals on how to craft an awk script. I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many... (3 Replies)
Discussion started by: n00ti
3 Replies

4. Shell Programming and Scripting

use awk to edit a file..pls help

hey i want to over write the fourth field of a ':' delimited file by first finding the required row by using grep. i have done the following cat file | grep no. | awk -F ':' { $4=count; print $1:$2:$3:$4;} the correct values are being printed but nothin is bein added to the file..please... (5 Replies)
Discussion started by: dhe.arora
5 Replies

5. Shell Programming and Scripting

Sed or Awk or both to edit file

What is an efficient way to remove all lines from the input file which contain a file name? inputfile: ======================= # comment # comment # comment 5 8 10 /tmp 5 8 10 /var/run 5 8 10 /etc/vfstab 5 8 9 /var/tmp 5 8 10 /var/adm/messages... (7 Replies)
Discussion started by: Arsenalman
7 Replies

6. Shell Programming and Scripting

File edit with awk or sed

I have the follwoing file: This looks to be : seperated. For the first field i want only the file name without ".txt" and also i want to remove "+" sign if the second field starts with "+" sign. Input file: Output file: Appreciate your help (9 Replies)
Discussion started by: pinnacle
9 Replies

7. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies

8. UNIX for Dummies Questions & Answers

Search for & edit rows & columns in data file and pipe

Dear unix gurus, I have a data file with header information about a subject and also 3 columns of n rows of data on various items he owns. The data file looks something like this: adam peter blah blah blah blah blah blah car 01 30 200 02 31 400 03 57 121 .. .. .. .. .. .. n y... (8 Replies)
Discussion started by: tintin72
8 Replies

9. Shell Programming and Scripting

How search,edit and save the file

Hi All, I want to edit a file using shell script..For ex...a file called /etc/passwd..here I am searching for "ftp" if it is there just change it to "tftp" without using any temporary file. (3 Replies)
Discussion started by: Vichu
3 Replies

10. Shell Programming and Scripting

script to search and edit scripts

Hi all, can you please help me in this one.. i have a many scripts in a directory & i get many requests to change the code of a particular script. for example file abc.txt contains #!/bin/bash mumbai 102403445 chennai 123980123 delhi 3456268468 kolkata 465376832 #kolkat 462945959 ... (3 Replies)
Discussion started by: geeko
3 Replies
Login or Register to Ask a Question