Searching the pattern and accordingly changing the contents using shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching the pattern and accordingly changing the contents using shell
# 1  
Old 10-05-2013
Searching the pattern and accordingly changing the contents using shell

HI,

I have File1 which contains :-
Code:
admins = anand,satheesha,user1,user2,user3,user4,user5,user10,vishal
nonadmins = read-only
@admins = rw
@nonadmins = r

One shell script, using that I want to change the File1 as per user input (let's say $1) which have value as 'John', so now I want to change File1 so that only admins line should be updated with John, so Now after shell script executed File1 should be like this :-

Code:
admins = anand,satheesha,user1,user2,user3,user4,user5,user10,vishal,John
nonadmins = read-only
@admins = rw
@nonadmins = r

Same way if user want to delete the same file let's say $2=user3, so same file should become like this :-

Code:
admins = anand,satheesha,user1,user2,user4,user5,user10,vishal,John
nonadmins = read-only
@admins = rw
@nonadmins = r

Please help me to do that, BIG THANKS in advance.

Last edited by Don Cragun; 10-05-2013 at 02:00 PM.. Reason: Add CODE tags
# 2  
Old 10-05-2013
Is this a homework assignment?

Do you always want to add one user to the admins list and remove one user from the admins lists every time you call your script? Wouldn't it be better to use an option to indicate whether an invocation of your script was supposed to add or remove an arbitrary number of users (presented as operands) to or from the admins list?
# 3  
Old 10-05-2013
Searching the pattern and accordingly changing the contents using shell

Actually I have one sheel script where I am asking user to enter option 1) for adding 2) deleting, so now on the basis of provided input I have to update file accordingly.

---------- Post updated at 11:28 PM ---------- Previous update was at 11:24 PM ----------

Code:
[root@lab vishal]# echo $tmp
admins = anand,ameo,satheesha,user1,user2,user3,user4,user5,user6,user7,user8,user9,user10,vishal
[root@lab vishal]# echo $tmp1
admins = anand,ameo,satheesha,user1,user2,user3,user4,user5,user6,user7,user8,user9,user10,vishal,rohit

But when I try to replace $tmp with $tmp1 in file1, still it didn't replaced and ",rohit" not added.

Code:
[root@lab vishal]# sed 's/$tmp/$tmp1/g' file1
[groups]
admins = broadhop,anand,ameo,satheesha,user1,user2,user3,user4,user5,user6,user7,user8,user9,user10,vishal
nonadmins = read-only
[/]
@admins = rw
@nonadmins = r


Last edited by Don Cragun; 10-05-2013 at 03:48 PM.. Reason: Add CODE tags, again
# 4  
Old 10-05-2013
Please use code tags as required by forum rules!

Don't use single quotes but double quotes if you want the shell variables to be expanded.
# 5  
Old 10-05-2013
Try

Code:
$ cat test.txt 
admins = anand,satheesha,user1,user2,user3,user4,user5,user10,vishal
nonadmins = read-only
@admins = rw
@nonadmins = r

Code:
#!/bin/bash

check(){
    if [ -z "$1" ];then echo "No user input exiting..";exit;fi
       }

printf "\nOption Availble :\n\t\t  1. Add User\n\t\t  2. Delete User\n\
Usage :\n\t   Add User----> 1 username filename\n\tDelete User----> 2 username filename\n\n\n\tAnswer  : \t"

read inp usr fil
check $inp;check $usr;check $fil

ex=$(grep "$usr" $fil)
if [ $inp -eq "1" ] || [[ $inp -eq "2"  &&  ! -z "$ex" ]];then 

    awk -F, -v arg=$inp -v usr=$usr '

    function remove(us,line){
                                u1=","us; u2=us","
                                error="User Not Found"
                                return ( ((gsub(u1,x,line)) || (gsub(u2,x,line)))==1 )?line:error    
                            }
                            {         
                                print (arg==1)?(/^admins/)?$0","usr:$0:\
                                      (arg==2)?(/^admins/)?remove(usr,$0):$0:"Error"
                            }' $fil

else 
    echo "It seems username not exist in file"    
    exit;
fi

To add user
Code:
$ bash user.sh

Option Availble :
          1. Add User
          2. Delete User
Usage :
       Add User----> 1 username filename
    Delete User----> 2 username filename


    Answer  :     1 dummy_user123 test.txt
admins = anand,satheesha,user1,user2,user3,user4,user5,user10,vishal,dummy_user123
nonadmins = read-only
@admins = rw
@nonadmins = r

To delete user
Code:
$ bash user.sh

Option Availble :
          1. Add User
          2. Delete User
Usage :
       Add User----> 1 username filename
    Delete User----> 2 username filename


    Answer  :     2 user3 test.txt
admins = anand,satheesha,user1,user2,user4,user5,user10,vishal
nonadmins = read-only
@admins = rw
@nonadmins = r

Error if username not found
Code:
$ bash user.sh

Option Availble :
          1. Add User
          2. Delete User
Usage :
       Add User----> 1 username filename
    Delete User----> 2 username filename


    Answer  :     2 akshay test.txt
It seems username not exist in file


Last edited by Akshay Hegde; 10-06-2013 at 05:32 AM..
# 6  
Old 10-06-2013
Hi Akshay,
BIG BIG Thanks, It's working.

I will say thanks from the heart for the expert like you.

-Vishal
# 7  
Old 10-06-2013
Welcome to Forum !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching for a pattern and extracting records related to that pattern

Hi there, Looking forward to your advice for the below: I have a file which contains 2 paragraphs related to a particular pattern. I have to search for those paragraphs from a log file and then print a particular line from those paragraphs. Sample: I have one file with the fixed... (3 Replies)
Discussion started by: danish0909
3 Replies

2. Shell Programming and Scripting

Problem with the awk in searching the contents in a file

Hello all, I am a newbie in awk. I am struggling in this problem for a long.Actually I have two files, filea and fileb. File a is actually a search key through it I have to find the corresponding japanese tag from file b. filea contains the data like this: sm982882 sm1893548 sm2420025... (3 Replies)
Discussion started by: csim_mohan
3 Replies

3. UNIX for Dummies Questions & Answers

Pattern searching

Hi, I need small help from you people. In a directory there are around 150 odd files and few them contain the word "TRACK" and few are not. How can I find out the the list of those files which doesn't contain the word "TRACK"? Thanks, Siba (4 Replies)
Discussion started by: siba.s.nayak
4 Replies

4. Shell Programming and Scripting

Create Multiple files by reading a input file and changing the contents

Being new to this area .I have been assigned a task which i am unable to do . Can any one please help me . Hi I have requirement where i have input file XYZ_111_999_YYYYMMDD_1.TXT and with header and series of Numbers and Footer. I want to create a mutiple output files with each file having a... (2 Replies)
Discussion started by: bhargavkr
2 Replies

5. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

6. Shell Programming and Scripting

searching for a pattern

can anybode tell me ? I want to search for a pattern present in a whole directory and subdirectories's files containg " crat" I tried grep -r "crat" */* ; is it right ? (3 Replies)
Discussion started by: pranabrana
3 Replies

7. Shell Programming and Scripting

Help needed in changing contents of portion of the file

Hi all, I need help on this. I have a file with 4500 bytes. I want to modify the contents of line from 1357 through 3208. 1. Starting 1357, there is 35 occurence of 40 bytes data making it to 2757. 2. The remaining 451 filler with spaces taking it to 3208. Now, the 40 byte data is... (2 Replies)
Discussion started by: jakSun8
2 Replies

8. Shell Programming and Scripting

searching and editing file contents

Can you please help me to edit parts of a file and write into a new file. ===================================== Suppose I have a huge data dump in a file I need to search for a tag in that and cut few lines around that tag in the file. Is there a way to keep track of line numbers and operate on... (18 Replies)
Discussion started by: jayana
18 Replies

9. Shell Programming and Scripting

Pattern searching pattern in c files

I have a problem in searching a specific pattern in c files. My requirement: I have to find all the division operator in all cfiles. The problem is, the multi line comments and single line comments will also have forward slash in it. Even after avoiding these comments also, if both... (6 Replies)
Discussion started by: murthybptl
6 Replies

10. UNIX for Advanced & Expert Users

Searching contents of a file

Is there a way a command or a combination through which i can check the contents of a all files in a directory and get the return as the file names which contains the partiuclar string. (2 Replies)
Discussion started by: thepitzaboy
2 Replies
Login or Register to Ask a Question