Remove last word of a string?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove last word of a string?
# 1  
Old 02-24-2014
[solved] Remove last word of a string?


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:
[root@dell nas]# ls *
creds:  someserver

shares:  someserver.sharefolder
[root@dell nas]#

I want to fill 2 variables, $SERVER and $SHARE.
Obviously either '' or 'servername' should be in $SERVER, while 'sharename' should become $SHARE.

Maybe i could just use another symbol than a dot as filename seperator between $SERVER and $SHARE?
But i nether want to use '-' nor '_' as they might occour in a share- or servername (right?), while i'm not sure how a coma as part of a filename works out.

Please note, the relevant code blocks are the function and the 'else' part.
	GetServerShare() { # SERVER | SHARE
	# Pass either Server- or Sharename
	# Returns SERVER/SHARE (list if multiple)
		# Look if ITEM is SERVER
		servers="$(ls $CONFIG_CREDS|grep $1)" 
		# Look if ITEM is SHARE
		shares="$(ls $CONFIG_SHARES|grep $1)"

		# The grep \. should print out only server.share item,
		# given the assumption the user has set them up already,
		# while grep $1 will reduce the list to match provided argument/string
		output="$(printf $servers $shares |grep \.|grep $1)"
		for ITEM in $output;do
		    printf "$ITEM\n" # Need code here
....... other code ....
	else	if ( [ $mode = mount ] && [ ! "" = "$(echo $1|grep '/')" ] ) || \
			( [ ! $mode = mount ] && [ ! "" = "$(echo $2|grep '/')" ] )
		then	# Adress contains slashes, thus contains server & share info
			if [ ! $mode = mount ] #|| [ $doEdit ] || [ $doUnmount ]
			then	export	SERVER=$(printf "$2"|sed s,'/',' ',g|awk '{print $1}') \
					SHARE=$(printf "$2"|sed s,'/',' ',g|awk '{print $2}')
			else	export	SERVER=$(printf "$1"|sed s,'/',' ',g|awk '{print $1}') \
					SHARE=$(printf "$1"|sed s,'/',' ',g|awk '{print $2}')
		else	# Its just the IP/NAS or the Share name
			STR=$(GetServerShare $server_info)
			# This export needs to be changed too,
			# if i'd change the sed from / to '.' i'd get only 127 as SERVER,
			# but it would work if the servername was used (but would required changes at /etc/hosts
			export	SERVER=$(printf "$STR"|sed s,'/',' ',g|awk '{print $1}') \
				SHARE=$(printf "$STR"|sed s,'/',' ',g|awk '{print $2}')
			echo $SERVER # DEBUG - debug::
			echo $SHARE # DEBUG - debug:: EMPTY
		# Current 'fix' as the above 'else' in combination with export produces a trailing dot
		[ "${CONFIG_SHARE:${#CONFIG_SHARE}-1}" = "." ] && \

As of now, within that elseblock, i need to split up $SERVER ( and make SHARENAME the $SHARE variable, which is currently empty.

Any ideas?
Thank you in advance

Always right after posting an idea pops up Smilie
I could just replace the dots with spaces (using sed), and then check how many words there are, and use the last word (like for word in line,do SHARE=$word;done) to be used as Variable $SHARE
No better solution comes to mind.

---------- Post updated at 16:13 ---------- Previous update was at 15:35 ----------


The loop works, though its not elegant, but once all is working, the scripts needs another rewrite anyway.

Last edited by sea; 02-24-2014 at 12:15 PM..
# 2  
Old 02-24-2014
Assuming your STR will be something like this, you can use something like this




---------- Post updated at 07:51 AM ---------- Previous update was at 07:48 AM ----------

Or if it is STR=



This User Gave Thanks to ahamed101 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

How to remove everything after a word containing string?

Hello, I wish to remove any word coming after searched string found in a word. source*.txt #!bin/bash #test1 http://www.xx.yy http://www.11.22.44 #test2 http://www.01.yy #test3 http://www.22.qq.fc http://www.0x.yy... (15 Replies)
Discussion started by: baris35
15 Replies

2. Shell Programming and Scripting

Remove string perl with first or last word is in a list

Hello, I try to delete all strings if their first or last word is one of this list of words : "the", "i", "in", "there", "this", "with", "on", "we", "that", "of" For example if i have this string in an input file "with me" this string will be removed, Example: input "the european... (2 Replies)
Discussion started by: cyrine
2 Replies

3. Shell Programming and Scripting

How to remove first word?

Hi All, I want to remove the first word "cn=" from the below details. Only I want the number like 171345,174144... cn=171345 cn=174144 How can I achieve this. Please suggest. Thanks- P (6 Replies)
Discussion started by: pokhraj_d
6 Replies

4. Shell Programming and Scripting

Remove 1st word and _ from string

var=abc_cde_def_ghi_678.txt Expected output: cde_def_ghi_678.txt Is there a better way to achive this other than cut command? Basically, I need to remove the 1st word and _ from the string. Thanks. (1 Reply)
Discussion started by: vedanta
1 Replies

5. Shell Programming and Scripting

sed command to remove a word from string

Hello All, I am running a command find . -name amp.cfg | cut -c 3- which gives me output something like below rel/prod/amp.cfg rel/fld/amp.cfg deb/detail/amp.cfg deb/err/amp.cfg I want to remove trailing "/amp.cfg" so that i should get output something like... (7 Replies)
Discussion started by: anand.shah
7 Replies

6. Shell Programming and Scripting

want to remove last word.

Hi, I have a file which has the following /u12/data/oracle/abc.dbf /u12/data/oracle/def.dbf /u12/data/oracle/daf.dbf /u12/data/oracledb/fgh.dbf /u12/data/oracledb/fkh.dbf /u12/data/oracledb/kdq.dbf I want to do something like this /u12/data/oracle /u12/data/oracle... (7 Replies)
Discussion started by: javeedkaleem
7 Replies

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

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

9. Shell Programming and Scripting

Remove particular word from file

Hi All, If my file is: Wed Sep 9 22:45:14 EDT 2009 sftp> sftp> sftp> sftp> sftp> sftp> sftp> sftp> sftp> sftp> sftp> sftp> sftp> This is log file generated from transfer... sftp> sftp> sftp> sftp> Files placed properly.... sftp> sftp> sftp> How can I remove "sftp>" word from this... (4 Replies)
Discussion started by: darshakraut
4 Replies

10. Shell Programming and Scripting

how to remove first word

Hi, i have a question about to remove first word from a sentence. my script; #!/usr/bin/perl $msgtxt = "this is a test script"; my @ap_txtMsg = split(/ +/, trim_data($msgtxt)); $ap_msgtxt = splice (@ap_txtMsg, 0, 1); print $ap_msgtxt; but the output is first word that i... (1 Reply)
Discussion started by: malaysoul
1 Replies
Login or Register to Ask a Question