use grep and cut command to get output of each line in next line [duplicate] - bash

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 2 months ago.
$ cat hello.txt
shreenivasa hi hello this is test mail
shreenivasa how are you man
if i run in terminal
$ cat hello.txt | grep shreenivasa | cut -d ' ' -f 2-
its giving following output
hi hello this is test mail
how are you man
but if i write it as script in hello.sh like below
#!/bin/bash
hi=`cat hello.txt | grep shreenivasa | cut -d ' ' -f 2-`
echo $hi
output for ./hello.sh is
hi hello this is test mail how are you man.
I want to print "how are you man" in next line
like
hi hello this is test mail
how are you man
tried $hi\n didn't work

Related

Bash script doesn't show output, but the exact same script in a oneliner does [duplicate]

This question already has answers here:
Printing output of C program in shell script
(1 answer)
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 7 months ago.
This bash script returns nothing, but the exact same script ran in a oneliner returns the correct output.
#!/bin/bash
URL="localhost:3000"
content=$(curl -s $URL | sed -E 's/<[^>]*>//g')
vidsWatched=$(echo $content | grep -A 3 "Videos Watched" | sed -n 4p)
echo "$vidsWatched"
Vs the onliner:
curl -s "http://localhost:3000" | sed -E 's/<[^>]*>//g' | grep -A 3 "Videos Watched" | sed -n 4p
Output:
600

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...

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

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

Get file extension with bash script [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
i' m trying to extract the file extension in bash without using regex.
i' ve tried the following
extension = $(echo $1 | cut -f 2 -d '.')
extension is the variable
$1 contains something like: file.txt or file.pdf etc.
this code is outputting:
./prova.sh: 3: ./prova.sh: extension: not found
extension=$(echo sample.txt | cut -f 2 -d '.')
echo $extension
Above command will give you the output as expected txt

How to pass output as command line argument in bash? [duplicate]

This question already has answers here:
How to pass command output as multiple arguments to another command
(5 answers)
Closed 3 years ago.
I have this two step bash command:
L=`wc -l testfile | cut -d' ' -f1`
myprogram testfile $L testfile.out
Long story short, myprogram needs the line count as an input.
I want to combine this into one line.
This does not work because using redirect | to - passes stdout stream as a file, not a string.
wc -l testfile | cut -d' ' -f1 | myprogram testfile - testfile.out
Is there a way to combine this into one line?
Use process substitution:
myprogram testfile $(wc -l < testfile) testfile.out
^^^^^^^^^^^^^^^^^^^
This way, wc -l < testfile is evaluated together with the call of the program and you have both commands combined.
Note wc -l < file returns you just the number, so you don't have to do cut or any other thing to clean the output.

Resources