how to read words from a file using shell script - shell

i have a file /ws/$1-rcd/temp.txt which has only one line as follows
198|/vob/ccm_tpl/repository/open_source/commons_collections/3_2_2/...
i have a script to get the value repository/open_source/commons_collections and 3_2_2 by reading the file and looping through it using for loop
i have my code as follows
grep -n "$4" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f1,2 | sed -e 's/\:element/|/g' | sed -e 's/ //g' > /ws/$1-rcd/temp.txt
for i in `cat /ws/$1-rcd/temp.txt`
do
line=`echo $i | cut -d"|" -f1`
path=`echo $i | cut -d"|" -f2`
whoami
directory_temp=`echo $path | awk -F "/" '{ print $(NF-2)}'`
if [ "$directory_temp" == "$4" ]
then
OLD_VERSION=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print $(NF-1)}'`
total_fields=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print NF }'`
dir_path=`expr ${total_fields} - 2`
loc=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | cut -d"/" -f1-"${dir_path}"`
location=`echo $loc | cut -d"/" -f4,5,6`
fi
done
but when i run this code it gives me an error as
-bash: line 45: syntax error near unexpected token |'
-bash: line 45:for i in 198|/vob/ccm_tpl/repository/open_source/commons_collections/3_2_2/...'
can anyone please suggest what am i doing wrong

If you want to iterate through each line of a file, use while loop like below
while read -r line ;do
echo $line
done <file.txt
so, your code can be rewritten as
grep -n "$4" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f1,2 | sed -e 's/\:element/|/g' | sed -e 's/ //g' > /ws/$1-rcd/temp.txt
while read i ; do
line=`echo $i | cut -d"|" -f1`
path=`echo $i | cut -d"|" -f2`
whoami
directory_temp=`echo $path | awk -F "/" '{ print $(NF-2)}'`
if [ "$directory_temp" == "$4" ]
then
OLD_VERSION=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print $(NF-1)}'`
total_fields=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print NF }'`
dir_path=`expr ${total_fields} - 2`
loc=`sed -n "${line}p" /ws/$1-rcd/raw-vobs-config-spec | cut -d " " -f2 | cut -d"/" -f1-"${dir_path}"`
location=`echo $loc | cut -d"/" -f4,5,6`
fi
done < /ws/$1-rcd/temp.txt

