Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.
replace text
Name: deep.singh Date: May 31, 2005 at 07:44:21 Pacific OS: HP-UX 11.11 CPU/Ram: 4
Comment:
I have i/p file with following text --> 456 StreetA Rd (702) 456-7890 123 StreetB Rd 345-5678 789 StreetC Rd 123-8765 356 StreetD Rd (702) 657-3456
Name: nails Date: May 31, 2005 at 08:40:47 Pacific
Reply:
Provided your data file follows the structure you laid out this should work. I'm using Solaris, so I'm using nawk. You'll probably have to change that to awk:
#!/bin/ksh
nawk ' BEGIN { ck_str="(702)" } { if ($4 != ck_str) { $5=$4 $4=ck_str } print $0 } ' data.file
0
Response Number 2
Name: Jim Boothe Date: May 31, 2005 at 08:48:11 Pacific
Reply:
This first solution targets only lines that do not contain (702), and changes the first occurrence (if any) of Rd in each of these lines to Rd (702) regardless of what follows Rd - but we know it will not be (702):
sed '/(702)/!s/Rd/Rd (702)/'
This second solution searches for the first occurrence in each line of Rd where Rd is delimited by single spaces and followed by any character that is not a left parenthesis. If found, it changes Rd to Rd (702). In other words, if Rd is already followed by a parenthesis, it leaves it as is.
Summary: We have a file that looks like this: Partly Cloudy 33 Windy Very Very Cold North 15-25 those values (and spaces in the text) need to replace text in a different file that text is: AAA, BBB, CCC, DDD, ...
Summary: I have i/p file with following text --> 1> ^C Red ^D Blue ^E Orange 2> ^C Grey ^E Black 3> ^C Yellow ^E Blue I want o/p file to be converted like below --> 1> ^C Red ^D Blue ^E Orange 2> ^C Grey ^D ...
Summary: My original post assumed the actual filename was the replacement text. That's why I was able to use awk's FILENAME internal variable. You can embed shell variables in awk: awk ' { print $0"|""'"$V...