cut subsrting from string depending on pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cut subsrting from string depending on pattern
# 1  
Old 08-04-2008
cut subsrting from string depending on pattern

Dears,
i'm glad to be a member of this big UNIXer's group Smilie ,hoping to be one of'm.



my question:
how i get the subsrting ahmad "from" the string ".ahmad." for example ??

Thank you all.
# 2  
Old 08-04-2008
try:
Code:
str=".ahmad."
echo $str | cut -d '.' -f2

# 3  
Old 08-04-2008
i really appreciate ur answer, but this resulted in the following:

ahmad.

while i want only ahmad with no dot in the end
# 4  
Old 08-04-2008
echo $str | awk -F"." '{print $2}'
# 5  
Old 08-04-2008
Quote:
Originally Posted by Muslem_it
[...]
my question:
how i get the subsrting ahmad "from" the string ".ahmad." for example ??
It depends on which shell you're using and on what exactly you want to achieve: remove the dots or remove the first and the last char.
Consider the following:

[zsh]

Code:
% s=.ahmad.
% print $s:gs/.//
ahmad

Code:
% print ${${s#?}%?}
ahmad

[bash/ksh93]

Code:
$ s=.ahmad.
$ printf "%s\n" "${s//.}"
ahmad

Code:
$ printf "%.*s\n" $((${#s}-2)) "${s#?}"
ahmad

# 6  
Old 08-04-2008
Quote:
Originally Posted by Muslem_it
i really appreciate ur answer, but this resulted in the following:

ahmad.

while i want only ahmad with no dot in the end
Very surprising!
Works fine on my AIX box and my PC (with cygwin).

If you have problem with the cut solution, use the awk solution.

And another way (for the fun) :
Code:
expr "$str" : '.*\.\(.*\).'

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change nth depending on matiching a pattern in Y line

Hi, I have below file, each line that starts with /* marks the beginning of the a new job. /* ----------------- cmdsMlyMoveTPMPLANTJ ----------------- UNIX_JOB CMMM002J CMDNAME /home2/proddata/bin/moveTPMPLANT.sh AGENT CMDSHP USER proddata AFTER CMMU001J /* "DDRG monthly... (13 Replies)
Discussion started by: varun22486
13 Replies

2. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

3. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

4. Shell Programming and Scripting

How to cut string and find missing pattern?

i have list in file named sample.txt eg i want to cut the 3rd and 4th character i.e. 01,02,03....,24(max length is 24) and i want to find the missing sequence .and display them i.e. (15 Replies)
Discussion started by: sagar_1986
15 Replies

5. Shell Programming and Scripting

cut specific pattern

Hi i want to cut a variable like if it is ABCDEF then i want to cut "DEF" only Please help me regarding this. (1 Reply)
Discussion started by: aishsimplesweet
1 Replies

6. Shell Programming and Scripting

To filter a certain pattern depending on neighbour

Hi, As I am new to filtering section of Unix I am facing a problem I have following lines in text file : Created RELEASE in dist/Apple_release_1_0_.zip Created RELEASE in dist/Banana_release_1_0_.zip Created RELEASE in dist/Mango_release_1_0_.zip Created RELEASE in... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

7. UNIX for Dummies Questions & Answers

Depending upon character at particular position, string needs to be appended

Depending upon character at particular position in a line, string needs to be appended followed by empty spaces till 256th column position at the end of the each line in the file. example: 123p4 J8769 123q4 R2345 123r4 S2345 What I Require is, it has to check at 7th column character and... (10 Replies)
Discussion started by: vaka
10 Replies

8. UNIX for Dummies Questions & Answers

How can I rename multiple files depending on a string occuring in the filenames?

I have many files that have "inputstring" somewhere in their filename (without the quotes), and I want to rename them all so that "inputstring" is replaced with "newstring". And I also want to specify arbitrary text for "inputstring" and "newstring" so that I can call the scripts that does this... (6 Replies)
Discussion started by: karman
6 Replies

9. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

10. Shell Programming and Scripting

Replace string B depending on occurence of string A

Depending upon the occurence of string 'xyz', I want to remove -t from the input file. There is not a fixed length input file. Any suggestions Input file: this is xyz line -t of the data this is line 2 of -t of the data xyz this is line 3 of -t the file this is line xyz of the -t file... (1 Reply)
Discussion started by: hemangjani
1 Replies
Login or Register to Ask a Question