Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Linux Shell Scripting: Semicolon and everything after it

Posted on 2013-11-11
4
Medium Priority
?
338 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 2000 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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

722 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