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 11-21-2018, 12:55 PM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Rep: Reputation: Disabled
Angry bash: Can't split string at colon (pure bash)


Here's another basic thing I've forgotten how to do: split a string and return the first part at a delimiter (in this case a colon :).

My code (scriptname: getfilenames.sh)
Code:
tg="$1"

while IFS=":" read -r y
do
echo "${y%%*:}"
done < "$tg"
echo "${y%%*:}"
done < "$tg"
stdout returns, on an example file (see attachment):
Code:
bash getfilenames.sh theunders
038_1000.jpg:Underwater Wonders
167_1000.jpg:Underwater Wonders
501_1000.jpg:Underwater Wonders
732_1000.jpg:Underwater Wonders
her-job-intrvu.jpg:Underwater Wonders
ihre-tag-im-ozean.jpg:Underwater Wonders
suf-261-919-015.jpg:Underwater Wonders
suf-699-136-133.jpg:Underwater Wonders
suf-791-432-188.jpg:Underwater Wonders
suf-936-891-544.jpg:Underwater Wonders
suf-996-295-145.jpg:Underwater Wonders
What I want it to return is
  • 038_1000.jpg
    167_1000.jpg
    501_1000.jpg
    732_1000.jpg
    her-job-intrvu.jpg
    ihre-tag-im-ozean.jpg
    suf-261-919-015.jpg
    suf-699-136-133.jpg
    suf-791-432-188.jpg
    suf-936-891-544.jpg
    suf-996-295-145.jpg

How do I get there, in pure BASH. I want to get away from using cut, but I'm on the verge of resorting to it if I can't find an "internal" work-around.

Carver.
Attached Files
File Type: txt theunders.txt (402 Bytes, 26 views)

Last edited by L_Carver; 11-21-2018 at 01:00 PM. Reason: Forgot first line of the script
 
Old 11-21-2018, 01:12 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by L_Carver View Post
Here's another basic thing I've forgotten how to do: split a string and return the first part at a delimiter (in this case a colon :).

My code (scriptname: getfilenames.sh)
Code:
tg="$1"

while IFS=":" read -r y
do
echo "${y%%*:}"
done < "$tg"
echo "${y%%*:}"
done < "$tg"
your * (star) is on the wrong side.
Code:
$ go="732_1000.jpg:Underwater Wonders"
$ g=${go%:*}
$ echo $g
732_1000.jpg

$ o=${go#*:}
$ echo $o
Underwater Wonders

Last edited by BW-userx; 11-21-2018 at 01:13 PM.
 
Old 11-21-2018, 04:11 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
You need to specify both variables in your read statement.

Code:
#!/bin/bash

while IFS=":" read -r y z
do
echo $y
done < "theunders.txt"
There are many ways to manipulate strings.

Last edited by michaelk; 11-21-2018 at 04:15 PM.
 
1 members found this post helpful.
Old 12-02-2018, 12:09 PM   #4
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Original Poster
Rep: Reputation: Disabled
@michaelk:

That looks like the best way to do it. Besides, it brings back one of my favourite things to do in bash: declare field separators.

Thanks a bunch.

Carver
[Thread closed.]
 
  


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] how to insert colon in a string with bash mia_tech Linux - General 6 02-13-2023 07:58 PM
Pure-authd not passing variables to pure-ftpd welshdemon Linux - Software 1 02-11-2011 04:47 PM
[perl]How to treat string like "a b" as a single string when split? john.daker Programming 21 06-01-2009 05:57 PM
pure-ftpd-mysql activates pure-ftpd zvikamer Linux - Software 2 03-01-2008 12:11 PM
awk escape character for colon in string quadmore Programming 2 02-27-2007 04:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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