Help identifying the first word in a string

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help identifying the first word in a string
# 1  
Old 12-09-2010
Help identifying the first word in a string

Hi all, I'd like to know how to identify the first word in a string (in bash) for e.g.
echo "enter your name"
read name
(user enters 'Joe Bloggs' for e.g.)

echo "hello $name"
(output says "hello Joe")

Thanks for any help

Last edited by Scott; 12-09-2010 at 04:57 AM.. Reason: Code tags
# 2  
Old 12-09-2010
echo "enter your name"
read name
name=$(echo $name | cut -d" " -f1)
echo "hello $name"

# 3  
Old 12-09-2010
read name
echo "Hello ${name%% *}"

# 4  
Old 12-09-2010
Spot on guys, thanks very much for your quick responses, much appreciated
# 5  
Old 12-09-2010
I have seen splitting/tokenizing a string using % OR # earlier also in other threads but never really understood how exactly that works.
Could you please explain that command?
# 6  
Old 12-09-2010
Hi, this particular expansion cuts the longest match of space followed by any character off of the end of the string. It is described here with examples: 2.6.2 Parameter Expansion
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove last word of a string?

Hello I have a string that may or may not have 4 dots. The string is actualy a file within a folder, where multiple files are located. Files may look like: # ls * creds: someserver shares: someserver.sharefolder # I want to fill 2 variables,... (1 Reply)
Discussion started by: sea
1 Replies

2. Shell Programming and Scripting

Deleting a word from a string

Hello All, I have a string like below - /LDCA20/rel/prod/libina.a I want to delete "libina.a" which is at the end.How can I do this ?? Thanks in advance Regards, Anand (10 Replies)
Discussion started by: anand.shah
10 Replies

3. Shell Programming and Scripting

How can we get the character before and after a word in a string?

Hi friends, I am working in a korn shell. i want to know the command which gives me the previous one character and next one character of a given keyword in a string? for exmaple: input string: /bin/dir/folder1/.proc_name^folderone Keyword: proc_name output : .^ ... (10 Replies)
Discussion started by: neelmani
10 Replies

4. Shell Programming and Scripting

Identifying entries based on 2 fields in a string.

Hi Guys, I’m struggling to use two fields to do a duplicate/ unique by output. I want to look IP addresses assigned to more than one account during a given period in the logs. So duplicate IP and account > 1 then print all the logs for that IP. I have been Using AWK (just as its installed... (3 Replies)
Discussion started by: wabbit02
3 Replies

5. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

6. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 Settings.xml (No such file or directory) at Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

7. Shell Programming and Scripting

Replace a word After a string.

Hello Gurus, I have a file which has foll contents scattered: ,TotUnasgndNetAmt FROM DEV_EIS_T.Wkly_SO_CCD_MOSumSnpsht WHERE CalDayRunDt = '2010-07-21' UNION ALL SELECT CalDayRunDt ,BusWkCd ,'N' I want to replace 2010-07-21 that starts after ' and ends before... (8 Replies)
Discussion started by: indrajit_u
8 Replies

8. Shell Programming and Scripting

Identifying a string from a set of files and printing to a new file

Dear All, I'm an amateur to writing scripts and need to do the following Need to read all files with a .log extension in a directory and identify the value for username i.e. all files have something like username = John. Once this is read, I need to print this value to a new file. The new file... (2 Replies)
Discussion started by: Kelly_B
2 Replies

9. UNIX for Dummies Questions & Answers

How to get the location of word in a string

How to use instr function in awk ? to get the location a) for example instr('productiondata_12','data',1) to get the location of data using awk. b) for example instr('sampledata_rev_12','rev',1) to get the location of data and reaplce with "org" using awk. can anyone help ... (3 Replies)
Discussion started by: Vrgurav
3 Replies

10. UNIX for Dummies Questions & Answers

How to get the n-th word in a string

Hi, Suppose I do this: $ wc file.txt 96 333 6629 file.txt and I want to get the 3rd word from the left (i.e. get the string 6629) and check its value. What do I do? Thanks (3 Replies)
Discussion started by: GMMike
3 Replies
Login or Register to Ask a Question

Featured Tech Videos