If pattern match in other column, modify column 3.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If pattern match in other column, modify column 3.
# 1  
Old 01-31-2019
If pattern match in other column, modify column 3.

My command sed will modify everything in column 3 if i will use the command below. I want to search for a pattern then modify everything in column 3.

Code:
sed -i 's/\|165\|/server1/g' file.txt

Code:
Input:
01-31-2019 19:14:05|device|165|1548962040165|5c5348f9-0804-1111|file_attach|7271|587|smtp|
01-31-2019 19:14:05|device|165|1548962040165|5c5348f9-0804-1234|file_attach|7271|587|smtp|
01-31-2019 19:14:05|device|165|1548962040028|5c5348f9-0804-6789|file_attach|7271|587|smtp|
01-31-2019 19:14:05|device|165|1548165040028|5c5348f9-0804-1111|file_attach|7271|587|smtp|

Output:
01-31-2019 19:14:05|device|server1|1548962040028|5c5348f9-3-4-0804-1111|file_attach|7271|587|smtp|
01-31-2019 19:14:05|device|165|1548962040028|5c5348f9-3-4-0804-1234|file_attach|7271|587|smtp|
01-31-2019 19:14:05|device|165|1548962040028|5c5348f9-5-6-0804-6789|file_attach|7271|587|smtp|
01-31-2019 19:14:05|device|server1|1548962040028|5c5348f9-1-2-0804-1111|file_attach|7271|587|smtp|

# 2  
Old 01-31-2019
how about to start with - you may have to make slight adjustments based your more detailed requirements:
Code:
awk -F'|' '$5~(pat "$"){$3=ser}1' pat=1111 ser=server1 OFS='|' myFile

These 2 Users Gave Thanks to vgersh99 For This Post:
# 3  
Old 01-31-2019
You may pimp up your own approach by adding an address specification. Be aware that it will NOT change the third field if that doesn't contain 165, or change any other field that holds that value.

Code:
sed -r '/([^|]*\|){4}[^|]*1111\|/ s/\|165\|/|server1|/' file
01-31-2019 19:14:05|device|server1|1548962040165|5c5348f9-0804-1111|file_attach|7271|587|smtp|
01-31-2019 19:14:05|device|165|1548962040165|5c5348f9-0804-1234|file_attach|7271|587|smtp|
01-31-2019 19:14:05|device|165|1548962040028|5c5348f9-0804-6789|file_attach|7271|587|smtp|
 01-31-2019 19:14:05|device|server1|1548165040028|5c5348f9-0804-1111|file_attach|7271|587|smtp|

EDIT: If you want to replace field 3 regardless of its contents, try

Code:
sed -r '/([^|]*\|){4}[^|]*1111\|/ s/(([^|]*\|){2})[^|]*\|/\1server1|/' file


P.S.: you might want to adapt the title of your thread.

Last edited by RudiC; 01-31-2019 at 04:21 PM..
These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 01-31-2019
Maybe I misunderstood something. Here are my drop
Code:
sed '/-1111|/ s/|165|/|server1|/' file

--- Post updated at 23:34 ---

You can be safe as it is. I think that will be enough
Code:
sed '/-1111|/ s/e|165|/e|server1|/' file

This User Gave Thanks to nezabudka For This Post:
# 5  
Old 02-01-2019
And I understood, the whole thing is exactly the coincidence of the fields Smilie
Code:
sed -r 's/(([^|]*\|){2})165((\|[^|]*){2}1111)/\1server1\3/' file

This User Gave Thanks to nezabudka For This Post:
# 6  
Old 02-04-2019
Hi Neza,
care to explain the solution, character by character?
Thank you

--- Post updated at 12:28 AM ---

HI Vgersh,
care to explain the solution, character by character?
Thank you

--- Post updated at 12:28 AM ---

Hi Rudi,
care to explain the solution, character by character?
Thank you

--- Post updated at 12:29 AM ---

Hi Rudi,
care to explain the solution, character by character?
Thank you
# 7  
Old 02-04-2019
Now, this might become lengthy and tedious. Some hints, maybe, c.f. man sed for further reference / explanation:


Code:
sed -r '/([^|]*\|){4}[^|]*1111\|/ s/\|165\|/|server1|/' file
sed -r '/([^|]*\|){4}[^|]*1111\|/ s/(([^|]*\|){2})[^|]*\|/\1server1|/' file

-r : switch sed to use EREs
{...}: "bound"; repetition factor of preceding expression
(...): "atom", also "parenthesized subexpression"; to be used by "back reference"
[...]: "bracket expression"
\| : escaped pipe, to be taken as a literal character, as opposed to the alternate operator |
\1 : "back reference".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

2. Shell Programming and Scripting

Delete line by pattern match in column

file: 1|12322|tow| 5|23422|pow| 6|23423|cow| 3|34324|how| deletelines: 12322 23423 My command to delete line while read NUM do awk -F"\|" '$2 !~ /`"$NUM"`/' file >file.back mv file.back file done<deletelines (3 Replies)
Discussion started by: Roozo
3 Replies

3. Shell Programming and Scripting

Perl removing line match with pattern in column

Hi, I have log like this: ... (1 Reply)
Discussion started by: justbow
1 Replies

4. Shell Programming and Scripting

Perl removing line match with pattern in column

Hi, I have log like this: ... (1 Reply)
Discussion started by: justbow
1 Replies

5. UNIX for Dummies Questions & Answers

Match sum of values in each column with the corresponding column value present in trailer record

Hi All, I have a requirement where I need to find sum of values from column D through O present in a CSV file and check whether the sum of each Individual column matches with the value present for that corresponding column present in the trailer record. For example, let's assume for column D... (9 Replies)
Discussion started by: tpk
9 Replies

6. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

7. Shell Programming and Scripting

awk pattern match and count unique in column

Hi all I have a need of searching some pattern in file by month and then count unique records D11 G11 R11 -------> Pattern available in file S11 Jan$1 to $5 column contains some records in which I want to find unique for this purpose I have written script like below awk '/Jan/ ||... (4 Replies)
Discussion started by: nex_asp
4 Replies

8. Shell Programming and Scripting

Adding Column Values Using Pattern Match

Hi All, I have a file with data as below: A,FILE1_MYFILE_20130309_1038,80,25.60 B,FILE1_MYFILE_20130309_1038,24290,18543.38 C,FILE1_dsc_dlk_MYFILE_20130309_1038,3,10.10 A,FILE2_MYFILE_20130310_1039,85,110.10 B,FILE2_MYFILE_20130310_1039,10,12.10... (10 Replies)
Discussion started by: angshuman
10 Replies

9. Shell Programming and Scripting

Awk or Sed, fubd match in column, then edit column.

FILE A: 9780743551526,(Abridged) 9780743551779,(Unabridged) 9780743582469,(Abridged) 9780743582483,(Unabridged) 9780743563468,(Abridged) 9780743563475,(Unabridged) FILE B: c3saCandyland 9780743518321 "CANDYLAND" "MCBAIN, ED" 2001 c3sbCandyland 9780743518321 ... (7 Replies)
Discussion started by: glev2005
7 Replies

10. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies
Login or Register to Ask a Question