sed to remove braces from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to remove braces from a file
# 1  
Old 05-28-2012
sed to remove braces from a file

i need to search for user belonging to group 'macusr' and the extract the user name .
i am able to write a oneliner for this using awk + sed + tr

i am using tr to chop off '()' from the output. but i want to use it in sed itself . can someone please help me with that

file contents

Code:
uid=1700(john) gid=1700(john) groups=4500(dba),1604(users),61343(macusr)
uid=1456(ben) gid=1456(ben) groups=11001(cap)



Code:
cat myfile | awk '/macusr/{print $1}' | sed 's/^uid=[0-9]*//g' | tr -d '()'

i want to remove that tr part and want to achieve its functionality in sed part itself. Please help me.
# 2  
Old 05-28-2012
Try

Code:
sed -n '/^uid=[0-9]*(\([^)]*\)).*groups=.*(macusr).*/s||\1|p' <filename>

By the way, that's a useless use of cat...awk can handle filenames as arguments.
# 3  
Old 05-28-2012
Hi

Code:
awk -F '[=)(]' '/macusr/{print $3}' file

OR

Code:
sed -n '/macusr/s/uid=[0-9]*(\([a-z]*\).*/\1/p' file

Guru.

Last edited by guruprasadpr; 05-28-2012 at 09:00 AM.. Reason: adding sed option
# 4  
Old 05-28-2012
Thanks. both codes are working.. but can you please explain me the sed oneliner

---------- Post updated at 07:08 AM ---------- Previous update was at 07:03 AM ----------

also i have i have id with pattern like ml345 instead of user like john

i.e.

uid=1700(ml345) gid=1700(ml345) groups=4500(dba),1604(users),61343(macusr)

now the above one liner didnt work on this patten.. please help me with one liner for this pattern
# 5  
Old 05-28-2012
Did you try mine with that input? It will work.

---------- Post updated at 07:26 AM ---------- Previous update was at 07:20 AM ----------

Code:
/^uid=[0-9]*(\([^)]*\)).*groups=.*(macusr).*/

is used to select those lines that contain the "macusr" group. In this expression, the username is "marked" with a tagged regular expression using \( and \). In these selected lines, I am substituting the remembered pattern (||, that's the pattern searched for(the entire line)) with \1. \1 indicates the first "tagged" pattern which in this case is the username.
This User Gave Thanks to elixir_sinari For This Post:
# 6  
Old 05-28-2012
yes.. Thanks your code works fine.. i am still trying to understand it Smilie

Code:
/^uid=[0-9]*(\([^)]*\)).*groups=.*(macusr).*/

does that highlighted part refer to (user_name) ?? like in my case (john) , (ben) , (ml345) ?
# 7  
Old 05-28-2012
Quote:
Originally Posted by chidori
yes.. Thanks your code works fine.. i am still trying to understand it Smilie

Code:
/^uid=[0-9]*(\([^)]*\)).*groups=.*(macusr).*/

does that highlighted part refer to (user_name) ?? like in my case (john) , (ben) , (ml345) ?
Yes, I am marking the pattern within the parentheses (the ones without the escape slashes) with the TRE. 0 or more occurrences of a character which is not the closing parenthesis [)] will give only the pattern with the parentheses. This is important as a pattern such as .* would have matched any character including the closing ) [which we don't want].
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed to remove text from file

Trying to use sed to, in-place, remove specific text from a file. Since there are / in the text I use | to escape that character. Thank you :). sed -i -e 's|xxxx://www.xxx.com/xx/xx/xxx/.*/|' /home/cmccabe/list sed: -e expression #1, char 51: unterminated `s' command (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Curly braces in sed

Hi, I have below command in one of the script. Can you please let me know what does the curly braces do over here \{1,\}. The remaining part of the code atleast I am able to understand. sed -n 's/.*\-\()\{1,\}\)\-.*/\1/p' (13 Replies)
Discussion started by: tostay2003
13 Replies

3. Shell Programming and Scripting

How to remove newline, tab, spaces in curly braces.. :( Pls Help?

Hi Everyone, in the below "xyz (Exception e)" part... after the curly braces, there is a new line and immediately few tabs are present before closing curly brace. xyz (Exception e) { } note: there can be one or more newlines between the curly braces. My desired output should be ... (6 Replies)
Discussion started by: NY_777
6 Replies

4. Shell Programming and Scripting

Remove the first match only in a file using sed

I'm trying to remove the first match only of 2Z694 from an xml file and replace with a blank File Example: </Phoenix_Response_Data> <Bundle_Name_Primary>2Z694</Bundle_Name_Primary> <Bundle_Name>2Z694</Bundle_Name> </Phoenix_Response_Data> tried using: sed -e 's/'2Z694'/''/1' but this... (15 Replies)
Discussion started by: cillmor
15 Replies

5. UNIX for Advanced & Expert Users

Trying to use sed to remove last FF from file

I have a script that I am trying to apply on files that have form feeds between pages but I am trying to replace the last form feed, with carriage return so that when I convert it to a PDF file it won't generate a blank page. My script looks like this sed '$ s/\^L/\^M/' invoice.txt... (12 Replies)
Discussion started by: ziggy6
12 Replies

6. Shell Programming and Scripting

remove particular line from a file using sed

Hi i need to remove all the lines staring with 'printf("\n' from a file, example : the file tmp.txt contains printf("\n "); printf("\n good"); printf("\n "); printf("\n "); printf(""); printf( m_sprintf(for printf("\n "); i have tried with following commands but... (5 Replies)
Discussion started by: mprakasheee
5 Replies

7. Shell Programming and Scripting

sed in windows does not parse curly braces

Hi everyone: I'm stuck at this point, could you guys please give me some hints about what I am doing wrong in the following script, I'm using sed for windows: sed ^"$ {^ a^ STRINGTABLE DISCARDABLE^ BEGIN^ #define CLIENT_MODULE, "%CLIENT_MODULE%"^ #define CLIENT_ID, "%CLIENT_ID%"^... (1 Reply)
Discussion started by: edgarvm
1 Replies

8. Shell Programming and Scripting

sed over writes my original file (using sed to remove leading spaces)

Hello and thx for reading this I'm using sed to remove only the leading spaces in a file bash-280R# cat foofile some text some text some text some text some text bash-280R# bash-280R# sed 's/^ *//' foofile > foofile.use bash-280R# cat foofile.use some text some text some text... (6 Replies)
Discussion started by: laser
6 Replies

9. Shell Programming and Scripting

how to find matching braces using sed or in shell script

hi, I want to print all the lines between the matching braces. For example,the file contains like the below. asdfsdf fsdfsd WO{ w1{ ada ... (3 Replies)
Discussion started by: Boopesh
3 Replies

10. Shell Programming and Scripting

how to remove ^@ from a file using sed...or anything

i tried the following:- sed -e file 's/^@//g' > temp also tried sed -e file 's///g' > temp nothing happened....can someone please tell me wht is wrong??? also someinformation abt the character "^@"(it is ONLY ONE character and NOT TWO characters) thanx in advance.. (13 Replies)
Discussion started by: sayonm
13 Replies
Login or Register to Ask a Question