LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-10-2023, 10:30 PM   #1
centguy
Member
 
Registered: Feb 2008
Posts: 627
Blog Entries: 1

Rep: Reputation: 48
what is @@ in sed?


This works:
Quote:
ls -1 *py | sed s@.py@@g
This lists out all files with .py extension.
Then it pulls out stem if the file name is stem.py.

But how to interpret this cryptic @@?

It seems @ is holding the stem, but why @@? Wouldn't it
give stemstem?

Last edited by centguy; 04-11-2023 at 12:34 AM.
 
Old 04-10-2023, 10:45 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
The basic syntax is s/// with the slash / being a delimiter between the substitution pattern being sought and the replacement pattern. However, you can use anything. These are all the same:

Code:
ls -1 *py | sed s/.py//
ls -1 *py | sed s|.py||
ls -1 *py | sed s#.py##
ls -1 *py | sed 's!.py!!' 
ls -1 *py | sed sa.pyaa 
ls -1 *py | sed sb.pybb 
# etc
You can do similarly with the regular pattern search // too.

Code:
ls -1 | sed -n -e '/py$/p'
ls -1 | sed -n -e '\|py$|p'
ls -1 | sed -n -e '\#py$#p'
ls -1 | sed -n -e '\!py$!p'
# etc
See "man sed" and scroll down to \cregexpc unde "Addresses"

Last edited by Turbocapitalist; 04-10-2023 at 10:47 PM.
 
Old 04-10-2023, 10:48 PM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
In this case "@" is being used as the delimiter for sed which is doing a search "s" for the string ".py" (sring between the 1st and 2nd delimiters) and replacing globally "g" with nothing (string between the 2nd and 3rd delimiters).

Evo2.
 
Old 04-10-2023, 10:51 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
@ is used as the separator instead of /

Quote:
ls -1 *py | sed s/.py//g
Does this make it easier to understand? s substitute, global replace. I assume the @ is used since the command might contain paths with /s

Too late...
 
Old 04-11-2023, 12:33 AM   #5
centguy
Member
 
Registered: Feb 2008
Posts: 627

Original Poster
Blog Entries: 1

Rep: Reputation: 48
ok. now I conclude the "g" (for global)
is pretty useless 99.999% of the time in this case (nobody will name a file stem.py.py or something like it)

To summarize:

ls -1 *py | sed s@.py@@

means look for .py and replace it with nothing, essentially removing .py from the string.

Now I learned that one could use a delimiter other than / (what I used to know!)

Thanks for the prompt replies and the fruitful comments!
 
Old 04-11-2023, 12:54 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Just for curiosity: sed will remove the first .py, not the last one.
Code:
echo filename.py.a.py | sed s@.py@@
 
Old 04-12-2023, 12:44 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
See this https://en.wikipedia.org/wiki/Leanin...hpick_syndrome
 
Old 04-17-2023, 09:01 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,793

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Perhaps you can consolidate to one RE
Code:
ls | sed -n 's/[.]py$//p'
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you do sed 's/anytext/'/g' or sed 's/anytext/\'/g' pppaaarrrkkk Programming 4 02-29-2008 12:15 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 - General

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