Checking for pattern and merging files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking for pattern and merging files
# 1  
Old 07-08-2014
Checking for pattern and merging files

I am trying to perform the following action.
1. A script runs the 'last' command for some users and prints the output to a file.
$ cat last_users.log
oracle pts/17 10.120.xxx.xxx Jun 28 14:42 - 18:01 (03:19)
oracle pts/11 10.120.xxx.xxx Jun 28 14:28 - 20:17 (5+05:49)
oracle pts/9 10.120.xxx.xxx Jun 28 00:11 - 14:02 (1+13:51)
oracle pts/13 10.120.xxx.xxx Jun 27 14:20 - 14:51 (7+00:31)
oracle pts/5 10.120.xxx.xx May 17 02:16 - 22:48 (5+20:32)
oracle pts/4 10.120.xxx.xx May 16 12:08 - 11:00 (2+22:52)

2. I have a file with some IPs and users associated with the IPs stored.
$ cat ip_list
10.120.xxx.xxx userA
10.120.xxx.xxx userB
10.120.xxx.xxx userC

3. I have to get the IP address from last_users.log, check if it exists in ip_list and get the username. The final output should be something like this

oracle pts/4 10.120.xxx.xx May 16 12:08 - 11:00 (2+22:52) userA

i.e. the username from ip_list should be merged with the last_users.log into a new file.

Thanks
# 2  
Old 07-08-2014
Code:
awk 'NR == FNR {a[$1] = $2; next}
  {$(NF+1) = a[$3]}1' ip_list last_users.log

This User Gave Thanks to SriniShoo For This Post:
# 3  
Old 07-08-2014
Thanks a lot ! It works...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging rows after matching a pattern

Hi All, I have the below file where I want the lines to merged based on a pattern. AFTER CMMILAOJ CMMILAAJ AFTER CMDROPEJ CMMIMVIJ CMMIRNTJ CMMIRNRJ CMMIRNWJ CMMIRNAJ CMMIRNDJ AFTER CMMIRNTJ CMMIRNRJ CMMIRNWJ (4 Replies)
Discussion started by: varun22486
4 Replies

2. Shell Programming and Scripting

Merging two files without any common pattern

Hi I have file1 as IJU_NSOMOW; SOWWOD_TWUIQ; and file2 as how are you?; fine there; Now my problem is i need the output file as IJU_NSOMOW; how are you?; SOWWOD_TWUIQ; fine there; (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. Shell Programming and Scripting

Merging two special character separated files based on pattern matching

Hi. I have 2 files of below format. File1 AA~1~STEVE~3.1~4.1~5.1 AA~2~DANIEL~3.2~4.2~5.2 BB~3~STEVE~3.3~4.3~5.3 BB~4~TIM~3.4~4.4~5.4 File 2 AA~STEVE~AA STEVE WORKS at AUTO COMPANY AA~DANIEL~AA DANIEL IS A ELECTRICIAN BB~STEVE~BB STEVE IS A COOK I want to match 1st and 3rd... (2 Replies)
Discussion started by: crypto87
2 Replies

4. Shell Programming and Scripting

Checking a pattern in file and the count of characters

I am having a zipped file which has the following URL contents - 98.70.217.222 - - "GET /liveupdate-aka.symantec.com/1340071490jtun_nav2k8enn09m25.m25?h=abcdefgh HTTP/1.1" 200 159229484 "-" "hBU1OhDsPXknMepDBJNScBj4BQcmUz5TwAAAAA" "-" In this line here is we only need to consider the... (4 Replies)
Discussion started by: Naks_Sh10
4 Replies

5. Shell Programming and Scripting

merging of 2 files in a particular pattern

how can i merge to files containing some random sort of numbers to a separate file .. file1 11111 10111 11011 file2 00000 01010 10101 file 3 11111_00000 10111_01010 11011_10101 Please let me know how to do this? (1 Reply)
Discussion started by: dll_fpga
1 Replies

6. Shell Programming and Scripting

merging of 2 consecutive lines in a file for a specific pattern

Hi , I'm looking for a way to merge two lines only for a given pattern / condition. Input : abcd/dad + -49.201 2.09 -49.5 34 ewrew rewtre * fdsgfds/dsgf/sdfdsfasdd + -4.30 0.62 -49.5 45 sdfdsf cvbbv * sdfds/retret/asdsaddsa + ... (1 Reply)
Discussion started by: novice_man
1 Replies

7. Shell Programming and Scripting

Find required files by pattern in xml files and the change the pattern on Linux

Hello, I need to find all *.xml files that matched by pattern on Linux. I need to have written the file name on the screen and then change the pattern in the file just was found. For instance. I can start the script with arguments for keyword and for value, i.e script.sh keyword... (1 Reply)
Discussion started by: yart
1 Replies

8. Shell Programming and Scripting

Help merging two files if search pattern true.

Hello everyone, I've been reading this forum whenever I had a problem with AWK but I can't seem to find how to solve my problem. What I would like to do is the following: I have a first file with two columns, on the first one is a certain name and in the second one, another corresponding... (4 Replies)
Discussion started by: Teroc
4 Replies

9. Shell Programming and Scripting

merging two files

Friends, os: redhat enterprise linux/SCO UNIX5.0 I have two files and I would like to merge on given key value. Now I have tried with join commd but it does not supporte multiple delimiters. and if records length is not fixed. join -a1 5 -a2 1 -t -o file1 file2 > outname Can any... (7 Replies)
Discussion started by: vakharia Mahesh
7 Replies

10. Shell Programming and Scripting

pattern checking

hi , i am having one file which is having the contents like this : "1111111111" "2222222222" "4444232344" ... i want to check if all the patterns in the file follows this sequence like starting with " ending with " inside that 10 digits... can anyone help me in checking that.. ... (5 Replies)
Discussion started by: kripssmart
5 Replies
Login or Register to Ask a Question