The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sequence number generation on a key column sbasetty Shell Programming and Scripting 6 03-27-2008 12:09 PM
Sorting in unix using column number swat UNIX for Advanced & Expert Users 1 11-15-2007 06:59 AM
Identifying the column number pondlife Shell Programming and Scripting 2 09-28-2007 10:43 AM
returning a column number scriptingmani Shell Programming and Scripting 2 07-06-2007 09:36 PM
awk to select a column from particular line number mab_arif16 Shell Programming and Scripting 4 05-08-2006 05:26 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-15-2008
jkl_jkl jkl_jkl is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 46
column number, awk, help

All,

Code:
$ cat myf.txt
A|xyz|1000|mm
B|9000|xyz|ss
C|BDE|2000|kk
D|xyz|1000|nn

I am searching "xyz"

$ awk -F "|" '
{for(k=0;k<=NF;k++)
if ( $k == "xyz" )
print "line="NR"(column="k")"
}' myf.txt

Output:
line=1(column=2)
line=2(column=3)
line=4(column=2)


I want to see the output as :

line=1(column=2)
line=2(column=3)
line=3(Not found)
line=4(column=2)
HTH,
jkl_jkl
  #2 (permalink)  
Old 03-15-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
It should be like:

Code:
awk -F "|" '
{found=0}
{for(k=0;k<=NF;k++)
   if( $k == "xyz" ){
     print "line="NR"(column="k")"
     found=1
   }
}
!found{print "line="NR" (not found)"}
' myf
Regards
  #3 (permalink)  
Old 03-15-2008
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almería, Spain
Posts: 393
Check this way:
Code:
> cat file
A|xyz|1000|mm
B|9000|xyz|ss
C|BDE|2000|kk
D|xyz|1000|nn
Code:
>  awk '{printf("%s","line="NR);a=gsub(/\|/,"",$1);if (a==3){print "(Not Found)";next}print "(column="a+1")"}' FS='xyz' file
line=1(column=2)
line=2(column=3)
line=3(Not Found)
line=4(column=2)
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0