text substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting text substitution
# 1  
Old 03-07-2008
Error text substitution

hi,
in a text file i want to find the lines
which contains A and B,and if these lines do not contain C and D.
i want to substitute X with Y.
how can i achive these?
i tried with sed and awk, but couldnt succeed.
# 2  
Old 03-07-2008
What have you done to attempt to solve this problem yourself?
Post your sample script, the inputfile and the desired output and we'll see how we can assist.

Regards
# 3  
Old 03-07-2008
i have tried to modify the text file using the awk one-liners below:

awk '/A/{gsub(/X/, "Y")};{print}' inputfile
awk '!/C/{gsub(/X/, "Y")};{print}' inputfile

but here the main problem is my conditions should be in the same statement
# 4  
Old 03-07-2008
Think this should be what you're after:

Code:
#  cat infile
A B X
B A X
C A B X
A B D X


#  awk '/A/ && /B/ && !/C/ && !/D/ {gsub("X", "Y")};{print}' infile
A B Y
B A Y
C A B X
A B D X

use nawk or /usr/xpg4/bin/awk for solaris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Text substitution & getting file name from url

hi, sorry if this seems trivial. i have a file url.txt which consists of a list of urls (it was supposed to be my wget -i file). however, since the server from which i am trying to download uses redirect, wget dows not remeber the filename of ther original url will save to a file name which is... (3 Replies)
Discussion started by: texttoolong
3 Replies

2. Shell Programming and Scripting

vi substitution

Hello, I'm trying to do a substitution in vi. which adds a field for the year to a line. If the line doesnt include a year, it should still add a field (although empty) the fields are: Country:number:number:name(and sometimes year):place this is a desired in and output: Sweden:55:32:John... (2 Replies)
Discussion started by: drareeg
2 Replies

3. Shell Programming and Scripting

Text Substitution Project

History: large open source PHP project, school management program. Comprises about 200 scripts. Had another developer for awhile, and he wanted a version in German, so he edited all the scripts and replaced text that would show up in the browser with variables (i.e. instead of "Click Here",... (7 Replies)
Discussion started by: dougp23
7 Replies

4. Shell Programming and Scripting

substitution help

How do i substitute ' with space in a file using sed or awk i am getting the following two scenarios 1) xyz'd with xyz d if i use sed 's/xyz\\\'d/xy z/g' it is taking ' after \ as closing expr for substitution 2) xyz';d with xyz d please advice (8 Replies)
Discussion started by: mad_man12
8 Replies

5. UNIX for Dummies Questions & Answers

help with substitution

Hello, I have a file with 10,000+ records which look like this: Image3992170.tif 4/21/200811:42:09AM 3,373.13KB Image3993265.tif 4/11/20087:17:58PM 2,369.72KB Image3996764.tif 5/2/200811:01:28AM 2,155.87KB Image3997700.tif ... (4 Replies)
Discussion started by: lotusdeva
4 Replies

6. Shell Programming and Scripting

Substitution

All, I have this text document that contains a listing(See below). What i would like to ask is how i could extract just the information i need which is the files name (CWS*****.***.gz) If anyone has any suggestions i would be very grateful. I am sure its relatively simple but i just... (6 Replies)
Discussion started by: Andyp2704
6 Replies

7. Shell Programming and Scripting

Difference between "Command substitution" and "Process substitution"

Hi, What is the actual difference between these two? Why the following code works for process substitution and fails for command substitution? while IFS= read -r line; do echo $line; done < <(cat file)executes successfully and display the contents of the file But, while IFS='\n' read -r... (3 Replies)
Discussion started by: royalibrahim
3 Replies

8. UNIX for Dummies Questions & Answers

Need help in substitution!!!!

In a file I want to globally change a "|" charater by a new line character. I am using the command 1,$s/\|/??/g Can anybody say what should I put in place of ?? in the above command? (3 Replies)
Discussion started by: uLearner
3 Replies

9. Shell Programming and Scripting

substitution

I am trying to substituted a variable to a file using sed. However, the value of that variable is not being substituted. Here is an example of my code. lf=' ' v_whole="${1} ${2} ${3} $lf" cp ${IPPDIR}/ctl/fax_sub_text_a.${4}.${5}.txt... (1 Reply)
Discussion started by: supercbw
1 Replies

10. UNIX for Dummies Questions & Answers

Update text files in place (string substitution) ??

The auditors have nailed us for world writeable files.... Apparently in years gone by, quite a number of our kornshell scripts have had: umask 000 put in the script. We have been able to turn off world writeable for existing dirs & files, but as these scripts run, new files keep getting... (1 Reply)
Discussion started by: kornshellmaven
1 Replies
Login or Register to Ask a Question