Simple Cut Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple Cut Question
# 1  
Old 01-12-2010
Simple Cut Question

I've got a file that contains a large list of links in this type of style:
Code:
'home_dir\2009\09\01\file.html'

I'd like to cut off all of the characters left of 'file.html'. I tried:
Code:
 
cat file.txt | cut -d\ -f4

but it told me that I had an invalid delimiter.

So I tried:
Code:
cat file.txt | cut -d "\" -f4

and it presented me with a line prompt

Next I tried

Code:
 
cat file.txt | cut -d "/\" -f4

but I got the same thing.

Can anyone tell me what I'm doing wrong? Thanks

Last edited by Scott; 01-12-2010 at 05:47 PM.. Reason: Please use code tags
# 2  
Old 01-12-2010
including or excluding quotes?
Did you try basename?
Code:
basename 'home_dir\2009\09\01\file.html'

# 3  
Old 01-12-2010
Quote:
$ cat file
home_dir\2009\09\01\file.html
$ cat file | cut -d'\' -f5
file.html
Quote:
$ str='home_dir\2009\09\01\file.html'
$ echo ${str##*\\}
file.html


---------- Post updated at 02:18 PM ---------- Previous update was at 02:00 PM ----------

Quote:
Originally Posted by anchal_khare
including or excluding quotes?
Did you try basename?
Code:
basename 'home_dir\2009\09\01\file.html'

Is basename working for you with backslash?

Quote:
$ basename 'home_dir\2009\09\01\file.html'
home_dir\2009\09\01\file.html
$ basename '/home_dir/2009/09/01/file.html'
file.html
# 4  
Old 01-12-2010
Or:

Code:
awk -F'\' '{print $5}' filename

# 5  
Old 01-12-2010
Quote:
Originally Posted by Rally_Point
I tried:

cat file.txt | cut -d\ -f4 but it told me that I had an invalid delimiter.

So I tried:

cat file.txt | cut -d "\" -f4 and it presented me with a line prompt

>

Next I tried

cat file.txt | cut -d "/\" -f4 but I got the same thing.

Can anyone tell me what I'm doing wrong? Thanks
cut -d\ -f4: In this case, the shell's parser eats the backslash. The error message you see is the result of cut not receiving an argument to the -d option. It is equivalent to: cut -d -f4

cut -d "\" -f4: You opened a double quoted string, within which \" is an escape sequence representing a literal double quote. The shell is prompting you for further input because you have an open string pending. If you closed it (with another double quote), it would call cut with the -d option being passed a long string as an argument (definitely not what you want, resulting in another 'bad delimiter' error).

cut -d "/\" -f4: Same problem as above, with a forward slash that does not affect the semantics explained in the previous case.

cut -d\\ -f4 or cut -d'\' is what you'd want to do to pass \ as the delimiter.

If the above explanations don't make much sense to you, review sh quoting rules in the man page. It may clear things up further.

Regards,
alister
# 6  
Old 01-13-2010
Quote:
Originally Posted by anbu23


---------- Post updated at 02:18 PM ---------- Previous update was at 02:00 PM ----------



Is basename working for you with backslash?


yes it works, (I have cygwin on my home pc)
Code:
/home->basename 'home_dir\2009\09\01\file.html'
file.html
/home->

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

2. UNIX for Dummies Questions & Answers

Question on cut

Korn Shell I have a file whose values are delimited using colon ( : ) $ cat test.txt hello:myde:temp:stiker $ cut -d: -f2,4 test.txt myde:stikerI want field 2 and field 4 to be returned but separated by a hyphen. The output should look like myde-stiker How can do this ? (without awk... (11 Replies)
Discussion started by: kraljic
11 Replies

3. Shell Programming and Scripting

A question on cut

hi, I used cut to get the I have a file f1 with content: 101.2 ms RTT from 3WHS 95.2 ms RTT from 3WHS 97.3 ms RTT from 3WHS 97.4 ms RTT from 3WHS 122.2 ms RTT from 3WHS 103.5 ms RTT from... (2 Replies)
Discussion started by: esolve
2 Replies

4. Shell Programming and Scripting

Simple Cut issue

I have a long string that looks something like this.... <string>http://abc.com/40/20/zzz061-3472/dP3rXLpPPV2KC846nJ4VXpH7jt4b3LJgkL/tarfile_date.tar</string> I need to but the tar file name. So I need to put between "/" and ".tar</string>". My desired result should be "tarfile_date". (7 Replies)
Discussion started by: elbombillo
7 Replies

5. UNIX for Dummies Questions & Answers

Cut Question

Hi, I have created a variable abc within my script which can have values as follows abc = Ram,Iam or it can be abc = Uam or it can be abc = Sam,Tam,Pam Basically it can have a max of 3 values , seperated by comma. I want to assign these 3 values to 3 different variables In case of... (2 Replies)
Discussion started by: samit_9999
2 Replies

6. Shell Programming and Scripting

The cut command. Really simple question!

hi, sorry for asking what I am sure is a really easy question, I am wanting to cut the users real name from the output of 'finger'. $ cut -f2-3 filename is in my script but it only seems to cut the first line. I need to cut the 2nd and 3rd word from each line and store them in variables... (1 Reply)
Discussion started by: rorey_breaker
1 Replies

7. Shell Programming and Scripting

Cut -d Question

I went through quite a few threads and didn't find anything on this. I also looked on other sites and couldn't turn up an answer. For completeness sake, I'm working off of solaris 10 in the korn shell environment. I wrote a script for a buddy to help him out with the following issue. He... (12 Replies)
Discussion started by: Janus
12 Replies

8. UNIX for Dummies Questions & Answers

cut question

#!/bin/bash echo "UserName PID Command" ps -ef > ps.temp grep '^\{2,3\}\{4\}' ps.temp > ps.temp2 cut -f1,2,8 ps.temp2 rm ps.temp* I am having some problems with the cut command. I only want to display the UID (field 1), PID(field 2), and Command(field 8). Right now the whole ps -ef... (5 Replies)
Discussion started by: knc9233
5 Replies

9. Programming

Simple C question... Hopefully it's simple

Hello. I'm a complete newbie to C programming. I have a C program that wasn't written by me where I need to write some wrappers around it to automate and make it easier for a client to use. The problem is that the program accepts standard input to control the program... I'm hoping to find a simple... (6 Replies)
Discussion started by: Xeed
6 Replies

10. UNIX for Dummies Questions & Answers

Ok simple question for simple knowledge...

Ok what is BSD exactly? I know its a type of open source but what is it exactly? (1 Reply)
Discussion started by: Corrail
1 Replies
Login or Register to Ask a Question