Output result of grep to var then to system file - linux

There is a parameter that I need to grep from a file and then I need to get that parameter into another file. I need to read the variable at boot time and have it inserted in
$grep "id" /file/one | cut -d " " -f2
$12345
So now I have the ID_VAR of 12345. Now what I would like to do is use this in /file/two
In file/two:
...
#program ID_VAR
...
Is there a way to run the grep function inside file two? Is there a way to share a variable between files? I am using Debian.

grep an id from one file and append to another file prepended with the string #program:
echo '#program' $(grep "id" file/one | cut -d " " -f2) >> file/two

There are some ambiguities in your question, but I think this snippet of script is what you are looking for:
Assuming #program is already in your file 2: (otherwise, see sudo_o's solution)
ID_VAR=$(grep "id" /file/one | cut -d " " -f2)
sed -i "s/#program/#program ${ID_VAR}/" /file/two
Explanation:
ID_VAR=$(...): save of the result of your grep and cut into ID_VAR
sed: invoke sed and use the -i option to edit the input file in place
"s/#program/#program ${ID_VAR}/": replace #program with #program (value of ID_VAR) in the input file
/file/two: what your input file is

Related

How to find a substring from some text in a file and store it in a bash variable?

I have a file named config.txt which has following data:
ABC_PATH=xxx/xxx
IMAGE=docker.name.net:3000/apache:1.8.109.1
NAMESPACE=xxx
Now I am running a shell script in which I want to store 1.8.109.1 (this value may differ, rest will remain same) in a variable, maybe using sed, awk or any other linux tool.
How can I achieve that?
The following will work.
ver="$(cat config.txt | grep apache: | cut -d: -f3)"
grep apache: will find the line that has the text 'apache:' in it.
-d specifies what delimiters to use. In this case : is set as the delimiter.
-f is used to select the specific field (array index, starting at 1) of the resulting list obtained after delimiting by :
Thus, -f3 selects the 3rd occurence of the delimited list.
The version info is now captured in the variable $ver
I think this should work:
cat config.txt | grep apache: | cut -d: -f3

Make this code read from file

Hello guys I wrote code in linux shell script but the code only read from keyboard i want to change it to read from file for example if i write ./car.sh lamborghini.txt it should give me most expensive model of it.
code is like this:
#!/bin/sh
echo "Choose one of them"
read manu
sort -t';' -nrk3 auto.dat > auto1.dat
grep $manu auto1.dat | head -n1 | cut -d';' -f2
and auto.dat file contains these:
Lamborghini;Aventador;700000
Lamborghini;Urus;200000
Tesla;ModelS;180000
Tesla;ModelX;140000
Ford;Mustang;300000
Ford;Focus;20000
The read command always reads from stdin. You can use redirection < to read the content of a file.
Reading $manu from a file's content
#!/bin/sh
read manu < "$1"
sort -t';' -nrk3 auto.dat | grep "$manu" | head -n1 | cut -d';' -f2
This version of your script expects a file name as a command line parameter. The first line of said file will be stored in $manu. Example:
./car.sh fileWithSelection.txt
The file should contain the text you would have entered in your old script.
Reading $manu from a command line parameter
In my opinion, it would make more sense to interpret the command line parameters directly, instead of using files and passing them to the script.
#!/bin/sh
manu="$1"
sort -t';' -nrk3 auto.dat | grep "$manu" | head -n1 | cut -d';' -f2
Example:
./car.sh "text you would have entered in your old script."
You can try this way but the file Tesla.txt must contain Tesla
#!/bin/sh
read manu < "$1"
awk -F\; -vmod="$manu" '
$1==mod{if($3>a){a=$3;b=$2}}
END{if(b){print "The more expensive "mod" is "b" at "a}}' auto.dat

execute cut command inside bash script

Every line printed with the echo includes the forward slashes for the directories that the given files are in. I am trying to cut the forward slashes using the cut command but it is not working. The files are gzipped so they have the .gz extension.
#!/bin/bash
for filename in /data/logs/2017/month_01/201701*
do
echo $filename
cut $filename -d '/' -f1
done
Thanks in advance.
The order of commands is wrong. You need to stream the string input to the cut command via pipe(|) or here-strings(<<<).
echo "$filename" | cut -d '/' -f1
(or)
cut -d '/' -f1 <<<"$filename"
(or) using here-docs
cut -d '/' -f1 <<EOF
$filename
EOF
data
And don't forget to double-quote variables to avoid Word-Splitting done by the shell.
Assuming filename is /a/b/c.gz you just want c.gz ?
Well there's two very easy answers:
basename $filename
The other is:
echo ${filename##*/}
The latter make use bash's built-in string delete parameter expansion.
Another way of solving your problem, is you could change directory first, i.e.
#!/bin/bash
pushd /data/logs/2017/month_01
for filename in 201701*
do
echo $filename
done
popd
Reference:
http://wiki.bash-hackers.org/syntax/pe
(EDIT: Fixed typo identified by #123)
As suggested b #lnian cut command used with echo command via pipe sign
For getting only file name with your script you need to use.
cut with -f1 option will get first value before / which would give blank so you need to get last value from the filename.
#!/bin/bash
for filename in /data/logs/2017/month_01/201701*
do
echo "$filename" | rev| cut -d '/' -f1
done
rev command reverse the filename so you will get last value which is your filename

using linux cat and grep command

I am having following syntax for one of my file.Could you please anyone explain me what is this command doing
path = /document/values.txt
where we have different username specified e.g username1 = john,username2=marry
cat ${path} | grep -e username1 | cut -d'=' -f2`
my question here is cat command is reading from the file value of username1 but why why we need to use cut command?
Cat is printing the file. The file has username1=something in one of the lines. The cut command splits this and prints out the second argument.
your command was not written well. the cat is useless.
you can do:
grep -e pattern "$path"|cut ...
you can of course do it with single process with awk if you like. anyway the line in your question smells not good.
awk example:
awk -F'=' '/pattern/{print $2}' inputFile
cut -d'=' -f2`
This cut uses -d'=' that means you use '=' as 'field delimiter' and -f2 will take only de second field.
So in this case you want only the value after the "=" .

How to store in a variable an echo | cut result?

for example
echo "filename.pdf" | cut -d'.' -f 1
This way I get the "filename" string.
I'd like to store it in a variable called FILE and then use it like this:
DIR=$PATH/$FILE.txt
So, my script wants to create a file.txt with the same name of the pdf (not a copy of the file, just the name)
This way I tried to assign the result of echo | cut
FILE=
but I get only "path/.txt" so the filename is missing.
FILE=$(echo "filename.pdf" | cut -d'.' -f 1)
So, my script wants to create a file.txt with the same name of the pdf
You can use BASH string manipulation:
s="filename.pdf"
p="${s/%.pdf/.txt}"
echo "$p"
filename.txt
POSIX parameter expansion would read
file=filename.pdf
filename="${file%%.*}" # Two % will remove multiple extensions, if applicable
dir=$path/$filename.txt

Resources