LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-28-2014, 12:07 AM   #1
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Rep: Reputation: Disabled
sed command


Hi Linux Guru's

I've been trying to understand sed command but I'm still stucked on my problem. Just want to ask does sed command has a capabilty to print the columns I desire?

sample

sample.txt
SERVER IP Domain
lamp_server 192.168.0.3 east_data_center
file_server 192.168.0.5 west_data_center
print_server 192.168.0.6 north_data_center
aut_server 192.168.0.2 south_data_center


I'm having some hard rime to print SERVERS, IP and Domain in a separate result.

thanks in advance
 
Old 05-28-2014, 01:01 AM   #2
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,395
Blog Entries: 2

Rep: Reputation: Disabled
I'm no Guru but if you save the output into a file named "rip.me"
Code:
sed 's/\([a-z]_[a-z]*\).*/\1/' rip.me
Will give you the first column. Tinker with it from there,
I imagine somebody will be along shortly to show us a really clean way to do this.
By the way, that's much easier to do with awk than sed

Last edited by dijetlo; 05-28-2014 at 01:08 AM.
 
Old 05-28-2014, 04:19 AM   #3
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by dijetlo View Post
I'm no Guru but if you save the output into a file named "rip.me"
Code:
sed 's/\([a-z]_[a-z]*\).*/\1/' rip.me
Will give you the first column. Tinker with it from there,
I imagine somebody will be along shortly to show us a really clean way to do this.
By the way, that's much easier to do with awk than sed
this is good! thanks dijetlo!
 
Old 05-28-2014, 04:29 AM   #4
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,395
Blog Entries: 2

Rep: Reputation: Disabled
If we're done, do me a favor and close up the thread.
 
Old 05-28-2014, 09:36 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Ultimately awk is the better tool when looking at well defined columns, eg:
Code:
awk '{print $1}' file
This will give all the server names.

As sed alternatives:
Code:
# server
sed 's/ .*//' file
# domain
sed -r 's/.* (.)/\1/' file
# ip (although the header line will be missed
sed 's/[^0-9.]*//g' file
 
1 members found this post helpful.
Old 05-28-2014, 10:36 AM   #6
kbnuts
Member
 
Registered: Apr 2014
Posts: 45

Rep: Reputation: Disabled
I was going to say, sed's probably not your man for that. You could use awk as above but cut is lighter so...
Code:
cut -d' ' -f1
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Sed substitution gives error "sed: command garbled" gsai0205 Linux - Newbie 3 09-13-2013 10:01 AM
[SOLVED] sed gives :sed: -e expression #1, char 1: unknown command: `'' samasat Linux - Newbie 10 06-09-2012 06:31 PM
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 01:27 PM
Sed command - command garbled errror gauavmahesh Linux - Newbie 3 03-30-2012 11:04 PM
Modifying text file with "one command line" SED command... daleo Linux - Newbie 3 01-13-2012 06:32 AM


All times are GMT -5. The time now is 10:44 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration