LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   piece of code (https://www.linuxquestions.org/questions/programming-9/piece-of-code-744795/)

Volcano 08-03-2009 11:48 AM

piece of code
 
Can anybody tell what this piece of code is doing?

DIR_NAME=`echo $1 | cut -d" " -f 9 | sed 's/"//g'`

pwc101 08-03-2009 11:52 AM

Depends what's in $1 (probably the first argument to the script).

Volcano 08-03-2009 12:00 PM

Quote:

Originally Posted by pwc101 (Post 3629784)
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

pwc101 08-03-2009 12:01 PM

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.

paulsm4 08-03-2009 12:02 PM

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

Volcano 08-04-2009 01:03 AM

>>>sed 's/"//g'

what this does more specifically ?

cicorino 08-04-2009 01:40 AM

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...

vikas027 08-04-2009 03:27 AM

Quote:

Originally Posted by Volcano (Post 3630413)
>>>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

paulsm4 08-04-2009 09:28 AM

So Volcano -

Have you fixed the bug?

Do you understand the sed "/g" modifier?


All times are GMT -5. The time now is 01:40 PM.