How to find last delimiter in line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find last delimiter in line?
# 1  
Old 03-13-2008
How to find last delimiter in line?

I am working in a ksh script.

I am reading a login, password, and database name from a pre-existing config file. Login and password are simple, I take the value after the first "=" sign, but the dbname has multiple equal signs in it. I have it working by temporarily reading the 23rd field, but the number of fields may vary in different environments depending on the number of servers. How can I identify the last delimiter and take the value after that?

Here is what I have now:

Code:
for LINE in `cat /config/db.properties|tr -d " )\r"`
do
        VALUE=`echo $LINE | cut -f2 -d=`
        KEY=`echo $LINE | cut -f1 -d=`
        case $KEY in
                ORACLE_USER_ID) 
                      USERNAME=$VALUE;;
                ORACLE_PASSWORD)
                    PASSWORD=$VALUE;;
                ORACLE_URL)
                    DBNAME=`echo $LINE | cut -f23 -d=`;;
        esac
done

echo "  You are $USERNAME.\n  Your password is $PASSWORD.\n  You're logging on "
echo "to $DBNAME.\n"

My input is after removing carriage returns, spaces, and right parens is:

ORACLE_USER_ID=user12
ORACLE_PASSWORD=pswd12
ORACLE_URL=ORACLE_URL=jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP(HOST=server1(PORT=1111(ADDRESS=(PROTOCOL=TCP(HOST=server2(PORT=2222(ADDRESS=(PROTOCOL=TCP(HOST=server3(PORT=3333(ADDRESS=(PROTOCOL=TCP(HOST=server4(PORT=4444(LOAD_BALANCE=yes(CONNECT_DATA=(SERVER=DEDICATED(SERVICE_NAME=oradb1

Output is:
Code:
You are user12.
  Your password is pswd12.
  You're logging on y\to oradb1.

# 2  
Old 03-13-2008
With awk you can get the last field as follow:
Code:
DBNAME=`echo $LINE | awk -F= '{print NF}'`

Regards
# 3  
Old 03-13-2008
Thanks Franklin. I added a $ in front of the NF and it worked perfect!

Code:
DBNAME=`echo $LINE | awk -F= '{print $NF}'`

# 4  
Old 03-13-2008
Quote:
Originally Posted by prismtx
Thanks Franklin. I added a $ in front of the NF and it worked perfect!

DBNAME=`echo $LINE | awk -F= '{print $NF}'`
O my, I forgot the $ sign, but glad to know you solve your problem.

Regards
# 5  
Old 03-13-2008
You can do the same with sed too:

Code:
print - $string | sed 's/.*=//'

This works because regular expressions are by default "greedy": the regexp engine tries to match as much as possible. In this case it matches every occurence of "=" up to the last one, so that ".*=" (any number of any character followed by an equal sign) is still fulfilled.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to identify delimiter to find and replace a string with sed?

I need to find and replace a date format in a SQL script with sed. The original lines are like this: ep.begin_date, ep.end_date, ep.facility_code, AND ep.begin_date <= '01-JUL-2019' ep.begin_date, ep.end_date, ep.facility_code, AND ... (15 Replies)
Discussion started by: duke0001
15 Replies

2. Shell Programming and Scripting

Need next line as a space delimiter in awk

Hi,Below is the output for p3fi_dev services 1/app/oracle> . ./oraprofile_p3fi_dev p3fi_dev_01 (P):/devoragridcn_01/app/oracle> srvctl config service -d p3fi_dev p3fi_p3fi_dev.world PREF: p3fi_dev_01 AVAIL: p3fi_dev_02 pplnet_p3fidev PREF: p3fi_dev_01 AVAIL: p3fi_dev_02 nexus_p3fidev PREF:... (3 Replies)
Discussion started by: Vishal_dba
3 Replies

3. Shell Programming and Scripting

Cutting a part of line till delimiter

here are the few scenarios... isoSizeKB text NOT NULL, reserved1 varchar(255), KEY `deviceId` (`deviceId`) `d5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `dHead` enum('HistoryInfo','Diversion') COLLATE utf8_unicode_ci, `ePR` int(11) DEFAULT '0', PRIMARY KEY (`id`) ... (7 Replies)
Discussion started by: vivek d r
7 Replies

4. UNIX for Dummies Questions & Answers

Find delimiter and double quote the field

Hi I have a asterisk (*) delimited file and there are some fields which contain data having asterisk , now i want to double quote the fileds which contain data with asterisk Ex: input file ID*NAME*EMAIL 1*BILL*BILL@AOL.com 2*J*OY*JOY@msn.com in the 2nd record JOY has a asterisk value in... (11 Replies)
Discussion started by: halmstad
11 Replies

5. Shell Programming and Scripting

how to find the nth field value in delimiter file in unix using awk

Hi All, I wanted to find 200th field value in delimiter file using awk.? awk '{print $200}' inputfile I am getting error message :- awk: The field 200 must be in the range 0 to 199. The source line number is 1. The error context is {print >>> $200 <<< } using... (4 Replies)
Discussion started by: Jairaj
4 Replies

6. Shell Programming and Scripting

Find Word within ^A delimiter

I have a file in which the following pattern is there TAG001^A<value>^A I want to find all such values(words) which comes right next to "TAG001^A" and before the next "^A". ^A is the delimiter here. Please help! Note: I think ^A in unix resolves to \001 as delimiter (7 Replies)
Discussion started by: royzlife
7 Replies

7. Shell Programming and Scripting

How to find the Delimiter?

Hi All, Is there any method we can use to find what is the delimiter used in a text file, asuming the files has fixed number of colomns. Thanks in advance. Js (9 Replies)
Discussion started by: jisha
9 Replies

8. Shell Programming and Scripting

Delimiter Count Line by Line

Hi All, I have totally 10 source files. I want to count the delimiter In my source files line by line and need to store the result in another file. I got the output for the total count of delimiter for one file. But I am struggling to get the delimiter count line by line for each my files. Plz... (6 Replies)
Discussion started by: suresh01_apk
6 Replies

9. UNIX for Dummies Questions & Answers

Cut a field from a line having quotes as delimiter

Hi , I have extract a single field from the 2nd row of a file of which the format is as given below "Field1","Field2","Field3",...,"Field17",... I need to cut Field17 as such (no quotes required).. I give the command head -2 file_name | tail -1 | cut -d "," -f17 But the output is... (2 Replies)
Discussion started by: nivin_govindan
2 Replies

10. Shell Programming and Scripting

extract string from varying delimiter line

Hi I have lines like this a=1, b=2, c=3, a=1, d=4, e=5, b=225, I need to extract the b=nnn... value. I dont know how many other entries will be before and after it in each line. Ive tried a basic line like awk '/b=/, $NF ~ /,/ ' myfile.txt but I think that it doesnt care which comma it... (5 Replies)
Discussion started by: rebelbuttmunch
5 Replies
Login or Register to Ask a Question