sed and awk to find pattern and add priffix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed and awk to find pattern and add priffix
# 1  
Old 08-23-2018
sed and awk to find pattern and add priffix

Original File

Code:
Server1|poweredOn|268401|[Z0109-GEN-SFCHT1-VMS-R001SR] Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit)

Need Output

Code:
Server1|poweredOn|DR|T1|268401|[Z0109-GEN-SFCHT1-VMS-R001SR] Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit)

Conduction to check find the string "SFCHT1" and "SR" and add prefix has "DR|T1"

Rule is if I find "SFCHT1" then this server is T1
and if it has "SR" then this server is T1 And DR

I used grep to find the and sed to replace

Code:
cat file.csv |grep "SFCHT1"|sed -e "s/poweredOn/poweredOn T1/" >> file2.csv
cat file2.csv |grep "SR"|sed -e "s/poweredOn/poweredOn DR/" >> file3.csv
cat file2.csv file3.csv > file4.csv

But i find duplicate output i need to keep only the line with DR and T1 prefix updated

Code:
Server1|poweredOn|T1|268401|[Z0109-GEN-SFCHT1-VMS-R001SR] Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit)
Server1|poweredOn|DR|T1|268401|[Z0109-GEN-SFCHT1-VMS-R001SR] Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit)

# 2  
Old 08-23-2018
Your request is not too specific; try and adapt if it doesn't fit:
Code:
sed -r '/SFCHT1.*SR/ s/^([^|]*\|){2}/&DR|T1|/' file
Server1|poweredOn|DR|T1|268401|[Z0109-GEN-SFCHT1-VMS-R001SR] Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit)

# 3  
Old 08-23-2018
Here is another solution. I'm thinking that SR and SFCHT1 should only be matched between [ and ]:

Code:
sed -e '/[[][^]]*SFCHT1[]-]/s/poweredOn/poweredOn|T1/' \
    -e '/[[][^]]*SR[]-]/s/poweredOn/poweredOn|DR/' file.csv


Example:

Code:
$ file.csv
Server1|poweredOn|268401|[Z0109-GEN-SFCHT1-VMS-R001SR] Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit)
Server2|poweredOn|268401|[Z0110-DSP-SFCHT1-VMS-R001SM] Not SR/Server1.vmx|Red Hat Enterprise Linux 3.2 (32-bit)
Server3|poweredOn|268401|[Z0109-XXX-PFCHT2-VMS-R001SM] Not SR or SFCHT1/Server1.vmx|Solaris 3.2

$ sed -e '/[[][^]]*SFCHT1[]-]/s/poweredOn/poweredOn|T1/' \
      -e '/[[][^]]*SR[]-]/s/poweredOn/poweredOn|DR/' file.csv
Server1|poweredOn|DR|T1|268401|[Z0109-GEN-SFCHT1-VMS-R001SR] Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit)
Server2|poweredOn|T1|268401|[Z0110-DSP-SFCHT1-VMS-R001SM] Not SR/Server1.vmx|Red Hat Enterprise Linux 3.2 (32-bit)
Server3|poweredOn|268401|[Z0109-XXX-PFCHT2-VMS-R001SM] Not SR or SFCHT1/Server1.vmx|Solaris 3.2

# 4  
Old 08-24-2018
Code:
perl -paF'\|' -e '$F[3] =~ /SFCHT1.+SR]/ and s/\|$F[2]/|DR|T1$&/' ranjancom2000.file

# 5  
Old 08-24-2018
thanks all i was using the

Code:
sort -u -K1,1

this one removing the duplicate host and keeping the host which has both "DR,H1". Need to know will there any bug using this filter for large file will it remove the host which "DR,H1" and keep only the host which has "H1"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk or sed to find a pattern that has lines before and after it

Dear gurus, Please help this beginner to write and understand the required script. I am looking for useing awk for sed. I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings. 1. Find a string, say user1. Then hash the line containing the... (6 Replies)
Discussion started by: ran_bon_78
6 Replies

2. 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="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

4. Shell Programming and Scripting

using sed to find a pattern

Hi All, I have a file with 100 lines contain a pattern like: input: a,b,4,2343,56456,7656,78787,786234,00:59:16.740 04 17 2012,4564,2453,556 f,j,er,45,fh,67,00:59:45.840 04 25 2012, sdf,grty,rtyr,wqerw output a,b,4,2343,56456,7656,78787,786234,00:59:16.7 04172012,4564,2453,556... (9 Replies)
Discussion started by: mimilaw
9 Replies

5. Shell Programming and Scripting

[Solved] Find duplicate and add pattern in sed/awk

<Update> I have the solution: sed 's/\{3\}/&;&;---;4/' The thread can be marked as solved! </Update> Hi There, I'm working on a script processing some data from a website into cvs format. There is only one final problem left I can't find a solution. I've processed my file... (0 Replies)
Discussion started by: lolworlds
0 Replies

6. Shell Programming and Scripting

awk to find pattern and add lines

My file goes like this: SID_LIST_HOSTNAME_LISTENER_3 = (SID_LIST = (SID_DESC = (SID_NAME = ORA0008) (ORACLE_HOME = /opt/oracle/product/ORA0008) (ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/ORA0008/lib") ) (SID_DESC = (SID_NAME = ORA0007) ... (4 Replies)
Discussion started by: jpsingh
4 Replies

7. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

8. UNIX for Advanced & Expert Users

Urgent Help required : awk/sed help to find pattern and delete till end of line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (1 Reply)
Discussion started by: rajan_san
1 Replies

9. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies

10. Shell Programming and Scripting

grep and sed to find a pattern and add newline

Hello All, I have log file the result from a multithreaded process. So when a process finishes it will write to this log file as 123 rows merged. The issue is sometimes the processess finish at the same time or write to the file at the same time as 123 rows merged.145 rows merged. At... (5 Replies)
Discussion started by: ssikhar
5 Replies
Login or Register to Ask a Question