LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-21-2021, 05:47 PM   #1
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Rep: Reputation: Disabled
Question need help with awk '{print $1}'


Good Evening!

I have this requirement that I need to print a certain portion of an SRV record and I could use some help on how to determine the section to print.

Code:
_mon._tcp.tax.ex.example.com has SRV record 0 5 3000 mdb-nkj.ex.example.com.
I need to be able to capture and print to screen this portion of the record above.

Code:
ex
Is that even possible to do? I figured I could grep something and then start the printing from there??

Code:
echo _mon._tcp.tax.ex.example.com has SRV record 0 5 3000 mdb-nkj.ex2.example.com. | grep -w "record" | awk -F\. '{print $6}'
Any help is much appreciated!
Thanks!

Last edited by CyberIT; 09-21-2021 at 06:20 PM.
 
Old 09-21-2021, 05:55 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
Put the string to be echo'd in double quotes and see how you go.
awk can handle it all in a single invocation. as can grep as it happens, but stick with getting what you have working first.
 
Old 09-21-2021, 06:09 PM   #3
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
Put the string to be echo'd in double quotes and see how you go.
awk can handle it all in a single invocation. as can grep as it happens, but stick with getting what you have working first.
my apologies but Im not grasping what you are saying to put into double quotes?
 
Old 09-21-2021, 06:18 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
I meant echo "..." but it doesn't matter here. Look at what you get out, then start counting your fields.
 
Old 09-21-2021, 06:22 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,748

Rep: Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927
The grep command as posted outputs the entire string regardless of where the match lies.

Code:
_mon._tcp.tax.ex.example.com has SRV record 0 5 3000 mdb-nkj.ex2.example.com.
 $1   $2  $3 $4    $5                  $6                   $7   $8     $9
Here is a hint. I've spelled out what syg00 (sorry...) is trying to post. Your splitting the string based on the location of each period (.). $1 is the first word from the start of the string to the first period and so on.

Last edited by michaelk; 09-21-2021 at 06:24 PM.
 
1 members found this post helpful.
Old 09-21-2021, 06:40 PM   #6
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
yep. I understand that part of it. The grep part isnt doing anything for me besides matching it. So I was trying to figure out the fields

Code:
_mon._tcp.tax.ex.example.com has SRV record 0 5 3000 mdb-nkj.ex.example.com.
                                                        $x   $x  $x     $x
so is this what you are saying instead of echo'n this from the line...

Code:
_mon._tcp.tax.ex.example.com has SRV record 0 5 3000 mdb-nkj.ex.example.com.
echo this from the line instead?

Code:
echo mdb-nkj.ex.example.com. | awk -F\. '{print $2}'

Last edited by CyberIT; 09-21-2021 at 06:41 PM.
 
Old 09-21-2021, 06:46 PM   #7
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by CyberIT View Post
yep. I understand that part of it. The grep part isnt doing anything for me besides matching it. So I was trying to figure out the fields

Code:
_mon._tcp.tax.ex.example.com has SRV record 0 5 3000 mdb-nkj.ex.example.com.
                                                        $x   $x  $x     $x
so is this what you are saying instead of echo'n this from the line...

Code:
_mon._tcp.tax.ex.example.com has SRV record 0 5 3000 mdb-nkj.ex.example.com.
echo this from the line instead?

Code:
echo mdb-nkj.ex.example.com. | awk -F\. '{print $2}'


Actually how about this...

Code:
echo _mon._tcp.tax.ex.example.com has SRV record 0 5 3000 mdb-nkj.ex.example.com. | echo mdb-nkj.ex.example.com. | awk -F\. '{print $2}'

Now I need to do the same for multiple lines in a file. Is it possible to echo a portion of a line within a current echo?


line = _mon._tcp.tax.ex.example.com has SRV record 0 5 3000 mdb-nkj.ex.example.com.


Code:
echo $line | awk '{print $8}' | echo the output of awk '{print $8}' | awk -F\. '{print $1"."$2}'

line | mdb-nkj.ex.example.com. | echo mdb-nkj.ex.example.com. | awk -F\. '{print $1"."$2}'
ending result would be mdb-nkj.ex

Last edited by CyberIT; 09-21-2021 at 07:03 PM.
 
Old 09-21-2021, 10:44 PM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Quote:
Originally Posted by CyberIT View Post
Now I need to do the same for multiple lines in a file. Is it possible to echo a portion of a line within a current echo?
AWK scripts can be pointed at an actual file.

Code:
awk '{print $NF}' /path/to/list.of.srv.records
By default, it will read one line at at time but that can be modified by changing the RS variable. See "man awk"

Also, is the "record" string which you have supposed to be a variable?

Code:
awk -vr="$record" '$0~r {print $NF}' /path/to/list.of.srv.records
As for the SRV record from DNS, does the target field always contain the same number of dots? If not, you may want to count them from the right instead of from the left. So creating an array from a split() function would work for that. By the way, the output of the split() function is the length of the new array.
 
1 members found this post helpful.
Old 09-23-2021, 09:40 AM   #9
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
Thank you all for the comments and help! Much appreciated! I have figured it out by using this approach...


line = _mon._tcp.tax.ex.example.com has SRV record 0 5 3000 mdb-nkj.ex.example.com.

Code:
 echo $line | awk '{print $8}' | tee /tmp/output | awk -F\. '{print $1}'
gives me mdb-nkj

