LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-19-2023, 12:46 PM   #1
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,316

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Script


Code:
for i in *.xxx;
  do name=`echo "$i" | cut -d'.' -f1
This script segment preserves everything up to the first '.' and cuts the rest. It fails when there's more than 1 dot in the filename.

That, however is the wrong way around for me, as some files have numerous dots. How would I cut from the other end? For example, how would I cut the '.xxx' fromn a file like 'long.silly.file.name.xxx'

Input: long.silly.file.name.xxx
Desired output: long.silly.file.name

TIA
 
Old 02-19-2023, 01:06 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
At first, this title is meaningless, would be nice to use a meaningful one. See man bash and parameter expansion, especially ${var##.*} and ${var%*} and friends https://www.gnu.org/software/bash/ma...eter-Expansion
 
Old 02-19-2023, 01:06 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,720

Rep: Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914
What about string manipulation?

Code:
name=${i%.xxx}
Not fast enough...
 
Old 02-19-2023, 01:06 PM   #4
lvm_
Member
 
Registered: Jul 2020
Posts: 935

Rep: Reputation: 338Reputation: 338Reputation: 338Reputation: 338
gawk 'BEGIN{FS=".";OFS=FS}{NF--;print}'
 
Old 02-19-2023, 01:07 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,799

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
cut is an external command, and has no means to address fields from the end.
Two reasons for a variable modifier:
Code:
name=${i%.*}
% deletes from the end, minimal expansion of wildcards.

%% does the same but greedy expansion of wildcards i.e. would delete till the leftmost dot.

Last edited by MadeInGermany; 02-19-2023 at 01:08 PM.
 
2 members found this post helpful.
Old 02-19-2023, 01:47 PM   #6
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,640

Rep: Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697
I second MadeInGermany. There is no reason to work in a shell unless you plan to use the power of the shell. If your shell is BASH, you have some powerful built-in string processing, so use it.
(Same can be said of KSH, ZSH, and some of the other shells but they may offer different options and implemented differently.)
 
Old 02-19-2023, 09:07 PM   #7
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
If you know the extension, as seems you want to strip only that part, then you could also use the basename command to get everything but the extension part.

Examples in the manpage
Code:
       basename include/stdio.h .h
              -> "stdio"

       basename -s .h include/stdio.h
              -> "stdio"
 
Old 02-19-2023, 10:50 PM   #8
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by business_kid View Post
Code:
for i in *.xxx;
  do name=`echo "$i" | cut -d'.' -f1
This script segment preserves everything up to the first '.' and cuts the rest. It fails when there's more than 1 dot in the filename.

That, however is the wrong way around for me, as some files have numerous dots. How would I cut from the other end? For example, how would I cut the '.xxx' fromn a file like 'long.silly.file.name.xxx'

Input: long.silly.file.name.xxx
Desired output: long.silly.file.name

TIA
Play around with
Code:
$ echo "aa.bb.cc.dd.xxx" | sed 's/.xxx$//'
I think it'll help you.
 
Old 02-20-2023, 03:25 AM   #9
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,153

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Agree with MadeInGermany, you can do like this:

Quote:
a='long.silly.file.name.xxx'
echo "${a%.*}"
"." <-- remove suffix starting this char
 
Old 02-20-2023, 04:27 AM   #10
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,590
Blog Entries: 19

Rep: Reputation: 4455Reputation: 4455Reputation: 4455Reputation: 4455Reputation: 4455Reputation: 4455Reputation: 4455Reputation: 4455Reputation: 4455Reputation: 4455Reputation: 4455
Why not use the rev command?
 
Old 02-20-2023, 05:55 AM   #11
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,316

Original Poster
Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Wow! First time I looked at this there was 10 replies. Thanks guys. And sorry, there was a word missing out of the subject.

Thanks to all, and kudos to MadeInGermany who was first in with a very elegant solution which has just been implemented and has completed it's first working run.

Along with all the half baked & useless tripe they had modules on in my hardware degree, you'd think they'd have time for an essential skill like scripting.
 
Old 02-20-2023, 06:01 AM   #12
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,130

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
It should be noted (by the OP) that bash (note shell specific) features are continually added. They likely didn't exist when you were a squeaky voiced student.

Some call it bloat ... :shrug:
 
Old 02-20-2023, 12:15 PM   #13
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,640

Rep: Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697Reputation: 2697
Quote:
Originally Posted by syg00 View Post
It should be noted (by the OP) that bash (note shell specific) features are continually added. They likely didn't exist when you were a squeaky voiced student.

Some call it bloat ... :shrug:
When I was a student BASH had not been written yet. Those man pages are worth gold, when they are well written and current. Search engines and the people who have put examples of new features and using them properly on the internet: priceless!
 
Old 02-20-2023, 02:10 PM   #14
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,316

Original Poster
Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Quote:
Originally Posted by syg00 View Post
It should be noted (by the OP) that bash (note shell specific) features are continually added. They likely didn't exist when you were a squeaky voiced student.

Some call it bloat ... :shrug:
No squeaky voice as a student here.
I didn't get my degree until my kids were grown - I was turning into an old fart
Every job offered for me after I closed my business needed a degree, so I decided to get one. What with my wife getting sick, and getting a year chopped out of my degree midstream(!) it proved to be a forgettable experience. In fact, the lecturers learned nearly as much from me as I did from them.

I graduated with a degree more suited to the last millenium in 2014, and had a stroke the next year .
 
  


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
[SOLVED] Script to trigger second script--second script gets arguments of first script keif Linux - General 10 08-12-2021 01:25 AM
can't run a script script from icon in konqueror scottsteibel Linux - Software 1 08-02-2003 07:59 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM
E-Mail notification to users via SMS (gateway script ok, but notification script?!?) Riku2015 Linux - Networking 10 03-08-2002 10:16 AM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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