Script Shell Extract substring


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script Shell Extract substring
# 8  
Old 06-27-2015
Quote:
Originally Posted by senhia83
How about

Code:
$ cat > tmp
g-82.text.text1.fr.worker1
g-xx.yyyyyy.zzzz.fr.worker2


$ awk -F".fr" '{print $1}' tmp
g-82.text.text1
g-xx.yyyyyy.zzzz

This suffers from exactly the same problem noted in post #5 in this thread!
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 06-27-2015
This might work?

Code:
$ cat > tmp
g-82.text.text1.fr.worker1
g-xx.yyyyyy.zzzz.fr.worker2
g-xx.Africa.zzzz.fr.worker3


$ awk -F"[.]fr" '{print $1}' tmp
g-82.text.text1
g-xx.yyyyyy.zzzz
g-xx.Africa.zzzz

# 10  
Old 06-27-2015
Quote:
Originally Posted by senhia83
This might work?

Code:
$ cat > tmp
g-82.text.text1.fr.worker1
g-xx.yyyyyy.zzzz.fr.worker2
g-xx.Africa.zzzz.fr.worker3


$ awk -F"[.]fr" '{print $1}' tmp
g-82.text.text1
g-xx.yyyyyy.zzzz
g-xx.Africa.zzzz

No. Try adding an input line:
Code:
g-xx.freezer.zzzz.fr.worker4

You have to match both periods; not just the first one. But, it would work if you used:
Code:
-F'[.]fr[.]'
   or 
-F'\\.fr\\.'

instead of:
Code:
-F"[.]fr"

This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 06-28-2015
Perhaps a sed like :
Code:
user@host:~/unix$ ls g*
g-82.text.text1.fr.worker1   g-xx.yyyyyy.zzzz.fr.worker2
g-xx.Africa.zzzz.fr.worker3
user@host:~/unix$ ls g* | sed "s/\(.*\)\(.fr.*\)/\1/g"
g-82.text.text1
g-xx.Africa.zzzz
g-xx.yyyyyy.zzzz

So \1 is what you want and \2 (which is not used) is the extension being removed from the ls output.
# 12  
Old 06-28-2015
Quote:
Originally Posted by Peasant
Perhaps a sed like :
Code:
user@host:~/unix$ ls g*
g-82.text.text1.fr.worker1   g-xx.yyyyyy.zzzz.fr.worker2
g-xx.Africa.zzzz.fr.worker3
user@host:~/unix$ ls g* | sed "s/\(.*\)\(.fr.*\)/\1/g"
g-82.text.text1
g-xx.Africa.zzzz
g-xx.yyyyyy.zzzz

So \1 is what you want and \2 (which is not used) is the extension being removed from the ls output.
In post #3 in this thread, I suggested simple, clear shell, sed and awk scripts that did exactly what was requested. Since then there have been several proposals that, although they work with the sample provided, would not work in the general case. Peasant's proposal above is more complicated than the sed suggestion I made earlier:
Code:
sed 's/[.]fr[.].*//' file

and, it works fine as long as the final string after the .fr. in the filenames being processed is literally worker followed by a string of digits. But, it fails if the two characters fr can ever appear anywhere in the filenames after the .fr. such as:
Code:
g-82.text.text1.fr.Alfred_Jones

With the sparse specification of the allowed filename format, we don't know if this is a problem in this case or not.
This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 06-30-2015
Thanks Don Cragun and Aia for clarification.

I have a doubt - If the text file is like -
Code:
cat file.txt
g-xx.fr.zzzz.fr.worker3
g-82.text.text1.fr.worker1
g-xx.yyyyyy.zzzz.fr.worker2

then -
Code:
sed 's/[.]fr[.].*//' file.txt

will result -

Code:
g-xx
g-82.text.text1
g-xx.yyyyyy.zzzz

I hope below one is suitable if file structure remains the same.
Code:
awk -F"." '{print $1"."$2"."$3}' file.txt

or
Code:
awk -F'.' 'BEGIN{OFS="." ;} {print $1,$2,$3}' file.txt

this will result -
Code:
g-xx.fr.zzzz
g-82.text.text1
g-xx.yyyyyy.zzzz


Last edited by Mannu2525; 06-30-2015 at 03:06 AM.. Reason: Adding Alternate Command
# 14  
Old 06-30-2015
Quote:
Originally Posted by Mannu2525
Thanks Don Cragun and Aia for clarification.

I have a doubt - If the text file is like -
Code:
cat file.txt
g-xx.fr.zzzz.fr.worker3
g-82.text.text1.fr.worker1
g-xx.yyyyyy.zzzz.fr.worker2

