BASH: Trying to extract a field from a variable, and place the result in a new variable
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
BASH: Trying to extract a field from a variable, and place the result in a new variable
Hi All
I'm surely missing something here, so please help me out :-)
It's CentOS 7, and a bash script.
I have an array. In that array the lines have fields seperated by tabs.
I want to extract the sixth field and place it in a variable for use in a grep, so I thought of doing this:
This command extracts the field:
echo "$i" | cut -f 6
But how do I place the field in a variable?
If I do
extract="${echo "$i" | cut -f 6}"
if doesn't work because of the quote's inside the quotes.
Doing
extract=${echo "$i" | cut -f 6}
Gives me an error with 'bad substition'.
I also tried escaping the 'inner' quotes, but it's the same reply.
If I do
extract="${echo "$i" | cut -f 6}"
if doesn't work because of the quote's inside the quotes.
Doing
extract=${echo "$i" | cut -f 6}
Gives me an error with 'bad substition'.
You don't need quotes around the $i.
But your problem is somewhere else. In order to assign the output of a command to a variable, you need to use the $(...) construct, e.g.
To OP, you have provided your attempt, but not an example of the input and your desired output.
This would best help to assist you in a solution. As has been pointed out, you mention arrays but use none in your example, so you may also need to show how you are assigning
the original variable, "i" in your example.
Thankyou all for the invaluable help, doh, mistypings with the $(, thankyou Berndbusch! My bad eyes couldn't see that in putty.
Doing the '$(i:6)' won't that cut of at the 6'th character, and not the 6'th word?
You are right that this line is not an array, but I have several lines in an array, and are going through the array, to extract the same positition from each line (thus emulating a two dimensional array).
SEARCHP=""
SEPARATOR="|"
for i in "${hits_array[@]}"
do
echo "$i"
SEARCHP+=$(echo "$i" | cut -f 7 | sed 's/.$//')$SEPARATOR
done <<< $i
# Clean up the output to a OR list to be used in grep
SEARCHP=$(echo $SEARCHP | sed 's/.$//' )
So now I've gotten the grep created, I have to use both an 'OR' and an 'AND' grep.
grep -E "$SEARCHP" /var/log/maillog | grep "DKIM-Signature field added" | awk 'BEGIN{FS=OFS=" ";}{print $6,$11;}' | sed 's/.$//'
The first is the OR part, it has | in between the patterns from the script, but I can't seem to find a way to combine it to an AND, other than doing the grep again, on the results.
The last bits just takes the words I'm interested in, and removes the paranthesis that's shown at the end of the second field shown.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.