Why This Bash Function Prints Only First Word of Whole String

Why this bash function prints only first word of whole string?

For me, I had to change the header for "#!/bin/bash", but apparently that is not the problem for you.

In your echo you are printing only the first word with the $1, if you change it to $2 you will print the second word (parameter) and so on.

You can pass the name inside quotes or print all the parameters with $@

Solution 1 (with $@):

lbGREEN='\e[1;92m'
NC='\e[0m'
normalMessage="Everything fine"
echo_message() {
echo -e ${lbGREEN}$@${NC}
}
echo_message $normalMessage

Solution 2 (with quotes):

lbGREEN='\e[1;92m'
NC='\e[0m'
normalMessage="Everything fine"
echo_message() {
echo -e ${lbGREEN}$1${NC}
}
echo_message "$normalMessage"

printing first word in every line of a txt file unix bash

To print a whole word, you want -f 1, not -c 1. And since the default field delimiter is TAB rather than SPACE, you need to use the -d option.

cut -d' ' -f1 filename

To print the last two words not possible with cut, AFAIK, because it can only count from the beginning of the line. Use awk instead:

awk '{print $(NF-1), $NF;}' filename

Only print first and second word of each line to output with sed

In GNU sed

sed -E '/^\s*(#.*)?$/d; s/^\s*(\S+)\s+(\S+).*/\1 \2/' pattern.txt

Update after the comments:

sed -E '/^\s*(#.*)?$/d; s/^\s*(\S+)\s+("[^"]*"|\S+).*/\1 \2/' pattern.txt

Bash function with array won't work

The following line seems suspicious

    for i in $arr ; do

I changed it as follows and it works for me:

#! /bin/bash

function storeDevNames {
n=0
b=0
while read line; do
# line=$line # ?!
tempArr=( $line )
name=${tempArr[2]}
for i in "${arr[@]}" ; do
if [ "$i" == "$name" ]; then
b=1
break
fi
done
if [ "$b" -eq 0 ]; then
arr[n]=$name
(( n++ ))
fi
b=0
done
}

storeDevNames < <(cat <<EOF
1 2 first 3
4 5 second 6
7 8 first 9
10 11 third 12
13 14 second 15
EOF
)

echo "${arr[@]}"

Bash Script To Find Dollar Words Not As Fast As Was Hoping

As @thatotherguy pointed out in a comment, there are two big problems here. First, the way you're reading lines from the file reads the entire file every line. That is, to read the first line you run sed -n "1"p Words.txt, which reads the entire file and prints only the first line; then you run sed -n "2"p Words.txt, which reads the entire file again and prints only the second line; etc. To fix this use a while read loop:

while read word; do
...
done <Words.txt

Note that if anything inside the loop tries to read from standard input, it'll steal some of the input from Words.txt. In that case you can send the file over FD #3 instead of standard input with while read -u3 ... done 3<Words.txt.

The second problem is this bit:

occurences["$i"]=$(echo $word | grep ${letter["$i"]} -o | wc -l)

...which creates 3 subprocesses (echo, grep, and wc), which isn't too bad except that this runs 26 times for each and every word in the file. Creating processes is expensive compared to most shell operations, so you should do your best to avoid it especially in loops that run many many times. Try this instead:

matches="${word//[^${letter[i]}]/}"
occurences[i]="${#matches}"

This works by replacing all characters that aren't ${letter[i]} with "", then looking at the length of the resulting string. The parsing happens entirely in the shell process, so it should be much faster.

bash get first word last line

Using awk:

awk '{w=$1} END{print w}' file

OR using sed:

sed -n '$s/^\([^ ]*\).*$/\1/p' file

In Bash, how can I check if a string begins with some value?

This snippet on the Advanced Bash Scripting Guide says:

# The == comparison operator behaves differently within a double-brackets
# test than within single brackets.

[[ $a == z* ]] # True if $a starts with a "z" (wildcard matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).

So you had it nearly correct; you needed double brackets, not single brackets.


With regards to your second question, you can write it this way:

HOST=user1
if [[ $HOST == user1 ]] || [[ $HOST == node* ]] ;
then
echo yes1
fi

HOST=node001
if [[ $HOST == user1 ]] || [[ $HOST == node* ]] ;
then
echo yes2
fi

Which will echo

yes1
yes2

Bash's if syntax is hard to get used to (IMO).



Related Topics



Leave a reply



Submit