Solved

Linux Shell Scripting: Semicolon and everything after it

Posted on 2013-11-11
4
331 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
ID: 39639585
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
ID: 39639588
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
ID: 39639679
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
ID: 39639735
# 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

929 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

14 Experts available now in Live!

Get 1:1 Help Now