LinuxQuestions.org
Latest LQ Deal: Linux Power User 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 06-24-2010, 01:11 AM   #1
subashinik
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Rep: Reputation: 0
Sed with AND operation


Hello All,

Need your help on writing a sed oneliner

Let'say if I have a Txt file with following lines

Set pin1 = 0
set pin2 = 1
set pin3 = 0 ;

I want to add ";" at the end for those lines Which starts with set infront and doesn't end with ; , What would be the correpsonding sed one liner command for that.??

My Final op should look .like
Set pin1 = 0 ;
set pin2 = 1 ;
set pin3 = 0 ;

( Basically I don't know How to add AND operation in the Sed command to combine the 2 conditions of lines starting with set and doesn't end with ; ..? Thanks for your help !

Last edited by subashinik; 06-24-2010 at 01:13 AM.
 
Old 06-24-2010, 01:20 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

Not so much a sed as a regex question...

Code:
sed -r 's/^([Ss]et.*)[^;]$/\1 ;/' sed_and
Set pin1 = 0 ;
set pin2 = 1 ;
set pin3 = 0 ;
If you're unsure how the regex works - please ask.



Cheers,
Tink

Last edited by Tinkster; 06-24-2010 at 01:27 AM.
 
Old 06-24-2010, 02:51 AM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,298

Rep: Reputation: 102Reputation: 102
Lightbulb

I have another solution, although not the best one


Suppose you have a file abc.txt
Code:
[linux1@HMLINUX1 abc]$ cat abc.txt
Set pin1 = 0
pe pin1 = 0
set pin2 = 1
ge pin1 = 0
set pin3 = 0
Then, this code should work

Code:
cat abc.txt  | grep -ni set | awk -F ":" '{print $1}' > tmp
for i in `cat tmp`; do sed -i $i's/.*/& \;/' abc.txt; done;
Now ,
[linux1@HMLINUX1 abc]$ cat abc.txt
Code:
Set pin1 = 0 ;
pe pin1 = 0
set pin2 = 1 ;
ge pin1 = 0
set pin3 = 0 ;

Last edited by vikas027; 06-24-2010 at 02:52 AM.
 
Old 06-24-2010, 03:10 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
hmmm ... one would think if you are going to use awk just use it instead of going the long way:
Code:
awk '/[Ss]et.*[^;]$/{$0=$0";"}1' file
 
1 members found this post helpful.
Old 06-24-2010, 03:15 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,839

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
Let's hope there are no trailing whitespaces on any of those lines ...
 
Old 06-24-2010, 03:23 AM   #6
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,298

Rep: Reputation: 102Reputation: 102
Question

Quote:
Originally Posted by grail View Post
hmmm ... one would think if you are going to use awk just use it instead of going the long way:
Code:
awk '/[Ss]et.*[^;]$/{$0=$0";"}1' file
Nice Grail, but what are is this for [^;]$, it is working without this also.
And what does 1 represents at last.


Code:
 
[linux1@HMLINUX1 abc]$ cat abc.txt
Set pin1 = 0
pe pin1 = 0
set pin2 = 1
ge pin1 = 0
set pin3 = 0
[linux1@HMLINUX1 abc]$
[linux1@HMLINUX1 abc]$ awk '/[Ss]et.*/{$0=$0";"}1' abc.txt
Set pin1 = 0;
pe pin1 = 0
set pin2 = 1;
ge pin1 = 0
set pin3 = 0;
[linux1@HMLINUX1 abc]$
 
Old 06-24-2010, 03:29 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Quote:
Nice Grail, but what are is this for [^;]$, it is working without this also.
If you read the OPs original post he shows that some lines may already have semicolon at the end, therefore [^;]$ is required to test that one does not exist.
Quote:
And what does 1 represents at last.
awk's default action is to print on any true value which is any non-zero value, hence 1
 
Old 06-24-2010, 04:15 PM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
is that vhdl ?;
what micro-controller are you working on ?
 
Old 06-24-2010, 04:42 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by vikas027 View Post
I have another solution, although not the best one


Suppose you have a file abc.txt
Code:
[linux1@HMLINUX1 abc]$ cat abc.txt
Set pin1 = 0
pe pin1 = 0
set pin2 = 1
ge pin1 = 0
set pin3 = 0
Then, this code should work

Code:
cat abc.txt  | grep -ni set | awk -F ":" '{print $1}' > tmp
for i in `cat tmp`; do sed -i $i's/.*/& \;/' abc.txt; done;
Now ,
[linux1@HMLINUX1 abc]$ cat abc.txt
Code:
Set pin1 = 0 ;
pe pin1 = 0
set pin2 = 1 ;
ge pin1 = 0
set pin3 = 0 ;
"Not the best" is a slight understatement ;}

You use cat & grep unnecessarily, and then a loop for no
good reason on top of that.



Cheers,
Tink
 
Old 06-26-2010, 08:27 PM   #10
subashinik
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Smile Thanks for the reply and suggestions :)

Hello every one.

Thanks to all for you reply.Really helped me lot! This is my first thread in this site And I am glad that I received so many replies

Again Thanks for your time !

Subashinik
 
Old 06-27-2010, 11:21 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Here's your explanation:

Code:
sed -r 's/^([Ss]et.*)[^;]$/\1 ;/'
Piece by piece:
Code:
-r
put send into enhanced regex mode
Code:
s/
start search & replace operation
Code:
^
Beginning of line
Code:
(
Begin a "back reference hold"
Code:
[Ss]et.*)
a word set starting with S or s, followed
by anything, ")" closes the "(" to remember
the match.
Code:
[^;]$
Carry the match on from the remembered part if
the last character before the line end is not a
; ....
Code:
/\1 ;/
replace the matched string with what
was remembered, a space and a semicolon
 
Old 06-29-2010, 01:24 AM   #12
subashinik
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Sed reg

Hello Tink,

Thanks a lot for your explanations.

But this command sed -r 's/^([Ss]et.*)[^;]$/\1 ;/' <filename> adds ";" at the end of those lines which already has ";" at the end.

Do we need to add up ! at [^;]$ ..? I tried out but it doesn't work ..?

Sorry I am not good in Sed, just trying to pick up things

Thanks
Suba
 
Old 06-29-2010, 03:34 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Hi Suba

You sure you copied/typed it correctly? What you are saying does not happen to me, although I do have a separate issue with the current sed command.

Quote:
Do we need to add up ! at [^;]$ ..? I tried out but it doesn't work ..?
If you re-read Tink's explanation you will see that "[^;]" already means not;

The issue i have when running the sed is that it removes the last character on the lines without a semicolon, ie:
Code:
#inputfile
Set pin1 = 0
set pin1 = 0
set pin1 = 0;

#output after current command
Set pin1 =  ;
set pin1 =  ;
set pin1 = 0;
As you can see the first two lines are now missing there zeroes. The following seems to remedy this:
Code:
sed -r 's/^([Ss]et.*[^;]$)/\1 ;/' <filename>
 
Old 06-29-2010, 04:55 AM   #14
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,839

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
Quote:
Originally Posted by subashinik View Post
But this command sed -r 's/^([Ss]et.*)[^;]$/\1 ;/' <filename> adds ";" at the end of those lines which already has ";" at the end.
That will happen if the semicolon is not actually the last character - see my previous post above.
 
Old 06-30-2010, 05:13 AM   #15
subashinik
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks SYg00 & grail ,

Got ur point !.
Yes , Shouldn't have any trailing whitespaces on any of those lines

Thanks to all for your time !

Btw, I am working on ASIC CHIP level verification ! And trying to learn more on scripting/Programming !

With regards
Suba
 
  


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
operation of "tac" in sed dina3e Programming 7 08-24-2008 09:45 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-04-2005 12:00 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM


All times are GMT -5. The time now is 08:38 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration