Command to get a substring.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Command to get a substring.
# 1  
Old 05-07-2012
Command to get a substring.

Hi Friends,
I am new to shell scripting. I am writting a script where i am reading word by word from a file . sometimes i need to retrieve only the subset of the word. for example:

suppose ",table2.col2,tableone.column1," is the string.

1>I just want to know the command which will give me "table2" and "tableone" as output.(all substrings which falls between a comma(first) and a dot)

thanks in advance Smilie
# 2  
Old 05-07-2012
Hi

Code:
$ echo ",table2.col2,tableone.column1," | awk -F[,.] '{print $2}'
table2

# 3  
Old 05-07-2012
@guruprasadpr : thanks for your reply. But i want a command which gives me all substring which falls between a comma(first) and a dot.
here it is: table2 and tableone .. thanks in advance Smilie
# 4  
Old 05-07-2012
Code:
 
$ echo ",table2.col2,tableone.column1," | nawk -F, '{for(i=1;i<=NF;i++){if($i~/\./){split($i,a,".");print a[1]}}}'
table2
tableone

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 05-07-2012
Code:
sed 's/,\([^.]*\)./ \1 /g' infile | awk '{print $1,$3}'

# 6  
Old 05-07-2012
Code:
echo ",table2.col2,tableone.column1," | perl -lne 'print $1 while (/,(.+?)\./g)'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looking for an awk command to print strings only if substring is missing

I have a file that I need to find each interface that has move-group on the interface line and print this line if the lines under the interface does Not have "filter-shared 14". Example file: interface 1/1/1/0 move-group decription one one one zero no shut filter-shared 14... (21 Replies)
Discussion started by: numele
21 Replies

2. Shell Programming and Scripting

Help me please: UNIX command to extract substring not squeeze spaces

Hi experts, Please help me!... I have a string " test1 test2 test3 ". There are two spaces before "test1"; There are four spaces between "test1" and "test2"; there are two spaces between "test2 and "test3". I want to extract a substring "2 test3" using positions. Below is my test... (5 Replies)
Discussion started by: sophiez16
5 Replies

3. Shell Programming and Scripting

Substring

Hi All, In ksh, am trying to get a substring stuff done. Not sure where the problem is.. can you guys guide me on this... for instance, var1=41, and var2=4175894567, then i want to know whether var2 starts with var1.. var1 and var2 can be of any length.. VAR1=41 VAR2=419068567777... (6 Replies)
Discussion started by: nram_krishna@ya
6 Replies

4. Shell Programming and Scripting

Get the substring

Hi All, I have a ouput string likes 'u8wos' or 'u10acsd' or somthing else 'u{number}{any characters}'and I want to get the number behind the letter 'u' by bash shell. Thanks Damon (11 Replies)
Discussion started by: Damon_Qu
11 Replies

5. UNIX for Dummies Questions & Answers

substring without using expr command

Hi guys, For some reason the terminal on my mac does not let me run string manipulations commands using the expr command. I'm not sure how to fix this so I'm requesting a "work-around" to using the expr command... This is the string I'm working with: "neo_opls01_1.log" And I'm trying to... (9 Replies)
Discussion started by: ah7391
9 Replies

6. Shell Programming and Scripting

sed command for substring operation

Hi All, Input=abcDEF_1.6k1 I need to use ‘sed' command to get 1.6 value and store to some variable from the given input. Please help me in getting the command. Regards, Kalai (2 Replies)
Discussion started by: kalpeer
2 Replies

7. UNIX for Dummies Questions & Answers

Need help with substring

I need to check the occurrence of one string within another. code ******************** if ;then do something done ******************** Thanks (7 Replies)
Discussion started by: w020637
7 Replies

8. Shell Programming and Scripting

Substring HELP!

Hi, I am trying to do something which I thought was very simple but still being a beginner, has proved not to be. Input: val1 val2 val3 val4 val5 val6 . . . etc Desired Output: Every row in which value of val6 is a number starting with 0.0 or contains a capital E. The input... (2 Replies)
Discussion started by: awknerd
2 Replies

9. Shell Programming and Scripting

substring command works but only in BASH shell

I am having trouble running a .sh file. The code 'x=${file_name:0:$z-11}' is giving me a bad substitution error. However when I run in BASH it works. Thing is when this goes to production the .sh will not be running in BASH. Is there a way to substring a string not in BASH or a way to invoke... (2 Replies)
Discussion started by: edwardtk11
2 Replies

10. 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
Login or Register to Ask a Question