LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2010, 09:01 PM   #1
spartiati
LQ Newbie
 
Registered: Jan 2010
Posts: 3

Rep: Reputation: 0
bash script trying to remove a leading ' and a tailing '


I have a script that calls for a file description on a core file. I then pull the name of the process that caused the core file.
unfortunately, the process name is pulled with a leading ' amd a tailing'. I would like to remove the leading char and the last char.

code:
VAR=`file /tmp/core.1432 | awk '{ print $14}'`
echo "Process Name: $VAR"

output:
Process Name: 'ProcessName'

Results I need:
Process Name: ProcessName

I tried sed, but it seems to not like using the special char '.

I would appreciate any help on this.
 
Old 03-28-2010, 10:26 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
VAR=`file /tmp/core.1432 | awk '{ gsub(/\047/,"",$14); print $14}'`

Last edited by grail; 03-28-2010 at 10:35 PM.
 
1 members found this post helpful.
Old 03-28-2010, 11:08 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Or it can be done entirely in the shell (which is a lot faster than using external commands) using Shell Parameter Expansion and Arithmetic Expansion
Code:
VAR=${VAR:1:$((${#VAR}-1))}
Peeling that onion from the inside out:
  1. ${#VAR} is the length of $VAR.
  2. $(( )) gives a string which is the value of the arithmetic expression inside it, in this case the length of $VAR less one.
  3. ${VAR:S:L} gives the substring of $VAR starting at S and continuing for L characters.
 
1 members found this post helpful.
Old 03-29-2010, 01:04 AM   #4
Olion
LQ Newbie
 
Registered: Mar 2010
Location: Germany
Posts: 24

Rep: Reputation: 15
Hi,

escape all metacharacter in the file using sed or

printf '%s' "$VAR"

Oleg

Last edited by Olion; 03-29-2010 at 01:11 AM.
 
1 members found this post helpful.
Old 03-29-2010, 01:19 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by Olion View Post
printf '%s' "$VAR"
Have I misunderstood? It doesn't seem to work:
Code:
c@CW8:~$ VAR="'ProcessName'"
c@CW8:~$ printf '%s' "$VAR"
'ProcessName'
 
Old 03-29-2010, 01:25 AM   #6
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
VAR=${VAR:1:$((${#VAR}-2))} works
 
1 members found this post helpful.
Old 03-29-2010, 02:05 AM   #7
Olion
LQ Newbie
 
Registered: Mar 2010
Location: Germany
Posts: 24

Rep: Reputation: 15
Quote:
Originally Posted by catkin View Post
Have I misunderstood? It doesn't seem to work:
Code:
c@CW8:~$ VAR="'ProcessName'"
c@CW8:~$ printf '%s' "$VAR"
'ProcessName'
O, sorry, I have misunderstood. I thought you need the ''.
 
Old 03-29-2010, 02:16 AM   #8
Olion
LQ Newbie
 
Registered: Mar 2010
Location: Germany
Posts: 24

Rep: Reputation: 15
again :-)

Code:
$ line="'Process'"
$ echo $line
'Process'
$ echo $line | tr -d "\'"
Process
$
 
Old 03-29-2010, 02:30 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Olion's version above works only if you can be sure that the ' will not appear anywhere else in the string, as it simply removes any and all marks it finds. But here again parameter substitution can do it faster and more efficiently than piping it through an external tool.
Code:
VAR="${VAR//\'}"
Otherwise, Catkin's version is probably the best, for the same reasons.
 
Old 03-29-2010, 02:30 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by dive View Post
VAR=${VAR:1:$((${#VAR}-2))} works
Thanks for the correction
 
Old 03-29-2010, 02:35 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
David made me rethink too as I have done the same by removing all

Code:
VAR=`file /tmp/core.1432 | awk '{ gsub(/^\047+|\047+$/,"",$14); print $14}'`
 
Old 03-29-2010, 03:04 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by spartiati View Post
I have a script that calls for a file description on a core file. I then pull the name of the process that caused the core file.
unfortunately, the process name is pulled with a leading ' amd a tailing'. I would like to remove the leading char and the last char.

code:
VAR=`file /tmp/core.1432 | awk '{ print $14}'`
echo "Process Name: $VAR"

output:
Process Name: 'ProcessName'

Results I need:
Process Name: ProcessName

I tried sed, but it seems to not like using the special char '.

I would appreciate any help on this.
Code:
VAR=$(file /tmp/core.1442)
echo ${VAR//\'}
VAR=${VAR//\'}
 
Old 03-29-2010, 03:09 AM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by spartiati View Post
I tried sed, but it seems to not like using the special char '.
I second the suggested solutions that use parameter substitution in BASH, anyway using sed you can do
Code:
echo $VAR | sed "s/'//g"
as you can see, using single quotes to embed the sed command is not mandatory. In this case you have to protect the single quote from bash and double quotes perform their job perfectly.
 
Old 03-29-2010, 04:13 AM   #14
Olion
LQ Newbie
 
Registered: Mar 2010
Location: Germany
Posts: 24

Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
I second the suggested solutions that use parameter substitution in BASH, anyway using sed you can do
Code:
echo $VAR | sed "s/'//g"
as you can see, using single quotes to embed the sed command is not mandatory. In this case you have to protect the single quote from bash and double quotes perform their job perfectly.
Thank you.
And for last word on the line:

$ cat x
ProcessName: 'Process'
$ sed "s/ '\([^ ][^ ]*\)' *$/ \1/" x
ProcessName: Process
$
 
Old 03-29-2010, 04:56 AM   #15
Olion
LQ Newbie
 
Registered: Mar 2010
Location: Germany
Posts: 24

Rep: Reputation: 15
Code:
VAR="${VAR//\'}"
This is cool, but not working with my shell. I use /bin/sh :-(

Oleg
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to remove first characters from every line (00) Linux - General 8 08-01-2011 10:28 AM
bash script to remove everything in a line before a space tallmtt Programming 2 12-28-2008 07:40 PM
bash script to remove the blank lines in the file naveensankineni Programming 7 03-25-2008 08:34 PM
help with bash script: remove * to the last . in string drkstr Linux - Software 3 04-25-2006 04:54 PM
remove part of string in bash script crewblunts Programming 2 03-16-2006 05:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration