Splitting strings based on delimiter


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Splitting strings based on delimiter
# 1  
Old 04-09-2015
Splitting strings based on delimiter

i have a snippet from server log delimited by forward slash.

/a/b/c/d/filename

i need to cut until last delimiter. So desired output should look like:

/a/b/c/d

can you please help?

Thanks in advance.
# 2  
Old 04-09-2015
Is this a homework assignment?

What operating system are you using?

What shell are you using?
# 3  
Old 04-09-2015
Not home work actually. Part of KT.
x86_64 GNU/Linux
bash shell.
# 4  
Old 04-09-2015
Like so?
Code:
var=/a/b/c/d/filename
echo "${var%/*}"

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-09-2015
If you want to get the pathname of the directory containing a file whose name is stored in a variable (as opposed to removing the last / and everything following it, consider this alternative:
Code:
var=/a/b/c/d/filename
echo "$(dirname "$var")"

With var set this way, you'll get the same results both ways (but dirname will run slower). But, compare the results of the above code with the code Scrutinizer suggested with these settings for var:
Code:
var=filename
var=/filename

# 6  
Old 04-09-2015
Thanks.
Actually i am trying to generate a report from log.
I have awk'ed this field which is containing directory and filename altogether.
All I need is, to pass directory name and filename in 2 variable and put it under final report.
# 7  
Old 04-09-2015
Show us the output you want, the input you have, and the program you've got. Should be possible to put it inside the awk program you have.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Splitting strings

I have a file that has two columns. I first column is an identifier and the second is a column of strings. I want to split the characters in the second column into substrings of length 5. So if the first line of the file has a string of length 10, the output should have the identifier repeated 2... (3 Replies)
Discussion started by: verse123
3 Replies

2. Shell Programming and Scripting

awk Splitting strings

Hi All, There is a file with a data. If the line is longer than 'n', we splitting the line on the parts and print them. Each of the parts is less than or equal 'n'. For example: n = 2; "ABCDEFGHIJK" -> length 11 Results: "AB" "CD" EF" GH" "IJ" "K" Code, but there are some errors.... (9 Replies)
Discussion started by: booyaka
9 Replies

3. Shell Programming and Scripting

Splitting records in a text file based on delimiter

A text file has 2 fields (Data, Filename) delimited by # as below, Data,Filename Row1 -> abc#Test1.xml Row2 -> xyz#Test2.xml Row3 -> ghi#Test3.xml The content in first field has to be written into a file where filename should be considered from second field. So from... (4 Replies)
Discussion started by: jayakkannan
4 Replies

4. Shell Programming and Scripting

Splitting number (no delimiter)

Hi ! here is my data file column 1 fields 2597 121297 13599 130498 want to print like this 02/05/1997 12/12/1997 13/05/1999 13/04/1998 how to split each field and print in correct way if there was delimiter I would have done some thing like this awk '{split($0,a,":");... (13 Replies)
Discussion started by: Akshay Hegde
13 Replies

5. Shell Programming and Scripting

splitting tab delimited strings

hi i have a requirement to input a string to a shell script and to split the string to multiple fields, the string is copied from a row of three columns (name,age,address) in an excel sheet. the three columns (from excel) are seperated with a tab when pasted in the command prompt, but when the ... (2 Replies)
Discussion started by: midhun19
2 Replies

6. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

7. Shell Programming and Scripting

Substring based on delimiter, finding last delimiter

Hi, I have a string like ABC.123.XYZ-A1-B2-P1-C4. I want to delimit the string based on "-" and then get result as only two strings. One with string till last hyphen and other with value after last hyphen... For this case, it would be something like first string as "ABC.123.XYZ-A1-B2-P1" and... (6 Replies)
Discussion started by: gupt_ash
6 Replies

8. Programming

Splitting strings from file

Hi All I need help writing a Java program to split strings reading from a FILE and writing output into a FILE. e.g., My input is : International NNP Rockwell NNP Corp. NNP 's POS Tulsa NNP unit NN said VBDExpected output is: International I In Int Inte l al... (2 Replies)
Discussion started by: my_Perl
2 Replies

9. Shell Programming and Scripting

splitting file with more than one delimiter

Hi, I just wandering how to split a record which has more than one delimiter, i have a file which contains pattern as group separtor and ~ as field separtor, Ultimately I need consider even the groups as a field, So i need to make this multi-delimited file into ~ delimited file. My record... (4 Replies)
Discussion started by: braindrain
4 Replies

10. UNIX for Dummies Questions & Answers

splitting strings

Hi you, I have the following problem: I have a string like the followings: '166Mhz' or '128MB' or '300sec' or ... What I want to do is, I want to split the strings in a part with the numbers and a part with letters. Since the strings are not allway three digits and than text i couldn't do... (3 Replies)
Discussion started by: bensky
3 Replies
Login or Register to Ask a Question