LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-26-2008, 05:33 PM   #1
n0gnus
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Rep: Reputation: 0
scripting help with sed


Hi, I am not good with sed, and I'm having trouble trying to print only the characters between matching expressions. For example, if I have a file with many lines of this general format:

xxx.xxx.xxx.xxx User [joeschmoe] Group [somegroup] connected, Session Type

I only want to print the characters after User and before Group. Basically I want to get a user listing of all entries in the file. How would I go about this?

From what I've read this is supposed to work, but it fails terribly:

sed -n '/User/,/Group/p' users > temp

What would be even better would be to get the info after User and before the second bracket ']'.

Any help is greatly appreciated.
 
Old 09-26-2008, 06:30 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
welcome to LQ!!

The stucture you used is called an "addressing range". It says to print lines, beginning with one containing "User", and ending with one containing "Group".

What you need is something like this:

sed -n 's/.*User\(.*\)Group.*/\1/p'

Running to an appointment---can't do more now.

Excellent SED tutorial here:
http://www.grymoire.com/Unix/Sed.html
 
Old 09-26-2008, 06:33 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I would use:
Code:
sed -e 's/.*User.*\[\(.*\)\].*Group.*/\1/' users > temp
Basically remove everything, just keep the value between \( and \)

Another alternative with cut:
Code:
cut -d[ -f2 users | cut -d] -f1 > temp

Last edited by keefaz; 09-26-2008 at 06:43 PM.
 
Old 09-26-2008, 07:27 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have Python,
Code:
for lines in open("file"):
    lines=lines.split() #get them into array
    #get the index where "User" is and add 1.
    # this will be position of the username
    print lines[lines.index("User") + 1]
 
Old 09-30-2008, 11:01 AM   #5
n0gnus
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for the help and the welcome! I really appreciate it; I'll attempt these today.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with shell scripting and sed Uzelth Programming 5 04-13-2008 06:21 PM
shell scripting problem, sed command kamkos Linux - General 5 03-31-2007 09:39 PM
Forum on scripting, sed, awk, etc.?? pixellany Programming 3 12-01-2005 01:16 PM
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 02:55 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration