LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2012, 02:50 AM   #1
shirt
LQ Newbie
 
Registered: Mar 2012
Posts: 6

Rep: Reputation: Disabled
search and truncate help


I need somehelp on the sed command. the file contains the following information.
((type = file)
(method = DIRECTORY = /usr/local/bin))

I tried with the following command but the output is different then running the multiple commands individually.
sed -ne "/DIRECTORY/p" -ne "s/^[^=]\+=//p" tmp.txt
Output:
file)
(method = DIRECTORY = /usr/local/bin))
DIRECTORY = /usr/local/bin)))

but i am trying for the result "/usr/local/bin". Another information to note here is that the /usr/local/bin path will differ each time, the output should be path in between = and ). Any help or direction for the result would be helpful.

Thanks for your help in advance.

Last edited by shirt; 03-16-2012 at 03:05 AM.
 
Old 03-16-2012, 03:07 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
What about this?
Code:
sed -rn '/DIRECTORY/s:.*= ([^)]+).*:\1:p' tmp.txt
 
Old 03-16-2012, 03:08 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

It is not entirely clear if you want the output to be just that one line (DIRECTORY = /usr/local/bin) or if the rest of the file needs to be shown as well (unchanged).

Only show specific, edited, line:
Code:
sed -n '/DIRECTORY/s/.*= \(.*\)\/.*/DIRECTORY = \1/p' tmp.txt

# output would be
DIRECTORY = /usr/local/bin
Show all, but change specific line:
Code:
sed '/DIRECTORY/s/.*= \(.*\)\/.*/DIRECTORY = \1/' tmp.txt

# output would be
((hello
(type = file)
DIRECTORY = /usr/local/bin
BTW: Please put your script/data inside [code] ... [/code] tags it preserves all spacing. If you don't know how: LQ - BB Code List.

Hope this helps.

EDIT: If the DIRECTORY = isn't wanted:
Code:
sed -n '/DIRECTORY/s/.*= \(.*\)\/.*/\1/p' tmp.txt

# output would be
/usr/local/bin

Last edited by druuna; 03-16-2012 at 03:21 AM. Reason: Added 3rd option.
 
Old 03-16-2012, 03:15 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Try this
Code:
$ sed -rn 's/.*DIRECTORY = ([^)]*)\).*/\1/p' infile.txt
/usr/local/bin/temp.txt
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Modifying text file with "one command line" SED command... daleo Linux - Newbie 3 01-13-2012 05:32 AM
sed command help viveksnv Programming 3 02-26-2008 07:40 AM
sed command ancys Programming 2 08-05-2006 10:50 AM
sed command pazvant Linux - Software 2 05-09-2004 12:58 PM
sed command kwigibo Linux - General 3 04-21-2002 04:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:29 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