Sponsored Content
Top Forums Shell Programming and Scripting Searching the pattern and accordingly changing the contents using shell Post 302860311 by Akshay Hegde on Saturday 5th of October 2013 03:13:06 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 09:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy