How to find previous string based on an input string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find previous string based on an input string?
# 1  
Old 02-23-2016
How to find previous string based on an input string?

Hi,

I did some research but cannot find the right solution so hopefully someone can help me here.


I have a long string format like:

Code:
VAR=111:aaaa,222:bbb,333:ccc

it could be
Code:
VAR=111:aaa,222:bbb,333:ccc,444:ddd, etc

what I looking for is eg.

if I give ccc, it will return me 333

if aaa, return 111.

a string before :$INPUT

Last edited by Scrutinizer; 02-23-2016 at 06:37 PM.. Reason: code tags
# 2  
Old 02-23-2016
Hi, try (using bash or ksh93) :

Code:
while IFS=: read val key
do
  if [ "$key" = "$INPUT" ]; then
    echo "$val"
  fi
done <<< "${VAR//,/$'\n'}"



Code:
$ INPUT=aaa
$ while IFS=: read val key; do   if [ "$key" = "$INPUT" ]; then     echo "$val";   fi; done <<< "${VAR//,/$'\n'}"
111
$ INPUT=bbb
$ while IFS=: read val key; do   if [ "$key" = "$INPUT" ]; then     echo "$val";   fi; done <<< "${VAR//,/$'\n'}"
222

# 3  
Old 02-23-2016
that works perfectly!
thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string based on input

I am trying to read a value from a mapping file and would need to replace the value based on country parameter source_table_@ctry_final Expected final_var=source_table_aus_final If the country is in nz,usa,uk then final_var=diff_table_nz_final final_var=diff_table_usa_final like that... (10 Replies)
Discussion started by: Master_Mind
10 Replies

2. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies

3. Shell Programming and Scripting

UNIX Scripting help to input string and search a file to find

Hi Don, this is not homework question. I work for a Credit card company and my development goal this year is to learn Unix. I would love if others can help me get started, thanks. Hi everyone I am new to Unix and need help writing a script that can ask user for an input, then search that input... (2 Replies)
Discussion started by: 12ic11
2 Replies

4. Shell Programming and Scripting

UNIX Scripting help to input string and search a file to find

Hi everyone, I am new to Unix and need help writing a script that can ask user for an input, then search that input within a file I know will have to use the read and grep commands, anyone can give me somewhere to start would help Task: Write a script to display which volume pool a given... (1 Reply)
Discussion started by: 12ic11
1 Replies

5. UNIX for Dummies Questions & Answers

UNIX Scripting help to input string and search a file to find

Hi everyone, I am new to Unix and need help writing a script that can ask user for an input, then search that input within a file I know will have to use the read and grep commands, anyone can give me somewhere to start would help Task: Write a script to display... (1 Reply)
Discussion started by: 12ic11
1 Replies

6. Shell Programming and Scripting

How to pad zeroes based on input string?

Hello everyone, I am comparing two floating point numbers by storing them in seperate files and then using difference command to verify,it is working fine. But I want to compare two values which will come at 4 precision places. ex: file1 Date,Count,Checksum... (7 Replies)
Discussion started by: karthik adiga
7 Replies

7. Shell Programming and Scripting

Find and replace string based on entries on another file

I have a file1 with different with multiple fields and records File2 has 2 fields. I want to find and replace strings in file1 based on file2 values (I Want an exact match i.e. for example: when searching for DT:3, Substr of DT:34 should not be matched) File2: DT:3 foo_err DT:34 bar_frr... (8 Replies)
Discussion started by: aydj
8 Replies

8. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

9. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

10. Shell Programming and Scripting

input a string and copy lines from a file with that string on it

i have a file1 with many lines. i have a script that will let me input a string. for example, APPLE. what i need to do is to copy all lines from file1 where i can find APPLE or any string that i specify and paste in on file 2 thanks in advance! (4 Replies)
Discussion started by: engr.jay
4 Replies
Login or Register to Ask a Question