how to pass parameters to awk in a script file (using it to replace)? [duplicate] - bash

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 2 years ago.
I would like to use awk to replace any occurrence of a given word with another word in a .sh
The script I wrote is;
#!/bin/bash
read -p "Enter the word to change: " wrd1
read -p "Enter the new word: " wrd2
awk '{sub(/"$wrd1 /, $wrd2")}1' file.txt
but it's not working.

Use the -v option.
awk -v w1="$wrd1" -v w2="$wrd2" '{sub(w1, w2)}1' file.txt
A demonstration:
$ awk -v w1="$wrd1" -v w2="$wrd2" '{sub(w1, w2)}1' <<EOF
foo=3
hi=9
bye=2
EOF
bar=3
hi=9
bye=2

Related

How to manipulate a string with the grep command? [duplicate]

This question already has answers here:
How can I remove the extension of a filename in a shell script?
(15 answers)
Closed 11 months ago.
I have a filename with the format yyyymmdd.txt. How can I output only yyyymmdd without the .txt extension?
Example
20220414.txt (before output)
20220414 (after the output)
basename has an option to remove a suffix:
basename -s .txt 20220414.txt
gives:
20220414
Or, if your filename is stored in a variable, bash can help:
a=20220414.txt
echo ${a%.*}
gives:
20220414
You can user awk with flag -F to specify the separator . and then print the first part with $1
echo "20220414.txt" | awk -F "." ' {print $1}'
output
20220414
grep doesn't manipulate anything, it shows what you have in a file. So, you can't modify that file using grep, but you can modify what it shows, using the -o switch, as you can see here:
Prompt> echo "20220414.txt" | grep -o "[0-9]*"
20220414
The [0-9]* means "a list of integers, going from character '0' to character '9'.

Bash unable to assign value [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 1 year ago.
Got a bit of a problem. I'm new to bash and I'm trying my hardest, but I can't figure out how to assign the desired output to the variable. Running this command in the prompt
wc -l data.txt | awk '{ print $1 }'
yields the result 12, which is desired. However if I put it in the test.sh file, it won't work. I've tried different quotations, but all I've managed to get is the entire line as a string...
Test.sh
#! /bin/bash
# Count lines for data.txt files
data1=wc -l data.txt | awk '{ print $1 }'
echo "Lines in data.txt: $data1"
exit
I think you want:
data1="$(wc -l data.txt | awk '{ print $1 }')"
The $() syntax causes bash to execute that expression and replace it with the results.
Actually, powershell does allow you to do a straight = assignment like you did...

bin sh script - "(" unexpected [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 3 years ago.
What is wrong with this script?
#!/bin/sh
for file in *.p; do awk -f get_p.awk "$file" > "$file"er; done
awk -f phase-comp.awk file1 file.per # výpočet fází
paste <(awk '{print $1}' output1) <(awk '{print $2}' file1) > out
The error is:
5: skript: Syntax error: "(" unexpected
When I write command separately to terminal. It works well. Thank you
The problem is your shebang:
#!/bin/sh
It means this script is meant to be interpreted by sh, but your script uses process substitution which is a bash-specific feature. So, you should change it to:
#!/bin/bash
to make your script work.

How to only read the last line from a text file [duplicate]

This question already has answers here:
How to read the last line of a text file into a variable using Bash? [closed]
(2 answers)
Print the last line of a file, from the CLI
(5 answers)
Closed 4 years ago.
I am working on a tool project. I need to grab the last line from a file & assign into a variable. This is what I have tried:
line=$(head -n $NF input_file)
echo $line
Maybe I could read the file in reverse then use
line=$(head -n $1 input_file)
echo $line
Any ideas are welcome.
Use tail ;)
line=$(tail -n 1 input_file)
echo $line
Combination of tac and awk here. Benefit in this approach could be we need NOT to read complete Input_file in it.
tac Input_file | awk '{print;exit}'
With sed or awk :
sed -n '$p' file
sed '$!d' file
awk 'END{print}' file
However, tail is still the right tool to do the job.

Unix Shell Script: Remove common prefix from a variable [duplicate]

This question already has answers here:
Remove a fixed prefix/suffix from a string in Bash
(9 answers)
Closed 2 years ago.
I have 2 variables one which is holding the prefix and other one the complete string.
For e.g
prefix="TEST_"
Str="TEST_FILENAME.xls"
I want the the Str to be compared against prefix and remove that common characters 'TEST_' and i want the output as FILENAME.xls. Please advise if it can be done with minimal lines of coding. Thanks a lot.
Using BASH you can do:
prefix="TEST_"
str="TEST_FILENAME.xls"
echo "${str#$prefix}"
FILENAME.xls
If not using BASH you can use sed:
sed "s/^$prefix//" <<< "$str"
FILENAME.xls
Try this:
$ Str=$(echo $Str | sed "s/^${prefix}//")
$ echo $Str
FILENAME.xls
Or using awk:
$ echo $Str | awk -F $prefix '{print $2}'
FILENAME.xls

Resources