Solved

Linux Shell Scripting: Semicolon and everything after it

Posted on 2013-11-11
4
330 Views
Last Modified: 2013-11-11
Using Linux shell scripting, how can I get the semicolon and everything after it?

For example:
xyz="Hello world; this is a test"
echo ${xyz##*;}

Open in new window

Should return:
; this is a test

And this should return nothing:
xyz="Hello world"
echo ${xyz##*;}

Open in new window

0
Comment
Question by:hankknight
  • 2
4 Comments
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
put single quote around the text e.g.

echo 'hi ; # {my name}'

you could escape special chars with \ e.g.

echo my name \; is tata
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
I don't believe this can be done with variable editing.
How about good old "awk"?

xyz="Hello world"
echo $xyz |awk 'BEGIN {FS=";"} {if ($0~FS) S=FS; print S $2}'
# or shorter:
echo $xyz |awk -v FS=";" '{if ($0~FS) S=FS; print S $2}'
xyz="Hello world; this is a test"
echo $xyz |awk 'BEGIN {FS=";"} {if ($0~FS) S=FS; print S $2}'
# or shorter:
echo $xyz |awk -v FS=";" '{if ($0~FS) S=FS; print S $2}'
0
 
LVL 16

Author Comment

by:hankknight
Comment Utility
woolmilkporc, your code does what I need, but how can it be integrated with this loop?
for f in *
do
x=`file -ib $f`
if test ${f##*.} = "css"; then
         m="text/css"
elif test ${f##*.} = "js"; then
         m="application/javascript"
elif test ${f##*.} = "htm"; then
         m="text/html"
elif test ${f##*.} = "html"; then
         m="text/html"
elif test ${f##*.} = "json"; then
         m="application/json"
elif test ${f##*.} = "jsonp"; then
         m="application/javascript"
elif test ${f##*.} = "xml"; then
         m="application/xml"
else 
  m=$x
fi
echo $m
done

Open in new window

In this example, the mime type determined by "x=`file -ib $f`" is:
text/x-c++; charset=us-ascii
My code posted above changes it to:
text/css
but I want it changed to
text/css; charset=us-ascii

How can I use "awk" with this code instead of echo?
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
# for f in *
for f in abc.css def.js ghi.htm jkl.html mno.json pqr.jsonp stu.xml vwx.other
do
# x=`file -ib $f`
x="text/x-c++; charset=us-ascii"
y=$(echo $x |awk -v FS=";" '{if ($0~FS) S=FS; print S $2}')
if test ${f##*.} = "css"; then
         m="text/css"
elif test ${f##*.} = "js"; then
         m="application/javascript"
elif test ${f##*.} = "htm"; then
         m="text/html"
elif test ${f##*.} = "html"; then
         m="text/html"
elif test ${f##*.} = "json"; then
         m="application/json"
elif test ${f##*.} = "jsonp"; then
         m="application/javascript"
elif test ${f##*.} = "xml"; then
         m="application/xml"
else
  m=$x
  y=""
fi
echo $m$y
done

Please note that I commented out your "for .." and "x=.." lines and replaced them with direct assignments for testing!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now