then -
Code:
sed 's/[.]fr[.].*//' file.txt

will result -

Code:
g-xx
g-82.text.text1
g-xx.yyyyyy.zzzz

I hope below one is suitable if file structure remains the same.
Code:
awk -F"." '{print $1"."$2"."$3}' file.txt

or
Code:
awk -F'.' 'BEGIN{OFS="." ;} {print $1,$2,$3}' file.txt

this will result -
Code:
g-xx.fr.zzzz
g-82.text.text1
g-xx.yyyyyy.zzzz

We assume that you realize that the requirements you have presented here are completely different from the requirements you presented in post #1 in this thread (where the request was to remove .fr. and everything following it). But, yes, with these new requirements, the above awk script does what you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substring check in IF condition in shell script

I want to check if the string has the substring in IF condition then process... i tried below but not working if ]; then ............. field can be "reserved1" ....reservedn / fillspaces1 ... fillspacesn (4 Replies)
Discussion started by: greenworld123
4 Replies

2. Shell Programming and Scripting

Need help with Korn Shell script for substring printing

Hi all, I am new to scripting. I have a file with colon separated values called mylist.txt cat mylist.txt 192.123.76.89:lmprod89 162.122.20.28:lmtstserver28 10.80.32.139:hewprod139 . . using our internal os utility (called mvsping) we need to check all these servers if they are... (6 Replies)
Discussion started by: kraljic
6 Replies

3. Shell Programming and Scripting

using substring in shell script

This is the data I am having in a file Just for sample I have given 3 records. The file which I am having consists of n number of records. ABC123 10 01/02/2008 2008-01-03-00.00.00.000000 DYUU 22 02/03/2008 2008-01-04-00.00.00.000000 RF33 88 03/05/2008 2008-01-05-00.00.00.000000 ... (24 Replies)
Discussion started by: kmanivan82
24 Replies

4. Shell Programming and Scripting

Substring in shell script

I need a help in getting substring of each line in input file. I am writing a script that will read a file from a directory on daily basis, I mean everyday a new file will be stored in this directory, it will replace old file. I have to read contents of this file, the contents will be as... (5 Replies)
Discussion started by: jyotib
5 Replies

5. Shell Programming and Scripting

Using Awk in shell script to extract an index of a substring from a parent string

Hi All, I am new to this shell scripting world. Struck up with a problem, can anyone of you please pull me out of this. Requirement : Need to get the index of a substring from a parent string Eg : index("Sandy","dy") should return 4 or 3. My Approach : I used Awk function index to... (2 Replies)
Discussion started by: sandeepms17
2 Replies

6. UNIX for Dummies Questions & Answers

Substring in Shell Script

Hi I'm new to Shell scripting. Someone please help me in extracting a portion of string from a file. Eg: I got a file like, Readme.txt and has the following name value pairs input1 : /homes/input1/ input2 : /homes/input2/ ... ... When I give the parameter input1, the value... (3 Replies)
Discussion started by: smartbuddy
3 Replies

7. Shell Programming and Scripting

command/script to extract a substring from a string

I have a long string "<ID type="Oid">{}</ID>" I need to extract "GigabitEthernet0/1" from the above string. How can it be done? :) (5 Replies)
Discussion started by: girisha
5 Replies

8. UNIX for Dummies Questions & Answers

Substring function in UNIX shell script

Hi All, Following is the output of a find commnd to locate log directories for various projects of UNIX AIX box: /home/hbinz6pf/projectlibs/dpr_pfsdw_dev/&PH& /opt/tools/ds/Template/&PH& /data/ds/ms/hmsdw/projectlibs/dpr_ms_dev/&PH& /data/ds/riskmi/projectlibs/dpr_riskmi_dev/&PH&... (5 Replies)
Discussion started by: csrazdan
5 Replies

9. Shell Programming and Scripting

Substring function in UNIX shell script

Hi All, Following is the output of a find commnd to locate log directories for various projects of UNIX AIX box: /home/hbinz6pf/projectlibs/dpr_pfsdw_dev/&PH& /opt/tools/ds/Template/&PH& /data/ds/ms/hmsdw/projectlibs/dpr_ms_dev/&PH& /data/ds/riskmi/projectlibs/dpr_riskmi_dev/&PH&... (1 Reply)
Discussion started by: csrazdan
1 Replies

10. Shell Programming and Scripting

Substring in C shell script?

i am a new user of C-shell script. I want to know can i create a substring in a string. That means when i got a variable $input = "it is number 2" I want to get the "2" to be another variable. Can i do that in C-shell and how to ? Thank you so much dinodash (0 Replies)
Discussion started by: dinodash
0 Replies
Login or Register to Ask a Question