Code:
 echo $line | awk '{print $8}' | tee /tmp/output | awk -F\. '{print $2}'
gives me ex

Code:
 echo $line | awk '{print $8}' | tee /tmp/output | awk -F\. '{print $3"."$4}'
gives me example.com

Last edited by CyberIT; 09-23-2021 at 09:42 AM.
 
1 members found this post helpful.
Old 09-23-2021, 09:51 AM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
If you just need the one part, you could also do it like this:

Code:
awk -vr="record" '$0~r {l=split($NF,a,/\./); print a[l-3]}'
If you need several parts, you could be sure to use Bash or Zsh and then catch the output as an array. The use of tee is not needed if you use command substitution with the AWK script.

Code:
a=($(echo _mon._tcp.tax.ex.example.com has SRV record 0 5 3000 mdb-nkj.ex2.example.com.  \
| awk -vr="record" '$0~r {l=split($NF,a,/\./); print a[l-4],a[l-3]}'))

echo Array element 0 = ${a[0]}
echo Array element 1 = ${a[1]}
 
Old 09-23-2021, 09:56 AM   #11
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
Question need help with awk

Good Morning!

I have a requirement that I need to print a certain portion of a TXT record and I could use some help on how to determine the section to print.

Code:
sed.ex.example.com descriptive text "re=sed_ex&source=%24ext"
I need to be able to break this portion out so I can insert new info between the underscore _ and ex and print to screen.

Code:
sed.ex.example.com descriptive text "re=sed_<newinfo>ex&source=%24ext"
Is that even possible to do?

Any help is much appreciated!
Thanks!
 
Old 09-23-2021, 10:00 AM   #12
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,680

Rep: Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971
Quote:
Originally Posted by CyberIT View Post
Good Morning!
I have a requirement that I need to print a certain portion of a TXT record and I could use some help on how to determine the section to print.
Code:
sed.ex.example.com descriptive text "re=sed_ex&source=%24ext"
I need to be able to break this portion out so I can insert new info between the underscore _ and ex and print to screen.
Code:
sed.ex.example.com descriptive text "re=sed_<newinfo>ex&source=%24ext"
Is that even possible to do? Any help is much appreciated! Thanks!
Very possible...may even want to check your other thread where you asked essentially the same question:
https://www.linuxquestions.org/quest...27-4175700930/

You have been using Linux for four years now; using awk/sed/grep should be familiar to you, and there are MANY examples and tutorials you can find, including the advice you've been given before.

Why don't you post what YOU have written/done/tried, and tell us where you're stuck. Since this is your 'requirement', you must be working on it, right??? If it's always between an underscore and "ex", a sed replacement should be fairly trivial...have you tried that?

Last edited by TB0ne; 09-23-2021 at 10:03 AM.
 
1 members found this post helpful.
Old 09-23-2021, 10:01 AM   #13
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
See "man awk" and scroll down to the split() function and you will see that it can operate on a single field and split that field into an array according to a specified pattern. It would be up to you to figure out the pattern and how to choose the appropriate parts of the array.

Can you provide several examples of full lines of input along with the corresponding desired output?
 
1 members found this post helpful.
Old 09-23-2021, 10:24 AM   #14
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
Very possible...may even want to check your other thread where you asked essentially the same question:
https://www.linuxquestions.org/quest...27-4175700930/

You have been using Linux for four years now; using awk/sed/grep should be familiar to you, and there are MANY examples and tutorials you can find, including the advice you've been given before.

Why don't you post what YOU have written/done/tried, and tell us where you're stuck. Since this is your 'requirement', you must be working on it, right??? If it's always between an underscore and "ex", a sed replacement should be fairly trivial...have you tried that?

Thank you for your reply! Im not very fluent with Linux even though my profile indicates that I joined this group 4yrs ago. Even though it has been 4yrs, Im still a beginner as I dont use Linux everyday.

My other post was using 'awk -F\.' which was splitting the string location by each period. This one is more of an insertion.

My apologies for asking a similar question. I didnt want to pollute the other post with a different scenario. I have used awk|grep but never sed. I will give that a try! Thank you again!
 
Old 09-23-2021, 10:28 AM   #15
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
AWK scripts can be pointed at an actual file.

Code:
awk '{print $NF}' /path/to/list.of.srv.records
By default, it will read one line at at time but that can be modified by changing the RS variable. See "man awk"

Also, is the "record" string which you have supposed to be a variable?

Code:
awk -vr="$record" '$0~r {print $NF}' /path/to/list.of.srv.records
As for the SRV record from DNS, does the target field always contain the same number of dots? If not, you may want to count them from the right instead of from the left. So creating an array from a split() function would work for that. By the way, the output of the split() function is the length of the new array.

I just saw this today ... after I posted what I had done. Thank you for your reply!
 
  


Reply

Tags
awk, linux, print, sed



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
[SOLVED] awk print $0 vs print $1, $2, $3, etc. vincix Linux - Newbie 8 04-28-2017 08:06 AM
[SOLVED] sed inside awk or awk inside awk maddyfreaks Linux - Newbie 4 06-29-2016 01:10 PM
[SOLVED] awk print dos not print text value jozelo Linux - Newbie 2 10-23-2013 04:37 AM
[SOLVED] Once again... awk.. awk... awk shivaa Linux - Newbie 13 12-31-2012 04:56 AM
Print-to-file print driver to print PDF Bill Fox Linux - General 3 05-02-2006 04:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:52 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