LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-23-2021, 01:19 PM   #16
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?

Im sorry for my post with a similar question. When I tried to use | awk -F\. it did what I thought so I tried to use | awk -F\_ and it failed. Then I tried to use | awk -F_ and it failed. So from there I wasnt sure where to go... hence the other post.

After doing some googling on sed and awk split() I found some other kind of examples and ended up figuring it out again. I tried a bunch of different methods but seem to get confusing to me and the output wasnt expected.

I went with a similar method and just worked around the output...


line = sed.ex.example.com descriptive text "re=sed_ex&source=%24ext"

Code:
echo $line | awk '{print $4}' | tee /tmp/output.t | awk -F"_" '{print $1"_"}'
gave me re=sed_

Code:
echo $line | awk '{print $4}' | tee /tmp/output.t | awk -F"_" '{print $2}'
gave me ex&source=%24ext

awk -F"_" is what triggered me to go with the same method. Dont know what I didnt try that before the post. I guess a quick swift in the head from comments here helped me think about it some more. Thanks again!

Last edited by CyberIT; 09-23-2021 at 01:26 PM.
 
Old 09-23-2021, 01:20 PM   #17
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,382
Blog Entries: 3

Rep: Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773
How well did the split() method shown in #10 above work, or not work, for you?
 
Old 09-23-2021, 01:30 PM   #18
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
How well did the split() method shown in #10 above work, or not work, for you?
I couldnt get it to work and for me got a little confusing...
Thanks!
 
Old 09-24-2021, 01:50 AM   #19
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,688

Rep: Reputation: Disabled
I don't quite understand what you're trying to achieve. If it's just to insert something after the _ then this will do:
Code:
sed -E 's/(.*_)(.*)/\1<newinfo>\2/'
as you were told
Quote:
Originally Posted by TB0ne View Post
If it's always between an underscore and "ex", a sed replacement should be fairly trivial...have you tried that?

Last edited by shruggy; 09-24-2021 at 02:06 AM.
 
1 members found this post helpful.
Old 09-24-2021, 01:53 AM   #20
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,382
Blog Entries: 3

Rep: Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773Reputation: 3773
Quote:
Originally Posted by CyberIT View Post
I couldnt get it to work and for me got a little confusing...
Thanks!
By the way, please show at least a few lines of sample input and the corresponding desired output. The one line you have does not look quite like a SRV record though it does appear similar.

As for #10 above, could you be more specific about what you tried?

Here is a slightly modified version and a breakdown piece by piece:

Code:
record="SRV"

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]}
The example is shell scripting, Bash to be specific.

In Bash, a=(...) means make an array. See:

Code:
a=("foo" "bar")
echo ${a}
echo ${a[0]}
echo ${a[1]}
And $(...) is command substitution. That is where what's between the parentheses is evaluated and used as input. Compare:

Code:
echo "Right now it is date +'%F %T'"

echo "Right now it is $(date +'%F %T')"
The AWK script works if you set the environment variable "$record" in the shell first.

-vr="$record" makes the shell variable $record available within the AWK script as the internal variable "r".

$0~r {...} means search each line for what's in the variable "r" and then executes what's between the braces if it is found. The "$0" stands for the whole line or record. The tilde ~ is the pattern search and the contents of the variable "r" is what is searched for.

l=split($NF,a,/\./);

The variables themselves have no dollar sign in front, and adding a dollar sign changes the meaning. This is fundamental to AWK. See the reference manual for the AWK scripting language, "man awk", or Bruce Barnett's AWK grymoire before proceeding.

AWK has some built-in variables. One built-in variable is NF which holds the number of fields in the current record being examined. In other words, NF contains the number of the last field. Thus $NF is the content of the last field, just like $1 is the conent of the first field, and $2 is the content of the second field and so on. So the split() function divides the content "$NF" of that last field , which should be "mdb-nkj.ex.example.com." in your example, using a dot \. as the separator. The results are stored as an array in the variable "a" and the size of that resulting array is stored in the variable "l". In other words "a[l]" would point to the last element in the array "a" and "a[0]" would indicate the first element in the array "a".

print a[l-4],a[l-3]}')

Then since the variable "l" now contains the number of the last element in the array, the fourth-from-last and third-from-last array elements are printed, separated by the Output Field Separator. The default for that is one space. That output field separator can be changed by setting the OFS variable in AWK.

However, all that might be a lot more than what you need or it might just be very different than what you need. So, again, please show at least a few more lines of sample input and the corresponding desired output.
 
1 members found this post helpful.
Old 09-24-2021, 06:04 PM   #21
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
I don't quite understand what you're trying to achieve. If it's just to insert something after the _ then this will do:
Code:
sed -E 's/(.*_)(.*)/\1<newinfo>\2/'
as you were told

Thank you and yes that is all Im trying to do. Im still not understanding all of this code here...

Code:
sed -E 's/(.*_)(.*)/\1<newinfo>\2/'
 
Old 09-24-2021, 09:04 PM   #22
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,844

Rep: Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222Reputation: 1222
Do you need to capture so much?
Code:
sed -E 's/(.*_)/\1<newinfo>/'
captures one group that spans up to the (rightmost) _
and gives it back as \1 (what the 1st capture group matched).
Code:
sed -E 's/(_)/\1<newinfo>/'
captures the first _
and the \1 gives it back.
Now the 1st capture group is a single literal character, and you can give it back literally, without using a capture group:
Code:
sed -E 's/_/_<newinfo>/'
You can give back the full match with a &
Code:
sed -E 's/_/&<newinfo>/'
Rule:
What matches on the left side (Regular Expression) is "absorbed". A portion to be kept must be given back on the right side (replacement string).

You can use awk to first split into fields and then run a substitution on only one field. This can prevent from unwanted matches in other fields.
The sub() works like the s command in sed -E
It does not support the \1 \2 references to capture groups (I think), but has the & reference to the full match.
An (optional) 3rd argument is a variable or a field.
Code:
echo "$line" | awk -v repl="<newinfo>" '{sub(/_/, ("&" repl), $4); print}'

Last edited by MadeInGermany; 09-24-2021 at 09:07 PM.
 
  


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 10:22 AM.

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