Solved

Linux Shell Scripting: Semicolon and everything after it

Posted on 2013-11-11
4
334 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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