Solved

Linux Shell Scripting: Semicolon and everything after it

Posted on 2013-11-11
4
336 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

615 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