Create a file after replacing a particular pattern in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create a file after replacing a particular pattern in another file
# 1  
Old 03-20-2010
Java Create a file after replacing a particular pattern in another file

I have a file as below:
file1
-----
Code:
1|CT1909090TRYGH
2|CTH909090GHDGJ
4|CT9909090HSGUS
10|AT0735732YQGDJ
21|CTP909090BAFSL
100|BAI891253NSHDD

Now, I have to replace "909090" to "451234". Since after the pipe delimiter, the field positions are fixed, so i tried the below command:
Code:
awk '{FS="|"}; {print $2}' file1 | sed 's/^\(.\{3\}\)909090/\1451234/' > file2

Got the below value in file2
Code:
CT1451234TRYGH
CTH451234GHDGJ
CT9451234HSGUS
AT0735732YQGDJ
CTP451234BAFSL
BAI891253NSHDD

But I also need the value before pipe delimiter in file2.
Like:
Code:
1|CT1451234TRYGH
2|CTH451234GHDGJ
4|CT9451234HSGUS
10|AT0735732YQGDJ
21|CTP451234BAFSL
100|BAI891253NSHDD

Just the value should be replaced, keeping the complete file intact.
Thanks in advance.

Last edited by Franklin52; 03-20-2010 at 12:06 PM.. Reason: Please use code tags!
# 2  
Old 03-20-2010
Try:
Code:
awk -F"|" 'substr($2,4,6)==a{sub(a,b,$2)}1' a="909090" b="451234" OFS="|" file > file2

# 3  
Old 03-20-2010
Hi and welcome to the forums, PriyankaM:
Code:
sed '/^[^|]*|...909090/s/909090/451234/' file1 > file2

Cheers,
Alister
# 4  
Old 03-20-2010
Java

Hi Franklin,

Getting the following error:
% awk -F"|" 'substr($2,4,6)==a{sub(a,b,$2)}1' a="909090" b="451234" OFS="|" file1 > file2
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

I am comfortable with awk command than sed, so thought of using awk for the issue.

Thanks.
# 5  
Old 03-20-2010
Use nawk or /usr/xpg4/bin/awk on Solaris.
# 6  
Old 03-20-2010
Thanks franklin and alister for the quick reply
worked with nawk
nawk -F"|" 'substr($2,4,6)==a {sub(a,b,$2); print}' a="909090" b="451234" OFS="|" file1 > file2
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing 12 columns of one file by second file based on mapping in third file

i have a real data prod file with 80+ fields containing 1k -2k records. i have to extract say 12 columns out of this which are sensitive fields along with one primary key say SEQ_ID (like DOB,account no, name, SEQ_ID, govtid etc) in a lookup file. i have to replace these sensitive fields in... (11 Replies)
Discussion started by: megh12
11 Replies

2. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

3. Shell Programming and Scripting

Replacing Date in the file with Create date and timestamp

Hello, I have files that with a naming convention as shown below. Some of the files have dates in the file name and some of them don't have dates in the file name. imap-hp-import-20150917.txt imap-dell-gec-import-20150901.txt imap-cvs-import-20150915.txt imap-gec-import.txt... (8 Replies)
Discussion started by: Saanvi1
8 Replies

4. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

5. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

6. Shell Programming and Scripting

Need help in sed command ( Replacing a pattern inside a file with a variable value )

Hello, The following sed command is giving error sed: -e expression #1, char 13: unknown option to `s' The sed command is echo "//-----" | sed "s/\/\/---*/$parChk/g" where parChk="//---ee-" How can i print the variable value from sed command ? And is it possible to replace a... (2 Replies)
Discussion started by: frozensmilz
2 Replies

7. Shell Programming and Scripting

replacing a pattern in a file

Hi guys, i have a pattern that i am searching in a file and i want to extract some of this pattern ... module TS1N65ULPA96X32M4 ( .... i want to extract only TS1N65ULPA96X32M4 part and i do the following sed 's/module \(x*\).*/\1/' name_of_file but this is not quite right. could... (6 Replies)
Discussion started by: ROOZ
6 Replies

8. Shell Programming and Scripting

help with finding & replacing pattern in a file

Hi everyone. Could u be so kind and help me with on "simple" shell script? 1. i need to search a file line by line for a pattern. example of a lines in that file 2947 domain = feD,id = 00 0A 02 48 17 1E 1D 39 DE 00 0E 00,Name Values:snNo = f10 Add AttFlag = 0 2. i need to find... (0 Replies)
Discussion started by: dusoo
0 Replies

9. Shell Programming and Scripting

Replacing a paragraph between pattern , with the content 4m another file

hi, i wanted to put the output of file f1 into the pattern space of file f2 f1: wjwjwjwjwjwjwj //these line go in file f2 jwjwjwjwjwjjwjw wjwjwjwjjwjwjwj f2: Pattern_start __________ //these are the line to be replaced __________ Pattern_end i m... (4 Replies)
Discussion started by: go4desperado
4 Replies
Login or Register to Ask a Question