LinuxQuestions.org
Help answer threads with 0 replies.
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 11-16-2018, 03:29 PM   #1
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Rep: Reputation: 0
Advise on the script


Hi Experts

Looking for small help

1. How to use AWK inside AWK
2. How to use cut/some unix commands inside awk

Ex:

echo "123:222,234:3333"|awk -F '[:,]' '{print $2}'
==> output : 222

$ echo "123:222,234:3333"|awk -F '[:]' '{print $1"-"$2"-"system("echo "$2|awk -F"," '{print $1}')}'
awk: {print $1"-"$2"-"system("echo "$2|awk -F"," {print
awk: ^ syntax error
awk: {print $1"-"$2"-"system("echo "$2|awk -F"," {print
awk: ^ syntax error

Here I want to get 3333


$ cat File.log
VAR1|11:1111,22:2222,33:3333|STD99
VAR2|A1:1212,A2:B2B2,A3:C3C3|STD98
VAR3|B1:6969,B3:9079,B3:9869|STD97


[oracle@pd4lmsdrdb01](+ASM1:/tmp)
$ cat File.log | awk -F "|" '{print $1"-"$2"-"$3"-"system("echo "$2"|awk -F '[:,]' '{print $2}'")}'
awk: {print $1"-"$2"-"$3"-"system("echo "$2"|awk -F [:,] {print
awk: ^ unterminated string

I want the output as below (showing for 1 line)

VAR1-11:1111,22:2222,33:3333-STD99|1111

Last edited by maddyfreaks; 11-16-2018 at 04:30 PM.
 
Old 11-16-2018, 03:54 PM   #2
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
Please let me know when I am making mistake

and using AWK inside AWK

Last edited by maddyfreaks; 11-16-2018 at 04:31 PM.
 
Old 11-16-2018, 05:13 PM   #3
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 4,828

Rep: Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763
Please use [code] tags for code and output.

It looks like your quotes are confused. You appear to have double quotes inside double quotes and single quotes inside single quotes. That's probably what's causing the "unterminated string" error. In the first example, there's a quote missing before the $1.

But I have to ask, if you want 3333 in your first example, why not
Code:
echo "123:222,234:3333"|awk -F '[:]' '{print $3}'
?? That is, change the delimiter to redefine the fields then print out the field you want.

Last edited by scasey; 11-16-2018 at 05:18 PM.
 
Old 11-16-2018, 05:25 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,885

Rep: Reputation: 3205Reputation: 3205Reputation: 3205Reputation: 3205Reputation: 3205Reputation: 3205Reputation: 3205Reputation: 3205Reputation: 3205Reputation: 3205Reputation: 3205
The simple answers are:
1. Don't
2. Don't

That might be the most convoluted code I've ever seen for such a simple task. Use a single instance of awk to do everything.
 
Old 11-16-2018, 08:00 PM   #5
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
but can you see how to use unix command inside awk using on one of the awk fields

this might not be so complicate code only thing is am not able to catch it.
 
Old 11-16-2018, 09:45 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,787

Rep: Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059Reputation: 3059
You need to advise us on what decisions need to be made when a line from the file is read. Obviously your current solution does not work but we don't understand how you came to that code
just that it does not work.

As mentioned by syg00, there would not be any reason to call awk inside awk, you just need to use what you have correctly.

So, presented with the following input:
Code:
VAR1|11:1111,22:2222,33:3333|STD99
What decisions were made that would make the output look like:
Code:
VAR1-11:1111,22:2222,33:3333-STD99|1111
To give you an idea, from a visual point of view there are 2 steps:

1. Change the pipe (|) into a hyphen (-)
2. Using pipe, colon and comma as separators, grab the third field and place at the end after change above and put a pipe in between
 
Old 11-16-2018, 11:17 PM   #7
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
Thats fine I was able to implement awk inside awk
Figured out
Thanks
 
Old 11-17-2018, 02:08 AM   #8
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 4,828

Rep: Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763Reputation: 1763
Quote:
Originally Posted by maddyfreaks View Post
Thats fine I was able to implement awk inside awk
Figured out
Thanks
Please tell us how. Show us what worked. That will help the next guy.

Thank you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Unable to run part of background shell script when invoked by PHP, Please advise. har_85 Programming 8 03-10-2015 05:41 AM
Need Advise in writing a Shell script using sed suDoh! Linux - Newbie 2 04-15-2014 04:50 AM
mysql script bash need advise packets Programming 6 01-10-2012 03:55 AM
Advise on patten matching script ajayjacobthomas Linux - General 7 05-17-2011 10:52 AM
Please advise on monitoring script pembo13 Linux - Software 1 07-22-2004 02:04 AM

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

All times are GMT -5. The time now is 04:46 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration