Need to find and replace in a file

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Need to find and replace in a file
# 1  
Old 05-19-2017
Need to find and replace in a file

Hi All,

I am having below sample data in a file.
I need to find all the line form this file with word ABC and i need to replace the characters at position 120 which is "CO:BOGFDUI"(30chars) in the lines with blank space.
I have tried using grep to find the word with ABC (grep ABC filename), but i am unable to substite the word at this position.
Could you please help me in this.

file data:
Code:
20840GFC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BASAGIO
20840GFC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BRGRUUR
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BTGFGFG
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BGGFEWE
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BIGFGFG
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BOGFDUI
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BPGFDSY
20840XYZ00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BUGFGFG
20840XYZ00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BTGFFSF

output file data:
Code:
20840GFC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BASAGIO
20840GFC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BRGRUUR
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737
20840XYZ00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BUGFGFG
20840XYZ00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BTGFFSF

# 2  
Old 05-19-2017
That specification is not too consistent: It's not clear if any ABC pattern should be matched, or just the one at positions 6 - 8. There's no 120th char position in the input sample, and esp. not 30 onwards. The replacement as well is not 30 char but 10 chars long. Will it always be the rest of the line to be replaced?
To simulate your sample output, try (GNU sed)
Code:
sed '/ABC/ s/./ /94g' file
20840GFC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BASAGIO
20840GFC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BRGRUUR
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737          
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737          
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737          
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737          
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737          
20840XYZ00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BUGFGFG
20840XYZ00524096100000LAU1LDNUSD CR       6.322017041120170411              00016574200165737CO:BTGFFSF

or
Code:
awk '/ABC/ {print substr ($0, 1, 93); next} 1' file

# 3  
Old 05-21-2017
Thank you RudiC.
The ABC pattern can be any where in the file. Please find he corrected data:
Code:
20840GFC00524096100000LAU1LDNUSD CR       6.322017041120170411                       						  	       00016574200165737CO:BASAGIOSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840GFC00524096100000LAU1LDNUSD CR       6.322017041120170411             								       00016574200165737CO:BRGRUURSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              								       00016574200165737CO:BTGFGFGSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411           								       00016574200165737CO:BGGFEWESDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411                       						  	       00016574200165737CO:BOGFDUISDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411                       						  	       00016574200165737CO:BPGFDSYSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840XYZ00524096100000LAU1LDNUSD CR       6.322017041120170411                       						  	       00016574200165737CO:BUGFGFGSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840XYZ00524096100000LAU1LDNUSD CR       6.322017041120170411                       						  	       00016574200165737CO:BTGFFSFSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411            								       00016574200165737CO:BIGFGFGSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD

I have tried using the awk, it replaces the pattern, but also i need other lines from the file which is not getting displayed.
I have tried using below. Could you please let me know what is next and 1 in the command are. I have also tried using sed which is not working due to syntax error. i have tried correcting it but having the same issue.
awk '/ABC/ {print substr ($0, 1, 119);}' file
# 4  
Old 05-21-2017
Hi abhi_123,
Your data is not consistent. It has spaces and tabs mixed in. ^I is how I told the cat utility to show tabs.

Code:
cat -t abhi_123.example

Code:
20840GFC00524096100000LAU1LDNUSD CR       6.322017041120170411                       ^I^I^I^I^I^I  ^I       00016574200165737CO:BASAGIOSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840GFC00524096100000LAU1LDNUSD CR       6.322017041120170411             ^I^I^I^I^I^I^I^I       00016574200165737CO:BRGRUURSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411              ^I^I^I^I^I^I^I^I       00016574200165737CO:BTGFGFGSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411           ^I^I^I^I^I^I^I^I       00016574200165737CO:BGGFEWESDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411                       ^I^I^I^I^I^I  ^I       00016574200165737CO:BOGFDUISDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411                       ^I^I^I^I^I^I  ^I       00016574200165737CO:BPGFDSYSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840XYZ00524096100000LAU1LDNUSD CR       6.322017041120170411                       ^I^I^I^I^I^I  ^I       00016574200165737CO:BUGFGFGSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840XYZ00524096100000LAU1LDNUSD CR       6.322017041120170411                       ^I^I^I^I^I^I  ^I       00016574200165737CO:BTGFFSFSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD
20840ABC00524096100000LAU1LDNUSD CR       6.322017041120170411            ^I^I^I^I^I^I^I^I       00016574200165737CO:BIGFGFGSDSDSDSDSDSDSDSDSDSDSDSDSDSDSD

Therefore you can not rely on positions how you have be proposing from the beginning of the thread.

Counting on this uncertainty, the following two options might or might not do what you want.
Code:
perl -pe '/ABC/ and s/CO:B\w+$//' abhi_123.example

or
Code:
perl -pe 's/(ABC.*?)CO:B\w+$/$1/' abhi_123.example


Last edited by Aia; 05-21-2017 at 05:56 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

2. Shell Programming and Scripting

Find and replace last line in a file

Hi I am having a file which has like this content shown below Aaa,bb,cc,dd Xxx,yy,d,12 Dodd,12-Jun,t I need to replace last line like this Aaa,bb,cc,dd Xxx,yy,d,12 Dodd,10-August,t (13 Replies)
Discussion started by: rohit_shinez
13 Replies

3. Shell Programming and Scripting

find and replace a line in a file

Hi, I am want find and replace in following content in the file. i want to repalce a word TABLESPACE XCRM_<ANY_CHAR> to TABLESPACE XCRM Sample File to Replace : LOB(COMPLEX_VALUE) STORE AS ( TABLESPACE XCRM_MED_D_NEW STORAGE(INITIAL 64K BUFFER_POOL DEFAULT) ENABLE... (3 Replies)
Discussion started by: gavemani
3 Replies

4. Shell Programming and Scripting

Find and Replace in File

Legends, I have a file /tmp/list.txt I want to find "/bin/" and replace it with "/log/" I tried the follwoing but no luck Sandy: /tmp> perl -pi -e 's/\/bin\/\/log\/' /tmp/list.txt >> /tmp/try Substitution pattern not terminated at -e line 1. AND, Sandy: /tmp> perl -pi -e... (2 Replies)
Discussion started by: sdosanjh
2 Replies

5. UNIX for Dummies Questions & Answers

Find and Replace then rename file

Hi, This is probably quite simple for an expert, but I keep getting confused about the best approach, grep, awk, sed. What I have is a range of files numbered 1 to 100. They go file1.txt file2.txt and so on In each file I need to find and replace a couple of items and rename add a... (5 Replies)
Discussion started by: chickenhouse
5 Replies

6. Shell Programming and Scripting

How to find a certain string in a file and replace it with a value from another file using sed/awk?

Hi Everyone, I am new to this forum and new to sed/awk programming too !! I need to find particular string in file1(text file) and replace it with a value from another text file(file2) the file2 has only one line and the value to be replaced with is in the second column. file 1: (assert (=... (21 Replies)
Discussion started by: paramad
21 Replies

7. Shell Programming and Scripting

Selectively Find/Replace in a file?

I have a file that is HTML encoded. Each line has something like this on each line.. <href=http://link.com/username.aspx>username </a> more info.. <a href=http://link.com/info1.aspx>info1</a> more code... <a href=http://link.com/info2.aspx>info2</a> I have one goal really.. to clean up the... (2 Replies)
Discussion started by: dragin33
2 Replies

8. Shell Programming and Scripting

Find and replace in a gz file

Is there a way to do a find and replace in a .gz file in a single script ? I can always unzip, find and replace and then zip it again but would hate to do this everytime. Thanks ! Vivek (1 Reply)
Discussion started by: vashah
1 Replies

9. Shell Programming and Scripting

Find and replace in a file

Hi everyone, I am new to the world of shell script programming. I have a file named Fnd1.txt which has the contents as below. I need to replace the \t with the tab space. Can any one help me to write a perl scipt for this. USA45V1\tG\t341029 USAV1T1\tG\t450545 USAREJ1\tG\t572645... (5 Replies)
Discussion started by: vinay123
5 Replies

10. UNIX for Dummies Questions & Answers

Find replace within a file?

I build several files by using the cut command to grab select fields(columns) from a really bid csv file. Each file is one column of data. I then put them together using paste command. Here is the code built in tcsh: cut -d , -f 1 some.csv > 1.csv cut -d , -f 10 some.csv > 10.csv paste 1.csv... (2 Replies)
Discussion started by: yankee428
2 Replies
Login or Register to Ask a Question