Reputation: 17
I am trying to execute below code in my script. It is a small part of my code which is throwing an I/O error after successfully executing my script.
Is anything wrong with my usage of code. I dont need that error message to print it in my screen after my output.
awk '/.edu { sub(".edu",".edu")}}1' | sed 's/\.edu/.fail/g' < temp1.txt >> output.txt
Upvotes: 0
Views: 799
Reputation: 5006
Are you sure that
awk '/.edu | .eda/{c++; if (c==2) { sub(".edu",".edu")}}1'
is doing what you expect ? It replaces <any character>edu
by .edu
on the second line where <any characyer>eda
or <any character>edu
appears.
Another error on that part : you don't provide any input to this awk command.
Then you redirect both content of temp1.txt and standard output of awk to sed (which is also weird)
awk ... | sed 's/\.edu/.fail/g' < temp1.txt
I guess that is why you get an I/O error.
If awk command is useful, just do :
awk '/.edu | .eda/{c++; if (c==2) { sub(".edu",".edu")}}{gsub("\.edu",".fail")}1' temp1.txt >> output.txt
If awk command is useless, do :
sed 's/\.edu/.fail/g' temp1.txt >> output.txt
My requirement is : if in my input, i see .edu or .eda , replace it with .fail. if the line has both .eda & .edu replace only .edu with .fail keeping .eda as it is
Input example :
.eda
.edu .eda
.eda .edu
.edu
hello world
Code :
awk '!/\.edu/{gsub(/\.eda/,".fail")}{gsub(/\.edu/, ".fail")}1' test.txt >> output.txt
You'll get:
.fail
.fail .eda
.eda .fail
.fail
hello world
Upvotes: 0
Reputation: 203522
This:
awk 'script' | sed 'script' < file
tells sed to expect it's input to come from the output of awk but then says "oh, no wait - take it from file instead". It also tells awk to run on no input so it'll hang waiting for you to type something. You probably meant to write this instead:
awk 'script' file | sed 'script'
so awk reads file
and pipes it's output to sed. Having said that, you never need to pipe awk output to sed, you can just do whatever you want inside the original awk script. You wrote:
awk '/.edu | .eda/{c++; if (c==2) { sub(".edu",".edu")}}1' | sed 's/\.edu/.fail/g' < temp1.txt
and said in a comment that:
My requirement is : if in my input, i see .edu or .eda , replace it with .fail. if the line has both .eda & .edu replace only .edu with .fail keeping .eda as it is
so this is probably what you really wanted (untested since you didn't provide any sample input/output to test against:
awk '!sub(/\.edu/,".fail"){sub(/\.eda/,".fail")}1' temp1.txt
Upvotes: 1