Cut out string in bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut out string in bash script
# 1  
Old 02-21-2010
Cut out string in bash script

Hi all,
I'm trying to extract string from variable in BASH. That's probably trivial for grep but I couldn't figure it out.

I want to get name, there's sometimes digit after it, but it should be left out.
Code:
STRING=http://name5.domain.com:8000/file.dat

Could someone help me with that?
Any solution will do. Smilie
# 2  
Old 02-21-2010
The following works fine if there's always a digit after "name" and none embedded within it.

Code:
$ STRING=http://name5.domain.com:8000/file.dat
$ n=${STRING%%[0-9]*}
$ n=${n#*//}
$ echo $n
name


Last edited by alister; 02-21-2010 at 07:15 PM..
# 3  
Old 02-21-2010
Code:
$ 
$ STRING=http://name5.domain.com:8000/file.dat
$ 
$ echo $STRING | perl -lne '/http:\/\/(.*?)\d.*/ && print $1'
name
$ 
$

tyler_durden
# 4  
Old 02-21-2010
The following will extract the first component of the url and strip any trailing digits that may be present (leading or embedded numbers are left intact):

Code:
echo "$STRING" | awk -F'\\/\\/|\\.' '{sub(/[0-9]+$/, "", $2); print $2}'


@durden tyler
Quote:
Originally Posted by durden_tyler
Code:
$ 
$ STRING=http://name5.domain.com:8000/file.dat
$ 
$ echo $STRING | perl -lne '/http:\/\/(.*?)\d.*/ && print $1'
name
$ 
$

That perl will not work if the trailing digit is not present or if there's a digit embedded elsewhere in the name. The latter may be unlikely, but the original query states that the digit is not always present.

Cheers,
Alister
# 5  
Old 02-21-2010
Thanks guys for fast response!

Maybe I wasn't clear enough about that digit, but yeah, alister's solution works in both cases.

Cheers.
# 6  
Old 02-21-2010
One more Smilie

Code:
echo $STRING | sed 's|^http://\([^.]*\)\..*$|\1|; s|[0-9]*$||'

Alister
# 7  
Old 02-21-2010
Quote:
Originally Posted by alister
...
That perl will not work if the trailing digit is not present or if there's a digit embedded elsewhere in the name. The latter may be unlikely, but the original query states that the digit is not always present.
...
Sorry, my bad.

Here's a file that contains variations of the URL, with the digit at the end, at the beginning, in between, and non-existent.

Code:
$ 
$ cat -n f6
     1  http://name5.domain.com:8000/file.dat
     2  http://na5me.domain.com:8000/file.dat
     3  http://5name.domain.com:8000/file.dat
     4  http://name.domain.com:8000/file.dat
$ 
$

Here's the Perl equivalent of your second awk script i.e. remove digit at the end (if present), but retain digit in-between or at the beginning (if present). Otherwise just fetch the word between "//" and "." that does not have any digits.

Code:
$ 
$ perl -lne '/http:\/\/(.*?)(\d)*\..*/ && print $_,"\t=>\t",$1' f6
http://name5.domain.com:8000/file.dat   =>      name
http://na5me.domain.com:8000/file.dat   =>      na5me
http://5name.domain.com:8000/file.dat   =>      5name
http://name.domain.com:8000/file.dat    =>      name
$ 
$

Since nothing has been mentioned about the case when the digit is at the beginning or in-between, I shall consider a couple of cases.

Case 1:
The digit at the beginning is to be removed.
The digit in-between has to be retained.
The digit at the end is to be removed.
Which means - "remove digits from the ends, retain it in the middle".

Code:
$ 
$ perl -lne '/http:\/\/(\d)*(.*?)(\d)*\..*/ && print $_,"\t=>\t",$2' f6
http://name5.domain.com:8000/file.dat   =>      name
http://na5me.domain.com:8000/file.dat   =>      na5me
http://5name.domain.com:8000/file.dat   =>      name
http://name.domain.com:8000/file.dat    =>      name
$

Case 2:
The digit at the beginning is to be removed.
The digit in-between has to be removed.
The digit at the end is to be removed.
Which means - "remove digits from the ends and the middle".

Code:
$ 
$ perl -lne 'if (/http:\/\/(\d)*(.*?)(\d)*\..*/){ ($x = $2) =~ s/\d//g; print $_,"\t=>\t",$x}' f6
http://name5.domain.com:8000/file.dat   =>      name
http://na5me.domain.com:8000/file.dat   =>      name
http://5name.domain.com:8000/file.dat   =>      name
http://name.domain.com:8000/file.dat    =>      name
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

2. Shell Programming and Scripting

Passing string from SQL to a BASH script

OS Solaris 10, DB oracle 10g Hello, We currently have a BASH script that runs and moves image files from a remote server to the local db server. A snippet of the code shows that we are picking up all Images that are 'mtime -1' some code... for file in `ssh user@10.200.200.10 'find... (3 Replies)
Discussion started by: JonP
3 Replies

3. Shell Programming and Scripting

Need script to cut string from a filename

Hi, I need a script which do below I have a filename: TEST2013_09_17_XX_XX_XX.csv Now script should create a new file with name: XX_XX_XX.csv Or I should say i need the output as XX_XX_XX.csv Please help. Mant thanks in advance (3 Replies)
Discussion started by: sv0081493
3 Replies

4. Shell Programming and Scripting

Requesting help to replace a string by my bash script

Hello every1, I need help to replace a string in a file by my bash script. Find: log4j.appender.toLogFile.layout.ConversionPattern= %d %5p (%F:%L) - %m%n= %d %5p (%F:%L) - %m%n Replace: log4j.appender.toLogFile.layout.ConversionPattern= %d %5p (%F:%L) - %m%n I tried by sed, but kept... (7 Replies)
Discussion started by: titanic4u
7 Replies

5. Shell Programming and Scripting

bash script tail when string found do something

Okay, I have two scripts, the first one does some stuff, and comes to a point where it has this: Right here it runs a quick script to start something that writes to a log file. /usr/bin/tail -f ${pathVar}/nohup_${servVar}.out | while read -r line do ] && continue cd ${pathVar}... (0 Replies)
Discussion started by: cbo0485
0 Replies

6. Shell Programming and Scripting

bash shell script string comparison

I want to remove a line that has empty string at second field when I use cut with delimeter , like below $cat demo hello, mum hello, #!/bin/sh while read line do if then # remove the current line command goes here fi done < "demo" i got an error message for above... (4 Replies)
Discussion started by: bonosungho
4 Replies

7. Shell Programming and Scripting

Bash script for searching a string

Hi, I have a file that contains thousands of records. Each record starts with "New Record". I need to search this file based on a given value of "timestamp" and write all the records that match this timestamp into a new file. I was able to locate the existence of a given value of timestamp using... (10 Replies)
Discussion started by: shoponek
10 Replies

8. Shell Programming and Scripting

bash script to check the first character in string

Hello would appreciate if somebody can post a bash script that checks if the first character of the given string is equal to, say, "a" thnx in advance (2 Replies)
Discussion started by: ole111
2 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. UNIX for Dummies Questions & Answers

Shell script: Cut / (slash) character in string

I have a string "\/scratch\/databases\". I want to have a new string "\/scratch\/databases" by cutting last '\' character using shell script. I can't do this Please help me. Thanks in advance ThuongTranVN (4 Replies)
Discussion started by: ThuongTranVN
4 Replies
Login or Register to Ask a Question