LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-01-2019, 08:05 AM   #1
fribse
LQ Newbie
 
Registered: Mar 2019
Posts: 7

Rep: Reputation: Disabled
Question 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.

So... What to do :-)
 
Old 03-01-2019, 08:26 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by fribse View Post
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.
Code:
extract=$(echo $i | cut -f 6)
 
Old 03-01-2019, 08:36 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
cli agrees,
Code:
userx@slackcurdvd.net:~
$ i="123456 78910"
 
$ ex=$(echo "$i" | cut -f6)
 
$ echo $ex
123456 78910
 
$ ex=$(echo "$i" | cut -f 6)
 
$ echo $ex
123456 78910
you might also want to look up cut
https://en.wikibooks.org/wiki/Cut

to chop off up to the 6th
Code:
${string:position}
$ echo ${i:6}
78910

Last edited by BW-userx; 03-01-2019 at 08:49 AM.
 
Old 03-01-2019, 03:03 PM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by fribse View Post
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
your example doesn't use arrays, just strings.

however, in bash you can use arrays!
which would make the assignment somewhat easier:
Code:
extract="${array[5]}"
yes, that's 5 for the sixth element because the indeces start at 0 (zero).
 
1 members found this post helpful.
Old 03-02-2019, 02:16 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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.
 
Old 03-02-2019, 02:29 AM   #6
notKlaatu
Senior Member
 
Registered: Sep 2010
Location: Lawrence, New Zealand
Distribution: Slackware
Posts: 1,077

Rep: Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732
You can obtain an item from a Bash array like this:

echo ${i[5]}

The braces insulate the array from being treated as a plain old variable. The number in the brackets is the item number, starting at 0.
 
Old 03-05-2019, 02:02 AM   #7
fribse
LQ Newbie
 
Registered: Mar 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
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).
 
Old 03-05-2019, 02:26 AM   #8
fribse
LQ Newbie
 
Registered: Mar 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
The script is now:

Code:
SEARCHP=""
for i in "${hits_array[@]}"
do
   echo "$i"
   SEARCHP+=$(echo "$i" | cut -f 7 )
   done <<< $i
And that works!
Now on to a grep with multiple pattern or :-) (where this string will be used).

Thankyou again for the help!
 
Old 03-05-2019, 03:42 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,123

Rep: Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371Reputation: 7371
Quote:
Originally Posted by fribse View Post
If I do
extract="${echo "$i" | cut -f 6}"
if doesn't work because of the quote's inside the quotes.
This work, if you use ( ) instead of { }.
Code:
extract="$(echo "$i" | cut -f 6)"
Quote:
Originally Posted by fribse View Post
The script is now:

Code:
SEARCHP=""
for i in "${hits_array[@]}"
do
   echo "$i"
   SEARCHP+=$(echo "$i" | cut -f 7 )
   done <<< $i
here i used outside the for loop and also as cycle variable. That will may cause confusion later.

Also try to use shellcheck to check/improve your script.
 
Old 03-05-2019, 04:24 AM   #10
fribse
LQ Newbie
 
Registered: Mar 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
So the script changed a bit
Code:
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.

Weehee, it's getting there :-)
 
Old 03-05-2019, 04:29 AM   #11
fribse
LQ Newbie
 
Registered: Mar 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
Also try to use shellcheck to check/improve your script.
Great tip, thankyou!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Xombrero: can't see text entered in address field and search field xiongnu Linux - Software 1 01-23-2016 08:20 AM
[SOLVED] Count occurrence of character in field and print in a new field Trd300 Linux - Newbie 5 03-21-2012 07:57 PM
Can I execute a shell command and put the result in command field? fran4tw Linux - General 9 08-08-2011 08:27 PM
[SOLVED] Grep for the result of a command within the result of another command jasonws Programming 6 11-18-2010 02:39 PM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration