How to insert a file into this code but not a string - unix

This is the code:
#!/bin/bash
title='Unix Shell Programming'
read search_word
if [[ $title =~ $search_word ]]
then
echo Yes - matchli
else
echo No - match
enter code here
fi
I want to substitute the $title to a file called emplist. Is it possible? And how can I do that? Thank you!

add these lines in your shell script,
echo "enter file name with extension"
read fname
cat $fname

You can use grep like this:
if grep -q "$search_word" emplist
then
# $search_word was found inside the file emplist

Related

How to get everything after an extension unix

I have a file name like
filename.txt.zip_20180202_30291_233
In script variable i will have filename in a variable and extension in variable
like
echo $fileprefix
filename
echo $filesuffix
.txt.zip
How do i rename the file to below one ? Moving everything after extension to before extension ? (Note: there could be any numbers after extension )
filename_20180202_30291_233.txt.zip
#!/bin/sh
prefix='filename'
suffix='.txt.zip'
name='filename.txt.zip_20180202_30291_233'
newname="$prefix${name#$prefix$suffix}$suffix"
echo mv "$name" "$newname"
This would output
mv filename.txt.zip_20180202_30291_233 filename_20180202_30291_233.txt.zip
The parameter substitution ${name#$prefix$suffix} removes $prefix$suffix, i.e. filename.txt.zip, from the start of $name, which gives you _20180202_30291_233. This is then prepended with $prefix and appended with $suffix to create $newname.
Looping over all files that matches "$prefix$suffix"* in the current directory and renaming all:
for name in "$prefix$suffix"*; do
newname="$prefix${name#$prefix$suffix}$suffix"
echo mv "$name" "$newname"
done
The echo is there for protection. Remove it once you are certain the code will do the right thing.
file_orig=filename.txt.zip_20180202_30291_233
fileprefix=filename
filesuffix=.txt.zip
file="${file_orig#$fileprefix}"
file="${file#$filesuffix}"
file="$fileprefix$file$filesuffix"
echo "$file"
filename_20180202_30291_233.txt.zip
https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

echo does not display proper output

Following code read the test.txt contents and based on first field it redirect third field to result.txt
src_fld=s1
type=11
Logic_File=`cat /home/script/test.txt`
printf '%s\n' "$Logic_File" |
{
while IFS=',' read -r line
do
fld1=`echo $line | cut -d ',' -f 1`
if [[ $type -eq $fld1 ]];then
query=`echo $line | cut -d ',' -f 3-`
echo $query >> /home/stg/result.txt
fi
done
}
Following is the contents of test.txt:
6,STRING TO DECIMAL WITHOUT DEFAULT,cast($src_fld as DECIMAL(15,2) $tgt_fld
7,STRING TO INTERGER WITHOUT DEFAULT,cast($src_fld as integer) $tgt_fld
11,DEFAULT NO RULE,$src_fld
everything works fine except output in result.txt is $src_fld instead of s1. Can anyone please tell me what is wrong in the code?
Try replacing the below line
echo $query >> /home/stg/result.txt
with this one
eval "echo $query" >> /home/stg/result.txt

sub string in unix on a character

I have file names inside a directory in unix as:
code1_abc.txt
code2_xyz.txt
code1_pqr.txt
I am looping over all files in this director to do some stuff on each files:
for myFile in $(ls $INPUT_DIR/* | xargs -n 1 basename)
do
echo $myFile
done
However, now I want to split the file name and want to get the part before the underscore i.e. code1, code2, code3
for myFile in $(ls $INPUT_DIR/* | xargs -n 1 basename)
do
echo $myFile
codeForCurrentFile= // want code1 here using myFile value
echo $codeForCurrentFile // should echo code1, code2, code3 respectively
done
How to do this? I am using korn shell.
Thanks for reading!
Use ksh pattern substitution to replace the underscore and anything after it with nothing (effectively delete):
echo ${myFile//_*/}
For your example:
codeForCurrentFile=${myFile//_*/}
More info here (see section 4.5.4): http://docstore.mik.ua/orelly/unix3/korn/ch04_05.htm
You can do this by calling out to an external program, regardless of the shell in use (provided it supports output capture of external programs, of course), such as with the following transcript:
pax$ fspec=code1_abc
pax$ echo $fspec
code1_abc
pax$ pre=`echo $fspec | cut -d_ -f1` ; echo $pre
code1
pax$ post=`echo $fspec | cut -d_ -f2` ; echo $post
abc
There are a wide variety of tools you can use to achieve this, cut (as above, probably the simplest), awk, sed and so on.
This has the disadvantage of kicking up external processes, something that should be okay provided you're not doing it many times per second. If it's something that needs to be fast, you're better off using shell-specific internal methods, such as:
ksh:
fspec=code1_abc
pre=${fspec//_*/}
post=${fspec//*_/}
bash:
fspec=code1_abc
pre=${fspec%%_*}
post=${fspec#*_}
csh:
set fspec = code1_abc
set arr = ( $fspec:as/_/ / )
set pre = $arr[1]
set post = $arr[2]

Displaying new line in shell variable

While executing the below script:
export var1="string('\n')"
echo $var1
I get the output as string(n). But I expect to see the output as : "string('\n')".
Is there any way to get that?
Note: I need the output to be displayed in string('\n') (\n in single quotes) to use that variable in m_wc abinitio command.
Update: Thanks for responding quickly, but the variable text goes to next line when '\n' occurs. output below:
export var='string('\''\n'\'')'
export var1="string('\\n')"
echo "$var1"
string('
')
>echo $var
string('
')
Is there any way to escape the '\n' entirely?
Update : printf '%s\n' "$var1" seems to be working fine, but how can i put this value to a variable and use it anywhere in the script ?
I tried to put like below : but does not work:
export var1="string('\n')"
export var2="printf '%s\n' "$var1""
export BYT_CNT=m_wc -no-commas -string $var2 /prod/users/edw/gvx770/list.dat|awk '{print $1}'
echo $var2
echo $var2
printf '%s
' string('
')
The easiest thing to do is probably:
export var1="string('\\n')"
echo "$var1"
This will do what you want:
export var='string('\''\n'\'')'
What you need to know is:
You can include anything between single-quotes except for an apostrophe; and
You can quote an apostrophe by preceding it with a backslash, outside of any single- or double-quoting.
With those two rules, you can quote anything in shell.
Alternatively, you can use double-quotes, but the rules for using it effectively are more complex.
Finally, got the solution to fix this issue. Here is the code:
export dir="string('\n')"
export j=$(printf "%s\n" $dir)
export BYT_CNT=m_wc -no-commas -string "$j" /temp/users/list.dat | awk '{print $1}'
echo $BYT_CNT
Thanks guys for all your help.

substring before and substring after in shell script

I have a string:
//host:/dir1/dir2/dir3/file_name
I want to fetch value of host & directories in different variables in unix script.
Example :
host_name = host
dir_path = /dir1/dir2/dir3
Note - String length & no of directories is not fixed.
Could you please help me to fetch these values from string in unix shell script.
Using bash string operations:
str='//host:/dir1/dir2/dir3/file_name'
host_name=${str%%:*}
host_name=${host_name##*/}
dir_path=${str#*:}
dir_path=${dir_path%/*}
I would do it using regular expressions:
if [[ $path =~ ^//(.*):(.*)/(.*)$ ]]; then
host="${BASH_REMATCH[1]}"
dir_path="${BASH_REMATCH[2]}"
filename="${BASH_REMATCH[3]}"
else
echo "Invalid format" >&2
exit 1
fi
If you are sure that the format will match, you can do simply
[[ $path =~ ^//(.*):(.*)/(.*)$ ]]
host="${BASH_REMATCH[1]}"
dir_path="${BASH_REMATCH[2]}"
filename="${BASH_REMATCH[3]}"
Edit: Since you seem to be using ksh rather than bash (though bash was indicated in the question), the syntax is a bit different:
match=(${path/~(E)^\/\/(.*):(.*)\/(.*)$/\1 \2 \3})
host="${match[0]}"
dir_path="${match[1]}"
filename="${match[2]}"
This will break if there are spaces in the file name, though. In that case, you can use the more cumbersome
host="${path/~(E)^\/\/(.*):(.*)\/(.*)$/\1}"
dir_path="${path/~(E)^\/\/(.*):(.*)\/(.*)$/\2}"
filename="${path/~(E)^\/\/(.*):(.*)\/(.*)$/\3}"
Perhaps there are more elegant ways of doing it in ksh, but I'm not familiar with it.
The shortest way I can think of is to assign two variables in one statement:
$ read host_name dir_path <<< $(echo $string | sed -e 's,^//,,;s,:, ,')
Complete script:
string="//host:/dir1/dir2/dir3/file_name"
read host_name dir_path <<< $(echo $string | sed -e 's,^//,,;s,:, ,')
echo "host_name = " $host_name
echo "dir_path = " $dir_path
Output:
host_name: host
dir_path: /dir1/dir2/dir3/file_name

Resources