Bash shell string contains print full word - string

the code:
READLINE=$(sudo /bin/cat < "file.txt");
IFS=$';'
for i in $READLINE;
do
txt=$txt" ""$i";
done
unset IFS
for x in $arr
do
if [[ $txt == *$x* ]] ; then
echo "$x is in the txt file";
output file.txt
telecomtest;yess;
output for loop:
telecomtest is in the txt file
tel is in the txt file
I want only print the full word not a part of it.
that means only print telecomtest and not tel.
How can i make this work?

Your $arr appears to contain both telecomtest and tel. Since you don't show us how $arr is assigned, everything else is speculation. Did you delete too much of the script to post it here? There's also a fi missing to make this valid shell grammar.
Please provide a minimal runnable script for us, including some stripped down contents of file.txt so we are able to reproduce the problem. Often this will uncover the problem right away.

Related

Concatenate Unknown Multiple Text Files While Inserting a Divider After the Contents of Each File Shell Script

For this problem in shell script, I need to accept an unknown multiple number of texts files and concatenate them. But I need to put a divider such as (-----) after the contents of each file. There also needs to be an exit code for the script where if it succeeds then it prints out the correct output. But if it fails it needs to print out this help message, "Usage..." (It's long and the help message itself isn't that important.)
I already figured out how to accept multiple number of text files to concatenate them but the problem is the divider really.
I also figured out how to show the help message using cat but the problem is that I don't know what conditions I need to put for the if and else statement because the conditions itself isn't working as intended.
For example, if it can find txt files and print the output it works as intended, however if there is no txt file it doesn't print the help message.
I am pretty sure it has to be some kind of loop statement for the divider to work but the problem is I have no idea how to do it. Not even sure of the conditions.
# Don't know what condition to put for the if here
if [ $? -eq 0 ]
then
# Don't know what condition to put for the for loop here
for j in "$?"
do
cat *.txt
echo "-----"
done
exit 0
else
cat <<< "Usage..." >&2
exit 1
fi
Let's say we have 2 text files.
one.txt has:
Hello
two.txt has:
Happy
Then if the code runs as intended the output should be:
Hello
-----
Happy
-----
Something amongst those lines ?
#!/bin/bash
for param in $#; do
if [[ -f $param ]]; then
cat $param
echo "-----"
else
echo "wrong parameter passed! usage:..."
exit 1
fi
done
exit 0
To explain quickly :
for param in $#; do
Iterate over every parameter passed to the script
[[ -f $param ]]
This checks whether the parameter is an existing file

Bash: For Each Line in a File, Set variable

I am trying to iterate through a text file, and set a variable once for each line.
I am having trouble doing so. I have simplified the process so that it is just echoing the result. There would be more going one, but this part of the widget is not working correctly.
while read p; do
set $FN=`echo $p`
echo $FN
done <file.txt
When I don't use set, it looks like it is trying to echo a file called the contents of $p.
I am using echo $p to send the line to a series of cuts, to get the data I want.
I know I am doing something fundamentally wrong, just not sure what that might be.
This works for me. Just say "FN=" without the "set
while read p; do
FN=$p
echo $FN
done < test.list

How to retrieve all code between two words in shell?

I need your help.
I have a text file with many lines and for each line i want to extract a code.
This code is betwwen two pattern :
1) "quarantine"
2) ","
Exemple of my log.txt :
George Thibault give two balls quarantine: x27041992, to someone
Edward Thin give three PC'S quarantine : m5405051993, to Fed
Tommy Dijoux give one shoe quarantine : cD001252000, to luc
Wanted result in new file :
x27041992
m5405051993
cD001252000
Someone can help me please ?
Try using the following bash script
#!/bin/bash
>output
while read line; do
for word in $line; do
if [[ $word == *","* ]]; then
echo ${word::-1} >> output
fi
done
done < gr
Note: Using shell loop to process text file is not a good practice.
read this
Match the substring between quarantine and the first ,, and replace everthing with the remembered string.
When you alse wont to delete a :, change this command.
sed 's/.*quarantine\([^,]*,.*/\1/' log.txt

Using bash to edit text file and display output

I have a text file with courses that looks like this:
csc 4567 - Computer Programming
This course is about stuff...blah blah blah
4.0000 credit hours
I need to write a bash script that takes the text file and extracts the necessary information to display course number, course name, course credit hours. No spaces, just commas between. Course credit hours as whole number.
I can extract the information using command line but I am lost when writing the bash script. I am guessing I should be using the grep and sed with bash? I'm not sure. This is what I have (which is obviously not anywhere near correct). Any advice would be appreciated.
while read courses.txt
do
$courseLine=grep -iE '^csc [0-9]{4}'
$creditHours=grep -iE '^[0-4]'
$courseNumber=${courseLine#/s/s*}
$courseName=${courseLine%/s/s*}
printf "$courseNumber,$courseName,$creditHours"
done
Above what I am trying to accomplish is: read the file line
save "csc 4567 - Comp prog" as course line
save "4" as credit hours
save everything left of second space in course line as course number
save everything right of second space in course line as course name
then display my desired output.
It should print csc4567,Computer Programming,4
And yes, this is homework, but since I am new to the forum it won't let me tag the post as such.
#/bin/bash
while read line
do
if [[ $line =~ ^(csc\ [0-9]+)\ \-(.*)+ ]]; then
a="$a${BASH_REMATCH[1]},${BASH_REMATCH[2]}"
elif [[ $line =~ ^([0-9]+)([0-9\.\,])+\ credit ]]; then
a="$a,${BASH_REMATCH[1]}\n"
#else
# a="$a,$line"
fi
done < "a1.txt"
echo -e $a

Directing awk output to variable

New guy here with a problem that will hopefully have an easy solution, but I just can't seem to manage.
So, I have a large list of files that I need to process using the same command line program, and I'm trying to write a small shell script to automate this. I wrote something that will read the input file name from a text file, and repeat the command for each of those files. So far so good. My problem though is with naming the output. Each file is named in the general format "lane_number_bla_bla_bla", and they are processed in pairs. So, there will be a "lane_1_bla_bla_bla_001" and "lane_1_bla_bla_bla_002" that need to combine into a single output file. For this, I'm trying to use awk to read the sample number from the .txt list of input files and parse it into the output file number. Here's the code I came up with (note that the echo statement before the command is there just for testing; it's removed when it comes to run the actual program; also this is not the actual command which is rather more complicated, but the principle still applies):
echo "Which input1 should I use?"
read text
input1=$text
echo "Which input2 should I use?"
read text
input2=$text
echo "How many lines?"
read text
n=$text
for i in $(seq 1 $n)
do
awkinput1=$(awk NR==$i $input1)
awkinput2=$(awk NR==$i $input2)
num=$(awk 'NR==$i{print $2 }' FS="_" $input1)
lane=$(awk 'NR==$i{print $1 }' FS="_" $input1)
echo "command $awkinput1.in > $awkinput1.out && command $awkinput2.in > $awkinput2.out && command cat $awkinput1.out $awkinput2.in > $num-$lane-CAT.out &"
if (( $i % 10 == 0 )); then wait; fi # Limit to 10 concurrent subshells.
done
When I run this, both $awkinput fields get replaced properly in the comand line by the appropriate filename, but not the $num and $lane fields, which print nothing.
So, what am I doing wrong? I'm sure it's pretty simple, but I tried quite a lot of different ways to format the relevant awk command, and nothing seems to work. I'm doing this on a remote linux server using SSH protocol, if it makes a difference.
Thanks a lot!
Shell does not parse $i quoted by single quote ('). So quoted string should be terminated before $i.
FS should be set before parsing lines.
Following code will work.
num=$(awk 'BEGIN{FS="_"} NR=='$i'{print $2 }' $input1)
lane=$(awk 'BEGIN{FS="_"} NR=='$i'{print $1 }' $input1)
Code below will be more efficient:
while read in1 ; do
read in2 <&3
num=$(awk 'BEGIN{FS="_"} {print $2 }' <<<"$in1")
lane=$(awk 'BEGIN{FS="_"} {print $1 }' <<<"$in1")
...
done <$input1 3<$input2

Resources