0

Wondering if somebody can help me with grep or awk. I have 2 files, the first one contains students IDs and a second that contains config files that relates to each student

File 1 (oldstudent.txt)

    5678D956 
    45S87954
    56898K78
    4D856898
    556987F8
    23657D87

File 2 (app_path.config)

    /export/home/5678D956/Scripts
    /export/home/5678D956/FTP
    /export/home/5678D956/HomeDirs   
    /export/home/4487984D/Scripts
    /export/home/4487984D/FTP
    /export/home/4487984D/HomeDirs    
    /export/home/45S87954/Scripts
    /export/home/45S87954/FTP
    /export/home/45S87954/HomeDirs      
    /export/home/56898K78/Scripts
    /export/home/56898K78/FTP
    /export/home/56898K78/HomeDirs  
    /export/home/909878S4/Scripts
    /export/home/909878S4/FTP
    /export/home/909878S4/HomeDirs 

I would like to have it so that if a student exists in File 1, it comments out their configuration setting and saves as File 3 (app_path_new.config)

    #/export/home/5678D956/Scripts
    #/export/home/5678D956/FTP
    #/export/home/5678D956/HomeDirs
    /export/home/4487984D/Scripts
    /export/home/4487984D/FTP
    /export/home/4487984D/HomeDirs    
    #/export/home/45S87954/Scripts
    #/export/home/45S87954/FTP
    #/export/home/45S87954/HomeDirs      
    #/export/home/56898K78/Scripts
    #/export/home/56898K78/FTP
    #/export/home/56898K78/HomeDirs  
    /export/home/909878S4/Scripts
    /export/home/909878S4/FTP
    /export/home/909878S4/HomeDirs 

I have been trying with grep, sed and awk, and don't seem to be going anywhere.

Thanks

tom1123
  • 33
  • 1
  • 5
  • Could you share the commands you used? – jraynal Apr 25 '17 at 17:27
  • For what it's worth, `grep -vf file1 file2` will remove all lines from file 2 that match lines in file 1. So there's a grep solution if you tolerate removal of the lines rather than commenting them out. – stevesliva Apr 25 '17 at 17:34
  • 2
    Was hard to pick a duplicate because there are so many... – 123 Apr 25 '17 at 17:37

3 Answers3

0

awk approach:

awk 'NR==FNR{a[$1]; next}{if($4 in a) $1="#"$1;}1' OFS="/" oldstudent.txt FS="/" app_path.config

The output:

#/export/home/5678D956/Scripts
#/export/home/5678D956/FTP
#/export/home/5678D956/HomeDirs   
/export/home/4487984D/Scripts
/export/home/4487984D/FTP
/export/home/4487984D/HomeDirs    
#/export/home/45S87954/Scripts
#/export/home/45S87954/FTP
#/export/home/45S87954/HomeDirs      
#/export/home/56898K78/Scripts
#/export/home/56898K78/FTP
#/export/home/56898K78/HomeDirs  
/export/home/909878S4/Scripts
/export/home/909878S4/FTP
/export/home/909878S4/HomeDirs

To redirect the output to the new file add the following to the end of the above command:

> app_path_new.config
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
-2

You can do this with a while loop and sed:

cp app_path.config app_path_new.config
cat oldstudent.txt | 
    while read line
    do
        sed -i "s@.*/$line/.*@# &@g" app_path_new.config
    done
Behrooz
  • 41
  • 1
  • Wow.. Works like a charm. Need to learn sed myself, as it seems very powerful. Thank you very much again – tom1123 Apr 25 '17 at 17:51
-2
cp app_path.conf app_path_new.conf
for id in `cat oldstudent.txt`; do sed -i '/'$id'/s@^@#@' app_path_new.conf; done