How to get next string of a matching string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get next string of a matching string?
# 1  
Old 10-23-2014
How to get next string of a matching string?

Code:
fruits="apple grape orange jack lemon"

I want the element present next to "orange". i.e. jack
Note: The position of orange is not fixed.

Code:
fruits="apple grape orange jack lemon"

echo $fruits | awk '{ for ( n=1; n<=NF; n++ ) if($n ~ my_string) print $n }' my_string="orange"

This will print "orange" but i want to print "jack"

Code:
 print {$n+1}

is throwing error

Im not restricted to awk. Any other approach is appreciated.
Thanks.
# 2  
Old 10-23-2014
Code:
fruits="apple grape orange jack lemon"
echo $fruits | awk '{ for ( n=1; n<=NF; n++ ) if($n == my_string) print $(n+1)  }' my_string="orange"
jack

---------- Post updated at 06:25 PM ---------- Previous update was at 06:19 PM ----------

OR like this

Code:
fruits="apple grape orange jack lemon"
echo $fruits | awk '$1==my_string{ n=FNR+1 }FNR==n' RS=" " my_string="orange"
jack

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 10-23-2014
With a recent ksh or bash, you could also try using arrays in the shell:
Code:
#!/bin/ksh
fruits=(apple "honeydew melon" grape orange jack lemon)
for i in "$@"
do	for (( f=0; f < ${#fruits[@]}; f++))
	do	if [ "${fruits[f]}" = "$i" ]
		then	printf 'Fruit after %s is %s\n' "$i" \
				"${fruits[(f + 1) % ${#fruits[@]}]}"
			break
		fi
	done
	if [ $f -ge ${#fruits[@]} ]
	then	printf 'Fruit %s not found\n' "$i"
	fi
done

which when invoked with the operands:
Code:
apple jack lemon pear

produces the output:
Code:
Fruit after apple is honeydew melon
Fruit after jack is lemon
Fruit after lemon is apple
Fruit pear not found

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Matching string

Hello all, i am trying to match a string and based on that proceed with my script or error out... i have a file called /tmp/sta.log that will be populated by oracle's spooling..it can have a output of either 2 of the below (OPEN or errors/ORACLE not avaiable) $ cat /tmp/sta.log OPEN $ $... (2 Replies)
Discussion started by: abdul.irfan2
2 Replies

2. Shell Programming and Scripting

String matching using awk

Hello, I am working with google ngram data set which is of size 100s of gb. Before using it with Java, I wanted to filter it out using shell script. Here is a sample line in the file: 2.55 1.57 1992 10 20 30 The first two fields (2.55 and 1.57) are... (3 Replies)
Discussion started by: shekhar2010us
3 Replies

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

4. Shell Programming and Scripting

Matching string from input to string of file

Hi, i want to know how to compare string of file with input string im trying following code: file_no=`paste -s -d "||||\n" a.txt | cut -c 1` #it will return collection number from file echo "enter number" read " curr_no" if ; then echo " current number already present" fi ... (4 Replies)
Discussion started by: a_smith
4 Replies

5. Shell Programming and Scripting

String matching

I have a string like ab or abc of whatever length. But i want to know whether another string ( for example, abcfghijkl, OR a<space> bcfghijkl ab<space> cfghijkl OR a<space>bcfghijkl OR ab<space> c<space> fghijkl ) starts with ab or abc... space might existing on the longer string... If so, i... (4 Replies)
Discussion started by: nram_krishna@ya
4 Replies

6. Shell Programming and Scripting

Matching 2 items in a string

Little lost here, I am trying to search a line for both values after the $ signs. My ultimate goal is to get percertage. <?php $string = "Something on sale for $4 and orginal price $10"; $strstr =. strstr($string, '$'); $strrchr =. strrchr($string, '$'); echo "$strstr<br>"; echo... (1 Reply)
Discussion started by: mrlayance
1 Replies

7. Shell Programming and Scripting

matching a string

I have a requirement of shell script where i need to read the File name i.e ls -t | head -1 and Match that Filename with some delimited values which are in a separate File. For Example i am reading the File name i.e (ls -t | head -1) after that i need to read one more sequential file which... (2 Replies)
Discussion started by: dsdev_123
2 Replies

8. UNIX for Dummies Questions & Answers

Matching string

Hello, i have a program where i have to get a character from the user and check it against the word i have and then replace the character in a blank at the same position it is in the word. (7 Replies)
Discussion started by: nehaquick
7 Replies

9. Shell Programming and Scripting

String matching

for a certain directory, I want to grep a particular file called ABCD so what I do is ls /my/dir | grep -i "ABCD" | awk '{print $9}' however, there is also this file called ABCDEFG, the above command would reurn both file when I only want ABCD, please help! (3 Replies)
Discussion started by: mpang_
3 Replies

10. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies
Login or Register to Ask a Question