2

I would like the output to be to be changed as below.

  • The line that contains (GROUP ) R should have sg starting off the line and replace the (GROUP ) R with props(USERLIST)
  • The line that contains (USER ) R should have su starting off the line and remove the (USER ) R from the end of the line.

file1.txt

host server1  
    group1        (GROUP  ) R  
    group2        (GROUP  ) R  
    group3        (GROUP  ) R  
    group4        (GROUP  ) R  
    user1         (USER   ) R  
    user2         (USER   ) R  
host server2  
    group5        (GROUP  ) R  
    group6        (GROUP  ) R  
    group7        (GROUP  ) R  
    user3         (USER   ) R  
    user4         (USER   ) R  
host server3  
    group8        (GROUP  ) R  
    group9        (GROUP  ) R  
    user5         (USER   ) R  
    user6         (USER   ) R 

Expected Output

host server1  
sg group1 props(USERLIST)  
sg group2 props(USERLIST)  
sg group3 props(USERLIST)  
sg group4 props(USERLIST)  
su user1  
su user2  
host server2  
sg group5 props(USERLIST)  
sg group6 props(USERLIST)  
sg group7 props(USERLIST)  
su user3  
su user4  
host server3  
sg group8 props(USERLIST)  
sg group9 props(USERLIST)  
su user5  
su user6 
2

With AWK:

 awk '{if( $2 == "(GROUP" ) {print "sg "$1" props(USERLIST)"} else if ( $2 == "(USER"  )  {print "su "$1} else print $0}' file1.txt

With SED:

 sed -i '/GROUP/ s/.*\(group[0-9]\).*/sg \1 props(USERLIST)/g;/USER/ s/.*\(user[0-9]\).*/su \1/g' file1.txt
  • Awesome so much Siva the awk one worked. With the sed I got error "sed: illegal option -- i". thanks again. – Binish Jul 9 '18 at 14:01
  • The inplace i option is a feature of gnu's sed. You will need to redirect the output to a temporary file with option -e – msp9011 Jul 9 '18 at 14:04
2
sed -e '/(GROUP  ) R/{s//props(USERLIST)/; s/^/sg /;}' \
    -e '/(USER   ) R/{s///;                s/^/su /;}' \
    -e 's/[[:blank:]][[:blank:]]*/ /g' file >newfile

This is a sed command with three sed expressions.

  1. For lines containing (GROUP ) R: replace that string with props(USERLIST) and insert sg at the start of the line.
  2. For lines containing (USER ) R: remove that string and insert su at the start of the line.
  3. Replace multiple consecutive whitespace characters with a single space on all lines.

The result is written to newfile and for the given input, the output will be

host server1
sg group1 props(USERLIST)
sg group2 props(USERLIST)
sg group3 props(USERLIST)
sg group4 props(USERLIST)
su user1
su user2
host server2
sg group5 props(USERLIST)
sg group6 props(USERLIST)
sg group7 props(USERLIST)
su user3
su user4
host server3
sg group8 props(USERLIST)
sg group9 props(USERLIST)
su user5
su user6
0
sed '/(GROUP[ \t]\+)[ \t]R/s/^.*\(group[[:digit:]]*\).*/sg \1 props(USERLIST)/;/(USER[ \t]\+)[ \t]R/s/.*\(user[[:digit:]]*\).*/su \1/' file1.txt
0

Very similar to @SivaPrasath's solution, but matching the whole (GROUP/USER ) R

awk '/\(GROUP\s*\) R/ { printf "sg %s props(USERLIST)\n",$1; };
     /\(USER\s*\) R/  { printf "su %s\n",$1 }' file1.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.