LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-03-2011, 04:26 PM   #1
pavan27
LQ Newbie
 
Registered: Nov 2007
Posts: 14

Rep: Reputation: 0
help on sed command


hello experts,
i am looking for help on shell scripting. here is my requirement.
I have a file called blackout.txt and content of the file is
Quote:
Oracle Enterprise Manager 10g Release 5 Grid Control 10.2.0.5.0.
Copyright (c) 1996, 2009 Oracle Corporation. All rights reserved.
[orclstg:3872, oracle_emd]
[LISTENER_ORA_ORCLSTG_orclstg, oracle_listener]
[orclstg, host]
[orclstg, oracle_database]
now i need to remove the first three lines and first character and last character in each line. and output should come like this(in one line with one space between each word)
LISTENER_ORA_ORCLSTG_orclstg orclstg orclstg
i was failed to get all three lines in one line. could you please help me.

i have done upto now
Quote:
blackhoney:{oracle}269% sed '1,3d' blackout_txt | awk '{print $1}' | sed 's/.\(.*\)/\1/' | sed 's/\(.*\)./\1/'
LISTENER_BMC_EURSTG_bmeurstg
bmeurstg
BMEURSTG.WORLD
please help me.
 
Old 02-03-2011, 04:42 PM   #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
You can do all the work with awk:
Code:
awk 'NR > 3 { gsub(/^.|.$/,"",$1); printf "%s ", $1 } END { printf "\n" }' blackout.txt
 
1 members found this post helpful.
Old 02-03-2011, 04:47 PM   #3
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
Good start, but a couple of things.

First combining the seds together will make it more efficient.
Next by default awk will use a newline between output records. You can either strip them out later. Setup your awk to concatenate then print the output, or tell awk to use a different record delimiter. I went with the latter.

Code:
sed -e '1,3d' -e's/.\(.*\)/\1/' -e 's/\(.*\)./\1/' blackout.txt | awk -F, -v ORS=' ' '{print $1}'
oops. looks like colucix beat me to it... and it looks like I need to expand my awk skills.

Last edited by z1p; 02-03-2011 at 04:52 PM. Reason: added final comment
 
Old 02-03-2011, 04:58 PM   #4
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
Quote:
Originally Posted by z1p View Post
oops. looks like colucix beat me to it... and it looks like I need to expand my awk skills.
Actually you introduced another way by changing the ORS value. In any case, we have to add a newline at the end - if required.
 
Old 02-03-2011, 04:59 PM   #5
pavan27
LQ Newbie
 
Registered: Nov 2007
Posts: 14

Original Poster
Rep: Reputation: 0
Angry

Thanks for Quick reply.
i am getting following error. please help me where i am doing wrong. i am bit poor in sed and awk command
Quote:
blackhoney:{oracle}: sed -e '1,3d' -e's/.\(.*\)/\1/' -e 's/\(.*\)./\1/' blackout_txt | awk -F, -v ORS=' ' '{print $1}'
awk: syntax error near line 1
awk: bailing out near line 1
 
Old 02-03-2011, 05:06 PM   #6
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
I cut and pasted that line straight into my terminal window and it works.

What OS and shell are you using?
 
Old 02-03-2011, 05:08 PM   #7
pavan27
LQ Newbie
 
Registered: Nov 2007
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by z1p View Post
I cut and pasted that line straight into my terminal window and it works.

What OS and shell are you using?
shell is :
echo $shell
/usr/bin/tcsh

OS is Solaris 10
 
Old 02-03-2011, 05:31 PM   #8
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:
awk 'NR > 3 { sub(/^./,"",$1); sub (/.$/,"",$1); printf "%s ", $1 } END { printf "\n" }' blackout.txt
This is a slightly modified version of my previous one, since I'm not sure the Solaris' awk can manage the alternative regular expression (see the pipe in the regexp of the gsub statement). Eventually you can try nawk which is more compatible with GNU awk (that is the linux version).
 
Old 02-03-2011, 05:50 PM   #9
pavan27
LQ Newbie
 
Registered: Nov 2007
Posts: 14

Original Poster
Rep: Reputation: 0
getting errors.

Quote:
blackhoney:{oracle}317% awk 'NR > 3 { sub(/^./,"",$1); sub (/.$/,"",$1); printf "%s ", $1 } END { printf "\n" }' blackout_txt
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1
i have tired different way, but i am not getting what i am looking for.

Quote:
{oracle}316% sed -e '1,3d' -e's/.\(.*\)/\1/' -e 's/\(.*\)./\1/' -e 's/^ *[^ ]* //' blackout_txt
oracle_listener
host
oracle_database
 
Old 02-03-2011, 06:13 PM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Your version of sed may not have the -e option. Try using a semicolon to separate sed commands.
sed '1,3d;s/.\(.*\).$/\1/' blackout_txt

Also use the 'od' or hexdump commands to print out a character by character output of a couple lines of the input file. A non-printable character or different line endings may be tripping you up.

sed -n '4p' blackout_txt | od -c
 
Old 02-03-2011, 06:51 PM   #11
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Here's another sed:
Code:
sed -nr '4,$ {s/\[([^,]+).*$/\1 /;H};$ {g;s/\n//gp}' blackout_txt
 
1 members found this post helpful.
Old 02-03-2011, 07:03 PM   #12
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
Its the assignment using the -v that awk doesn't like. try:
Code:
sed -e '1,3d' -e's/.\(.*\)/\1/' -e 's/\(.*\)./\1/' blackout_txt | awk -F, '{ORS=" "} {print $1} END {print "\n"} '
 
Old 02-03-2011, 08:34 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Assuming the file is comma separated for all the data we are looking at, getting rid of character at the end of the line is a waste of time (with awk) as
it is never called. So just:
Code:
awk -F"[][,]" 'NR > 3{printf $2" "}END{print ""}' file
 
Old 02-03-2011, 09:07 PM   #14
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Quote:
Originally Posted by crts View Post
Here's another sed:
Code:
sed -nr '4,$ {s/\[([^,]+).*$/\1 /;H};$ {g;s/\n//gp}' blackout_txt
from memory, don't think Solaris's sed has the -r option. i may be wrong
 
Old 02-03-2011, 09:08 PM   #15
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Quote:
Originally Posted by grail View Post
Assuming the file is comma separated for all the data we are looking at, getting rid of character at the end of the line is a waste of time (with awk) as
it is never called. So just:
Code:
awk -F"[][,]" 'NR > 3{printf $2" "}END{print ""}' file
does Solaris's awk or nawk allow multiple field delimiters? I don't have nawk at hand to check.
 
  


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
sed command help tekmann33 Linux - Newbie 1 10-20-2008 04:06 PM
sed command help prashant.kumar Linux - General 6 10-15-2007 05:48 PM
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 > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:15 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
Open Source Consulting | Domain Registration