LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 06-24-2010, 12: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 12:13 AM.
 
Old 06-24-2010, 12:20 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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 12:27 AM.
 
Old 06-24-2010, 01:51 AM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
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 01:52 AM.
 
Old 06-24-2010, 02:10 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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, 02:15 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

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

Rep: Reputation: 107Reputation: 107
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, 02:29 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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, 03:15 PM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

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

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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, 07: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, 10:21 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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, 12: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, 02:34 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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, 03:55 AM   #14
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
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, 04: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



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 08:45 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
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 - Newbie

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