LinuxQuestions.org
Help answer threads with 0 replies.
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 08-03-2009, 11:48 AM   #1
Volcano
Member
 
Registered: Sep 2005
Posts: 225

Rep: Reputation: 15
piece of code


Can anybody tell what this piece of code is doing?

DIR_NAME=`echo $1 | cut -d" " -f 9 | sed 's/"//g'`
 
Old 08-03-2009, 11:52 AM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Depends what's in $1 (probably the first argument to the script).
 
Old 08-03-2009, 12:00 PM   #3
Volcano
Member
 
Registered: Sep 2005
Posts: 225

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pwc101 View Post
Depends what's in $1 (probably the first argument to the script).

$ could be either of two cases


(1)$1 is a filename

(2) $1 is a string value
 
Old 08-03-2009, 12:01 PM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Care to give an example of both?

It looks like it's parsing a particular part of that string/filename to remove quotes and then assigning it to a variable called DIR_NAME.
 
Old 08-03-2009, 12:02 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
It's also got a bug. Here's sample output:
Quote:
ls -l /tmp/*.c
-rw-r--r-- 1 paulsm users 1589 2009-01-16 13:55 /tmp/1.c
-rw-r--r-- 1 paulsm users 1180 2008-06-15 09:21 /tmp/determinant.c
-rw-r--r-- 1 paulsm users 1249 2006-11-23 13:42 /tmp/hello_gtk.c
-rw-r--r-- 1 paulsm users 782 2009-02-17 14:56 /tmp/tmp.c
-rw-r--r-- 1 paulsm users 110 2009-07-27 15:13 /tmp/x.c
-rw-r--r-- 1 paulsm users 125 2009-06-27 19:16 /tmp/xx.c
<= Here's an example of what could go into "$1"

ls -l /tmp/*.c | cut -d" " -f 9 | sed 's/"//g'
/tmp/1.c
/tmp/determinant.c
/tmp/hello_gtk.c
14:56
15:13
19:16
<= Here's an example of what happens when you pipe "ls -l" through "cut -d" "..."
The problem is that "cut -d" counts *the number of spaces*. If you happen to have exactly 9 spaces (like the 7th column in "ls -l /tmp/1.c") it works. Otherwise, it doesn't ;-).

Here's an alternate solution:
Quote:
ls -l /tmp/*.c | awk '{print $8}'
/tmp/1.c
/tmp/determinant.c
/tmp/hello_gtk.c
/tmp/tmp.c
/tmp/x.c
/tmp/xx.c

Last edited by paulsm4; 08-03-2009 at 12:09 PM.
 
Old 08-04-2009, 01:03 AM   #6
Volcano
Member
 
Registered: Sep 2005
Posts: 225

Original Poster
Rep: Reputation: 15
>>>sed 's/"//g'

what this does more specifically ?
 
Old 08-04-2009, 01:40 AM   #7
cicorino
Member
 
Registered: May 2006
Location: Italy
Distribution: Slackware, Slackware64
Posts: 31

Rep: Reputation: 16
come on!!
if you type "info sed" then you can discover that 'substitute [globally]' is one of the most frequently used sed commands.
As example: you can use it to remove double quotes...
 
Old 08-04-2009, 03:27 AM   #8
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by Volcano View Post
>>>sed 's/"//g'

what this does more specifically ?
The most common use of sed is to replace characters. The same is applied here.

Examples:

Here, abc is replaced by vikas.
Code:
-sh-3.00$ echo "abc def" | sed 's/abc/vikas/g'
vikas def
Here, I am removing abc from my text.
Code:
-sh-3.00$ echo "abc def" | sed 's/abc//g'
 def

In the same way, double quotes are removed here.
Code:
sed 's/"//g'
Hope, this helps.

To have more examples and clear picture of sed, see these
http://www.tldp.org/LDP/abs/html/x21467.html
http://www.grymoire.com/Unix/Sed.html
http://www.pathogenomics.sfu.ca/brin...xcmds.html#sed
http://www.alexandersandler.net/sed-the-missing-manual
http://www.linuxhowtos.org/System/se...m?ref=news.rdf
http://www.gnulamp.com/sed.html
http://www.unixguide.net/unix/sedoneliner.shtml
http://www.unix.com/shell-programmin...iable-sed.html
 
Old 08-04-2009, 09:28 AM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
So Volcano -

Have you fixed the bug?

Do you understand the sed "/g" modifier?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Is there such a piece of software? wezzie Linux - Software 3 06-05-2008 09:38 AM
is there a piece of c code that can set the terminal window tinieprotonjam Programming 2 01-27-2007 07:20 AM
building shell command piece by piece aunquarra Linux - General 7 05-02-2006 06:43 AM
Unusual piece of bash script code raypen Slackware 2 03-18-2006 10:49 PM
nice piece of code ....HOW DOES IT WORK? deadhead Programming 7 12-07-2003 12:52 AM

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

All times are GMT -5. The time now is 07:18 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