You may be better served relying on parameter expansion and substring removal. For example:
#!/bin/sh
a=$(<dat/lline.txt) ## read file into a
a=${a##*ccm_tpl/} ## remove from left to ccm_tpl/
num=${a##*collections/} ## remove from left to collections/
num=${num%%/*} ## remove from right to /
a=${a%%${num}*} ## remove from right to $num
Input File
$ cat dat/lline.txt
198|/vob/ccm_tpl/repository/open_source/commons_collections/3_2_2/..
Output
$ sh getvals.sh
a : repository/open_source/commons_collections/
num : 3_2_2
If you need to trim in some other way, just let me know and I'm happy to help further.

Related

i dont know why it writed me 11:not found

it writes me "file name" 18:11 not found
line 18 is where the if statement starts
#!/bin/bash
westfunc() {
echo "please select 2 airports(example:jfk):\n"
read place
read place2
echo "\e[32m--------------------------------------------------------------------------------"
echo
echo " $place $place2"
de=`weather-util -m $place | grep "Temperature"| cut -d "T" -f2 | cut -d "e" -f4| cut -d ":" -f2`
we=`weather-util -m $place | grep "Wind" | cut -d "W" -f3 | cut -d ")" -f2`
sc=`weather-util -m $place | grep "Sky conditions" | cut -d "n" -f3 | cut -d ":" -f2`
var=`weather-util -m $place | grep "Temperature"| cut -d " " -f5 | cut -d "." -f1`
if (( $var > 5 ))
then
echo "hello"
fi
}
i tried to change $ the ( and stuff like that

echo output not getting assigned to a variable in shell script

end=echo $FSDB_FILE_NAME | rev | cut -d'_' -f 2 |rev
begin=echo $FSDB_FILE_NAME | rev | cut -d'_' -f 3 |rev
echo $end
echo $begin
echo abc_11204.00_15713.00_.csv | rev | cut -d'_' -f 2 |rev ---- This works
But echo $end is not printing anything
I even tried:
set end=echo abc_11204.00_15713.00_.csv | rev | cut -d'_' -f 2 |rev
echo $end
This prints empty
Please help me with this
Sample input : abc_123.00_345.00_xyz.csv
Output : end=345.00
begin=123.00
Could you please try following. Easy approach with awk.
start=$(echo "$input_variable" | awk -F'_' '{print $2}')
end=$(echo "$input_variable" | awk -F'_' '{print $3}')
When I print variable's values it will be as follows:
echo "$start"
123.00
echo "$end"
345.00

Shell Script do while flow

I have a script whose content is like:
#!/bin/bash
DB_FILE='pgalldump.out'
echo $DB_FILE
DB_NAME=''
egrep -n "\\\\connect\ $DB_NAME" $DB_FILE | while read LINE
do
DB_NAME=$(echo $LINE | awk '{print $2}')
STARTING_LINE_NUMBER=$(echo $LINE | cut -d: -f1)
STARTING_LINE_NUMBER=$(($STARTING_LINE_NUMBER+1))
TOTAL_LINES=$(tail -n +$STARTING_LINE_NUMBER $DB_FILE | \
egrep -n -m 1 "PostgreSQL\ database\ dump\ complete" | \
head -n 1 | \
cut -d: -f1)
tail -n +$STARTING_LINE_NUMBER $DB_FILE | head -n +$TOTAL_LINES > /backup/$DB_NAME.sql
done
I know what it is doing. But i have a doubt about the flow of do while in this case. At line egrep -n "\\\\connect\ $DB_NAME" $DB_FILE | while read LINE will egrep runs first or while . Because DB_NAME is empty at start of code.
Could anyone please explain the flow of do while in this case.

How to fix an unexpected end of file error in bash?

The code saying that there is "unexpected end of file" error in line 16. Could someone please tell me my mistake?
#!/bin/bash
total=0
for i in `grep 01/Oct/2006 log.txt | cut -d' ' -f1 | sort | uniq -c | sort -n | tail`;
do if [[ $i =~ ^[0-9]+$ ]]; then
total=$(( $total + $i )); fi
for i in `grep 01/Oct/2006 log.txt | cut -d' ' -f1 | sort | uniq -c | sort -rn | head -10 | tr -s ' ' | cut -d' ' -f2,3 | sed -E 's/(\S*) (\S*)/\2 - \1/' | nl -s'. '`;
do
if ! [[ $i =~ ^[0-9]+$ ]];
then
printf $i;
printf " ";
else
printf " $i - $(echo "scale=0; $i * 100 / $total" | bc )%% \n" ;
fi
done
Your first for loop lacks a done.
Here's a working version with improved formatting (but with all original flaws and bugs left inside, I just fixed the one issue asked for here):
#!/bin/bash
$total;
for i in $(
grep 01/Oct/2006 log.txt |
cut -d' ' -f1 |
sort |
uniq -c |
tail);
do
if [[ $i =~ ^[0-9]+$ ]]
then
$total += $i
fi
done
for i in $(
grep 01/Oct/2006 log.txt |
cut -d' ' -f1 |
sort |
uniq -c |
sort -rn |
head -10 |
tr -s ' ' |
cut -d' ' -f2,3 |
sed -E 's/(\S*) (\S*)/\2 - \1/' |
nl -s'. ')
do
if ! [[ $i =~ ^[0-9]+$ ]];
then
printf " $i - ";
else
printf " $i - 0$(echo "scale=0; $i / $total" | bc)%% " ;
fi
done

Delete <feff> from text file, UTF 8

I use this BASH script to filter text from one text file to another. Text is encoded in UTF 8.
#!/bin/bash
mid=$1
infile="/var/www/tmp/textgrid_uploads/${mid}.txt"
outfile="/home/var/www/vids/$mid/${mid}_textgrid.mlf"
tmpfile="/home/var/www/vids/$mid/${mid}.tmp"
i=1
touch $tmpfile
cat $infile | grep "text =" | cut -d '"' -f2 | tr -d ',' | tr -d '.' | tr -d ':' | tr -d ';' | tr -d '!' | tr -d '?' > $tmpfile
#| awk '{ print tolower($0) }'
#cat $infile | grep -v "<" | egrep -v '^[[:space:]]*$' | tr -d '.' | tr -d "," | tr -d ";" | tr -d ":" | tr -d "^" | tr -d '#' | tr -d '?' | tr -d '!' | tr -d '%' | tr -d '#' | tr -d '*' | tr -d '~' | grep -v '((xxxxx))' | awk '{ print tolower($0) }' > $tmpfile
#cat $infile | grep -v 'WEBVTT' | grep -v "\--" | grep -v '^$' | sed 's/?/./g' | sed 's/!/./g' | tr -d '.' | tr -d "," | tr -d ";" | tr -d ":" | awk '{ print tolower($0) }' > $tmpfile
nlines=$(cat $tmpfile | wc -l)
echo "#!MLF!#" >> $outfile
echo "\"*/dummyfile.lab\"" >> $outfile
while [ $i -le $nlines ]
do
line=$(cat $tmpfile | sed $i'q;d') #zobrazi konkretny riadok
printf '%s\n' $line | sed '/^\s*$/d' | cut -d "/" -f1 | egrep -v '^[[:space:]]*$' >> $outfile #zapis po riadkoch s odstranenim znaciek
i=$[$i+1] #pocitadlo
done
echo "." >> $outfile
rm $tmpfile
Output of this script is:
#!MLF!#
"*/dummyfile.lab"
<feff> V utorok o devätnástej bude vo fejs
I want to remove first word feff with both brackets. Please give me whole example how to do it. I am new in Bash and I just use this script I didn't create it. Thanks guys.

Resources