Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Trying to Uppercase Text up to a Point
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
wswartzendruber
Veteran
Veteran


Joined: 23 Mar 2004
Posts: 1261
Location: Idaho, USA

PostPosted: Sat Oct 03, 2009 10:36 pm    Post subject: Trying to Uppercase Text up to a Point Reply with quote

I'm guessing Perl's the best tool, but I need to turn this...

Quote:
artist=ZZ Top
title=Gimme All Your Lovin'
album=Greatest Hits
Date=1992
Tracknumber=01
genre=Rock

...into this...
Quote:
ARTIST=ZZ Top
TITLE=Gimme All Your Lovin'
ALBUM=Greatest Hits
DATE=1992
TRACKNUMBER=01
GENRE=Rock

_________________
Git has obsoleted SVN.
10mm Auto has obsoleted 45 ACP.
Back to top
View user's profile Send private message
yabbadabbadont
Advocate
Advocate


Joined: 14 Mar 2003
Posts: 4791
Location: 2 exits past crazy

PostPosted: Sat Oct 03, 2009 10:39 pm    Post subject: Reply with quote

A shell script using cut (with = as the delimiter) and tr (to force uppercase) shouldn't be too hard to knock out.
Back to top
View user's profile Send private message
wswartzendruber
Veteran
Veteran


Joined: 23 Mar 2004
Posts: 1261
Location: Idaho, USA

PostPosted: Sat Oct 03, 2009 10:52 pm    Post subject: Reply with quote

yabbadabbadont wrote:
A shell script using cut (with = as the delimiter) and tr (to force uppercase) shouldn't be too hard to knock out.

That works except that it splits the tag names and tag values into two separate outputs. I don't know how to append two lines into one.

EDIT: Wait a minute, I think join is what I'm looking for.
_________________
Git has obsoleted SVN.
10mm Auto has obsoleted 45 ACP.
Back to top
View user's profile Send private message
yabbadabbadont
Advocate
Advocate


Joined: 14 Mar 2003
Posts: 4791
Location: 2 exits past crazy

PostPosted: Sat Oct 03, 2009 10:55 pm    Post subject: Reply with quote

I was bored, so while you were replying, I was coding... :D

Code:
/home/daffy/temp $ cat DataFile
artist=ZZ Top
title=Gimme All Your Lovin'
album=Greatest Hits
Date=1992
Tracknumber=01
genre=Rock
/home/daffy/temp $ . ./go.sh
ARTIST=ZZ Top
TITLE=Gimme All Your Lovin'
ALBUM=Greatest Hits
DATE=1992
TRACKNUMBER=01
GENRE=Rock
/home/daffy/temp $ cat go.sh
while read TheLine
do
        TheTag=$(echo "$TheLine" | cut -d = -f1)

        TheRest=$(echo "$TheLine" | cut -d = -f1 --complement)

        TheTag=$(echo "$TheTag" | tr [:lower:] [:upper:])

        echo "$TheTag=$TheRest"
done < DataFile

You just need to redirect that last echo statement so that it appends to a new file.

Code:
echo "$TheTag=$TheRest" >> NewDataFile
Back to top
View user's profile Send private message
wswartzendruber
Veteran
Veteran


Joined: 23 Mar 2004
Posts: 1261
Location: Idaho, USA

PostPosted: Sat Oct 03, 2009 11:37 pm    Post subject: Reply with quote

I came up with my own solution. Probably not as elegant:

Code:
#!/bin/sh
#
# This little guy's going to fix metadata within FLAC files.
#
#   $1 - Name of the FLAC file we're working on.

# First, dump the file's metadata somewhere.
metaflac --export-tags-to="$1.tags" "$1"

# Next, separate the tag names from their values, uppercasing the names.
cat "$1.tags" | cut --fields=1 --delimiter== | tr '[:lower:]' '[:upper:]' > "$1.tags.names"
cat "$1.tags" | cut --fields=2 --delimiter== > "$1.tags.values"

# Paste the two together.
paste --delimiters== "$1.tags.names" "$1.tags.values" > "$1.tags.almost"

# Sort the tags file.
cat "$1.tags.almost" | sort > "$1.tags"

# Remerge the sorted tags back into the file.
metaflac --remove-all-tags "$1"
metaflac --import-tags-from="$1.tags" "$1"
flac --best --force "$1"

# Clean up the garbage.
rm -rf "$1.tags" "$1.tags.names" "$1.tags.values" "$1.tags.almost"

_________________
Git has obsoleted SVN.
10mm Auto has obsoleted 45 ACP.
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10589
Location: Somewhere over Atlanta, Georgia

PostPosted: Sun Oct 04, 2009 1:39 am    Post subject: Reply with quote

Code:
awk -F '=' '{ print toupper($1) substr($0,length($1)+1) }' oldfile >newfile
:)
- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
wswartzendruber
Veteran
Veteran


Joined: 23 Mar 2004
Posts: 1261
Location: Idaho, USA

PostPosted: Sun Oct 04, 2009 1:42 am    Post subject: Reply with quote

john_r_graham wrote:
Code:
awk -F '=' '{ print toupper($1) substr($0,length($1)+1) }' oldfile >newfile
:)
- John

Huh?! One line?

* Goes off to find rope and a tree. *
_________________
Git has obsoleted SVN.
10mm Auto has obsoleted 45 ACP.
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10589
Location: Somewhere over Atlanta, Georgia

PostPosted: Sun Oct 04, 2009 3:36 pm    Post subject: Reply with quote

Yeah. I think that AWK code results in the most readable and understandable implementation of the standard *nix text processing languages. If you can guarantee that there's only one equals sign on each line, then the AWK gets even more simple and readable:
Code:
awk -F '=' '{ print toupper($1) "=" $2 }' oldfile >newfile

sed also results in a very short one liner based on regular expressions:
Code:
sed -i -r 's/^([^=]*=)/\U\1\E/' filename
However, it's pretty abstruse. At a glance you (or, at least, I) just can't tell what that does.

You're right that Perl can do the job but, as the best mechanism in Perl is also a regex-based one, it ends up looking almost identical to the sed implementation.

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum