How to pass a variable to a sed command in a csh script?

Aditya

Unix & Sed noob here!

I have a requirement where I need to put a suffix ",waive" in case a line in a report containing a matching string.The matching string is part of another file "waive.txt". I am also trying to perform these operations on a copy of the main report.

I am putting an example below

Here is how the report looks like:

i love apple_tart as a desert
banana is full of potassium and iron
there are so many helath benefits of apple eating 
the king of fruit is mango 
there are apple_pie of many variety

Here is how waive.txt looks like:

apple_pie
banana

This is my expeted output which I want in the same file :

i love apple_tart as a desert
banana is full of potassium and iron,waive
there are so many helath benefits of apple eating 
the king of fruit is mango 
there are apple_pie of many variety,waive

This is the script I tried but it is giving Illegal variable name Error

#!/bin/csh -f
if (-r fruits_bkup.txt) then
  rm fruits_bkup.txt
endif

yes|cp fruits.txt fruits_bkup.txt
foreach word ("`cat waiver.txt`")
  sed -i "/${word}/s/$/ ,waived/" fruits_bkup.txt 
end  

If I replace the double quotes with a single quotes in the sed command, nothing happens to the report.

Thanks!

Was expecting:

i love apple_tart as a desert
banana is full of potassium and iron,waive
there are so many helath benefits of apple eating 
the king of fruit is mango 
there are apple_pie of many variety,waive

Got: Illegal variable name Error

Kusalananda

Avoiding the quirks of the csh shell, we may solve this with a single awk command (which you may put into a csh script if you so wish). This also allows us to reduce the number of passes we need to make over the input file (we only need a single pass).

awk 'NR==FNR { query[++n] = $0; next } { for (i in query) if ($0 ~ query[i]) { $0 = $0 ",waive"; break } };1' waive.txt report.txt

The command first reads the lines of the first file, waive.txt, into the associative array query. When we reach the second file, report.txt, we iterate over the patterns in query and test each against the current line. If there is a match, we add ,waive to the end of the line and terminate the loop.

At the end, the trailing 1 causes the (possibly modified) line to be outputted.

Note that this uses the text from the first file as regular expressions. Would you want to do string comparisons instead, use index(query[i],$0) in place of $0 ~ query[i] in the code.

To write the result back to the original file, pipe the output to sponge report.txt, or redirect it to a new filename and then rename that new file to report.txt.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to pass a variable in sed command

How to use wildcard to be the variable and give it to a csh script

How to pass variable to exec command in expect script?

csh script variable substitution

Simple if command not working in csh script

How do I set a variable to a command's output in csh?

How to pass output of command to sed

How to pass a variable as an argument to a CasperJS script through the command line?

How to pass bash script variable value in PHP command (CLI)

How to pass '*' wildcard to path parameter of find command via a variable in script?

How to pass export variable with two find command output in shell script

How to Pass variable inside a curl command in a bash script

How to use variable in sed command

Event not found in sed call in csh script

variable in a script is not seen inside the script (csh, tcsh)

Pass command line variable into npm script?

Pass grep output to variable and use that variable as input to sed in shell script

store a result of a command into a variable using csh

How do i store the output of AWK as a variable to use in a SED command in a BASH script?

How to pass a script-file to sed as a heredoc?

How sed command used in shell script

How to run sed command in ruby script

how to pass a variable to sed from within r

How to pass a variable containing slashes to sed

How to run a csh script from a sh script

how to apply sed command on a array variable

How to use variable in sed command in shell

How to insert a variable content using sed command

Change variable value in Script shell with sed command ; error syntax