Return alias with pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Return alias with pattern
# 8  
Old 02-13-2015
Try this
Code:
awk     'FNR==NR        {T[$1]; next}
         FNR==1         {FILE++}
         FILE==1        {if ($1 in T) ID[$2]
                         next}
         $2 in ID &&
         $1 ~ "^" PAT    # {print; delete ID[$2]}
        ' PAT="ab"  list lookup lookup
ab1 1 
ab3 1
ab2 2
ab31 2

Remove the # before the last action to achieve an output like
Code:
ab3 1
ab2 2

This User Gave Thanks to RudiC For This Post:
# 9  
Old 02-13-2015
You could also try something like this:
Code:
#!/bin/ksh
awk -v abbrev="${2:-ab}" '
# Process 1st file (lookup.txt)...
FNR == NR {
	if(NR > 1) {	# Skip over heading in file lookup.txt.
		# Build the arrays a[abbreviation, id] = name for the 1st
		# abbreviation found for a given id, and n2id[name] = id for all
		# names.
		if(!(((ab = substr($1, 1, match($1, /[[:digit:]]/) - 1)), $2) in a))
			a[ab, $2] = $1
		n2id[$1] = $2
#printf("in=%s, n2id[%s]=%s, a[%s,%s]=%s\n", $0, $1, n2id[$1], ab, $2, a[ab,$2])
	}
	next
}
# Process 2nd file (1st script operand)...
(abbrev, n2id[$1]) in a {
	$1 = a[abbrev, n2id[$1]]
}
# Print original or converted input line...
1' lookup.txt "${1:-list.txt}"

This was written and tested using the Korn shell, but will also work with any other shell that performs POSIX standard variable expansions (such as bash, ash, or dash).

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

It is intended to be invoked with two operands, but if only one operand (specifying the list file to be used) is provided, the abbreviation defaults to "ab"; and if no operands are provided, the list file defaults to "list.txt" and the abreviation defaults to "ab".

(If you uncomment the printf statement, you can watch it build the arrays it will be using as it reads lookup.txt.)

With your sample input, the output produced when it is invoked as:
Code:
./lookup.sh list.txt ab
        or
./lookup.sh

is:
Code:
ab1
ab2
er3

and when invoked as:
Code:
./lookup.sh list.txt fgh

it produces the output:
Code:
fgh1
fg21
fgh5

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

Pattern match exclusive return pattern/variable

I have an application(Minecraft Server) that generates a logfile live. Using Crontab and screen I send a 'list' command every minute. Sample Log view: 2013-06-07 19:14:37 <Willrocksyea1> hello* 2013-06-07 19:14:41 <Gromden29> hey 2013-06-07 19:14:42 Gromden29 lost connection:... (1 Reply)
Discussion started by: gatekeeper258
1 Replies

2. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

3. Shell Programming and Scripting

Suppressing carriage return in bash alias

I'd like to create an alias that displays my string but leaves my cursor at the end. Not seeing any examples of this. One indirect way might be to preload or stuff the history buffer, so I just hit up arrow. (2 Replies)
Discussion started by: tns1
2 Replies

4. Shell Programming and Scripting

Grep command to return all the lines between one matched pattern to another.

14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. 14:50:00- abcdefghijkl. 123456789. 15:30:00-abcdefghijkl. (3 Replies)
Discussion started by: dev_shivv
3 Replies

5. Solaris

Grep command to return all the lines from one matched pattern to another.

For example a log file looks like below- 13:30:00- abcdefghijklhjghjghjhskj. abcdefghijkl. 14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. (0 Replies)
Discussion started by: dev_shivv
0 Replies

6. Shell Programming and Scripting

How to add a return after a special pattern?

Hello I'm new to Solaris I have a file have things like these PATTERN12345678 PATTERN12345678PATTERN87654321 PATTERN12345678 PATTERN87654321 i hope i could change to this format PATTERN12345678 PATTERN12345678 PATTERN87654321 PATTERN12345678 PATTERN87654321 (4 Replies)
Discussion started by: beterhans
4 Replies

7. UNIX for Dummies Questions & Answers

Search specific pattern in file and return number of occurence

Hi I want to search for a specific pattern in file Say ABC;HELLO_UNIX_WORLD;PQR ABC;HELLO_UNIX_WORLD_IS_NOT_ENOUGH;XYZ ABC;HELLO_UNIX_FORUM;LMN Pattern to search is : "HELLO_UNIX_*****" and not "HELLO_UNIX_***_***_" I mean after "HELLO_UNIX" there can only be one word.In this case... (2 Replies)
Discussion started by: dashing201
2 Replies

8. Shell Programming and Scripting

Perl help - Searching for a pattern and return the position

Hi, I need to search a file, in each line I need to check for occurance of '1' from a particular position through the next 32 bytes. If 1 is found, i need to return the position. Here is an example of the file and the output i need. Please help. I'm new to perl and unix. File: ... (1 Reply)
Discussion started by: gpaulose
1 Replies

9. UNIX for Dummies Questions & Answers

return previous line for pattern match

Hi, Need some idea on file processing, I have file like below, Processing al sources ... ...No value found : CHECK. Completed comparing all sources. Comparing schedulers... Processing al targets ... ...No value found : From above I need to extract the line where "No value... (4 Replies)
Discussion started by: braindrain
4 Replies

10. Shell Programming and Scripting

Help with pattern search and return

I would like to write a script which will read a file containing a list of filenames of the format as shown below : /usr/local/packages/runcmdlinetool /home/john.doe/sdfsdf/sdfsdfsd/sdfsdf/sdfsdfTemplates.xml /usr/local/bin/gtar... (4 Replies)
Discussion started by: inditopgun
4 Replies
Login or Register to Ask a Question