LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-01-2007, 04:25 AM   #1
kalyanofb
LQ Newbie
 
Registered: Feb 2007
Posts: 21

Rep: Reputation: 15
Awk Output


--------------------------------------------------------------------------------

i have one file (4geList) which contains two strings(filename) in each line seperated by '|'.

Using the following script:

awk -F '|' '{ printf "%s|%s",system("basename " $1 " .4gl"),system("basename " $2 " .4ge") }' 4geList

It outputs the two string as follows:

menu
main
0|0

My expectation is, it should come
menu|main


Please help me in this problem.

Kalyanaraman
 
Old 02-01-2007, 05:30 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986
What you get is the output of the first basename command, the output of the second one, and finally the exit code of the two commands printed separated by a pipe. The correct way to do this in awk, is:
Code:
gawk -F"|" '{( " basename " $1 )  | getline file1 ; ( " basename " $2 ) | getline file2 ; print file1 "|" file2}' 4geList
Here the construct command | getline var is a way to pass output from a shell command to an awk variable
 
Old 02-01-2007, 05:34 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 67
The system function executes an external command and returns the error level. basename happens to print to standard output, so you see the basename of the file in the terminal, but awk gets back only the error level (0 is success), which it puts into the printf placeholders, thus the output you see.

On a broader note, calling external commands should be kept to a minimum because each invocation is expensive. If you're going to have more than a few lines in this file, that's a lot of processes being created, and your program will be slow and un-reliable.

Much better would be to do the basename function internally.
Code:
awk -F'|' 'function bn(p) { n=split(p,a,"/"); return a[n]; }  { printf("%s|%s\n", bn($1), bn($2)); }'
If you are doing this in the middle of a shell script, there's no need even to invoke awk - the shell can do this processing easily enough
Code:
IFS="|"
while read f1 f2; do
        echo "${f1##*/}|${f2##*/}"
done < 4geList
Note that if you want to retain the original IFS value, you can enclose this section in parenthesis - it will then be executed in a sub-shell and prevent modification of the original IFS value.
 
Old 02-01-2007, 10:13 AM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Good thread. Here's another awk way to solve the same problem.

Code:
awk 'BEGIN{ FS="|"; OFS="|"; } 
{ 
sub(/.*\//,"",$1); 
sub(/.*\//,"",$2); 
print $1,$2; 
}' 4geList
For me, this is more straightforward and readable. (Probably because I wrote it. ). Nice to see the other creative / elegant ideas, though.
 
Old 02-02-2007, 02:23 AM   #5
kalyanofb
LQ Newbie
 
Registered: Feb 2007
Posts: 21

Original Poster
Rep: Reputation: 15
AWK - Help

thanks to all of u send the reply.



M. KALYANA RAMAN.
 
  


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
how to pipe/redirect awk output into a variable? johnpaulodonnell Linux - Newbie 2 01-25-2007 06:54 AM
the sound gives output when using mic but no output when run a music file medo Debian 0 04-19-2004 07:17 PM
Awk output Trouble fooforon Programming 6 04-12-2004 07:14 AM
How do I zip and attach the output data of a grep | awk | mail shell script? 360 Programming 1 05-08-2002 08:26 AM
Awk and Shell CMD Output xanthium Programming 16 04-24-2002 06:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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