Sponsored Content
Top Forums Shell Programming and Scripting Cut out string in bash script Post 302397318 by durden_tyler on Sunday 21st of February 2010 08:27:49 PM
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
UNKNOWN(1)						      General Commands Manual							UNKNOWN(1)

NAME
unknown - identify possible genotypes for unknowns SYNOPSIS
A program to rapidly identify which genotypes are possible for individuals typed as unknowns in the input pedigree. unknown [ -cl ] DESCRIPTION
unknown infers possible genotypes and mating combinations for parents with unknown genotypes for ilink(1), mlink(1) and linkmap(1). OPTIONS
-c Use conditional allele frequencies. -l Choose a good set of loop breakers automatically. RETURN VALUE
0 Successful completion ERRORS
10 File not found 255 Failure EXAMPLES
Normally, unknown(1) is run immediately prior to its sister programs, ilink(1), mlink(1) and linkmap(1), like this: unknown mlink FILES
unknown(1) reads the two files pedfile.dat and datafile.dat as its own input and produces various temporary files that are used as input to the next program. These temporary files are ipedfile.dat, upedfile.dat, speedfile.dat and newspeedfile.dat. NOTES
unknown(1) is part of the FASTLINK package, which is a re-implementation of the LINKAGE suite of computer tools that help investigate genetic linkage as first proposed G.M. Lathrop, J.M. Lalouel, C. Julier, and J. Ott. AUTHORS
Dylan Cooper, Alejandro Schaffer, and Tony Schurtz based on work originally by Jurg Ott, Ph.D, et. al. This manual page was written by Elizabeth Barham <lizzy@soggytrousers.net> for the Debian GNU/Linux system (but may be used by others). WORD-WIDE-WEB http://www.ncbi.nlm.nih.gov/CBBResearch/Schaffer/fastlink.html SEE ALSO
ilink(1), linkmap(1), lodscore(1), mlink(1). April 15, 2003 UNKNOWN(1)
All times are GMT -4. The time now is 10:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy