Find pattern suffix matching pattern

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find pattern suffix matching pattern
# 1  
Old 10-24-2013
Find pattern suffix matching pattern


I am trying to get a result out of this but fails please help. Have two files /tmp/1 & /tmp/hosts. 



I want this result in /tmp/hosts if hostname is already there dont want duplicate entry.

This is wrong
123.456.789.01 abcdefgh.was.tb.dsdc abcdefgh.was.tb.dsdc

This is correct
123.456.789.01 abcdefgh.was.tb.dsdc

Trying with this:

if grep abcdefgh.was.tb.dsdc /tmp/hosts; then
. /tmp/1 && sed -i "s/^$IP\(.*\)$/$IP\1 $WAS_HOSTNAME/" /tmp/hosts;

Thanks in advance

Last edited by rajeshwebspere; 10-24-2013 at 05:14 PM.. Reason: Add CODE tags.
# 2  
Old 10-24-2013
Try using awk:
awk -F= 'NR==FNR{I[$1];next}$2 in I{v=$2;getline;print v,$2}' /tmp/hosts /tmp/1

This User Gave Thanks to Yoda For This Post:
# 3  
Old 10-24-2013
Re Find pattern suffix matching pattern

Hi Yoda,

Thanks for the reply. I was wondering if the same file can be updated with the actual result. Here the /tmp/hosts file should be updated.

# 4  
Old 10-24-2013
You could redirect the output of awk program to a tmp file and rename it back to original.
awk ... > /tmp/$$; mv /tmp/$$ /tmp/hosts

This User Gave Thanks to Yoda For This Post:
# 5  
Old 10-25-2013
Re: Find pattern suffix matching pattern

Hi Yoda,

I tried that, when for first time i run this it writes and moves the content required. But, when i run it second time it creates empty redirect file, as nothing is there to write and the same is moved to original file. So, getting an empty file on second attempt.

cp -p /tmp/hosts /tmp/; awk -F= 'NR==FNR{I[$1];next}$2 in I{v=$2;getline;print v,$2}' /tmp/hosts /tmp/1 > /tmp/$$; mv /tmp/$$ /tmp/hosts

Please suggest.


# 6  
Old 10-25-2013
Could the process id change between two statements?

Try using a file with fixed file name such as /tmp/hosts.tmp instead of /tmp/$$
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. UNIX for Dummies Questions & Answers

find Search - Find files not matching a pattern

Hello all, this is my first and probably not my last question around here. I do hope you can help or at least point me in the right direction. My question is as follows, I need to find files and possible folders which are not owner = AAA group = BBB with a said location and all sub folders ... (7 Replies)
Discussion started by: kilobyter
7 Replies

6. Shell Programming and Scripting

Find all matching words in text according to pattern

Hello dear Unix shell professionals, I am desperately trying to get a seemingly simple logic to work. I need to extract words from a text line and save them in an array. The text can look anything like that: aaaaaaa${important}xxxxxxxx${important2}ooooooo${importantstring3}...I am handicapped... (5 Replies)
Discussion started by: Grünspanix
5 Replies

7. UNIX for Dummies Questions & Answers

find files NOT matching name pattern

Hi, I have following files in my directory: /TESTDONTDEL> ls -alt total 14 drwxr-xr-x 2 oracle dba 1024 May 15 06:30 . -rw-r--r-- 1 oracle dba 40 May 15 06:30 exception.txt -rw-r--r-- 1 oracle dba 19 May 15 06:22 ful_1234_test1.txt -rw-r--r-- 1... (2 Replies)
Discussion started by: sagarparadkar
2 Replies

8. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

9. UNIX for Dummies Questions & Answers

Find files matching a pattern

Hi, I am writing a BASH shell script. I would like to count all the files in the CURRENT directory matching a specific pattern. Could someone suggest the best/simplest way to do this. I have thought of these solutions (for simplicity the pattern is all files starting with A): ls -1 *A | wc -l... (5 Replies)
Discussion started by: msb65
5 Replies

10. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444||015236433 0233654|122555||023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444||015236433 0233654|122555||023365433... (4 Replies)
Discussion started by: imas
4 Replies
Login or Register to Ask a Question