The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
To cut entire column from a file and apend it to another file as another column sakthifire Shell Programming and Scripting 4 06-25-2008 01:27 AM
Inserting a column in a file dhanamurthy Shell Programming and Scripting 7 05-11-2008 07:29 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 06:57 AM
Replace 10th column with a new column--- Terriblly hurry ahmedwaseem2000 Shell Programming and Scripting 2 09-05-2005 10:10 PM
Splitting file using value in a column gboom Shell Programming and Scripting 3 05-22-2005 08:07 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-28-2005
Registered User
 

Join Date: Aug 2005
Posts: 75
cut the third column from a file

I have a text file which has the following data. There can be more lines in the file. But, I am only interested in these two lines which has ~ZZ~VISTN and ~ZZ~F159B segments.

ISA~00~ ~00~ ~ZZ~VISTN ~ZZ~U1CAD ~051227~183
7~U~00200~000011258~0~P~<

ISA~00~ ~00~ ~ZZ~F159B ~ZZ~U1CAD ~051227~191
3~U~00200~000011467~0~P~<

I am to get this third column from the file and store it in some variable and then compare if it is "~ZZ~VISTN" it is passed otherwise it is fail.

In oder to get the third column, I am using the cut command along with grep but it does not seems to be working and it is returning the whole line.

grep -n ~ZZ~ success.lst | cut -f3 > pass.txt.

The desired result is "~ZZ~VISTN and ~ZZ~F159B but it is returning the whole data. Maybe I am missing something here. Please advise.

Regards,
Inder
Reply With Quote
Forum Sponsor
  #2  
Old 12-28-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Code:
nawk '{ print ($3 == "~ZZ~VISTN" || $3 == "~ZZ~F159B") ? "passed" : "failed" }' myFile
Reply With Quote
  #3  
Old 12-28-2005
Registered User
 

Join Date: Aug 2005
Posts: 75
Hi Vgersh99,

Thanks much for the reply!!

The solution you have provided in your post did not really work.

When I run the command it is returning lots of rows with passed and failed, when there are two rows with ~ZZ~, the data I am interested in.

If I want to grep the value of third column and store it in some variable, How I can achieve that. As I said, when I tried cut it did not gave me the desired results.

Regards,
Inder
Reply With Quote
  #4  
Old 12-28-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Quote:
Originally Posted by isingh786
Hi Vgersh99,

Thanks much for the reply!!

The solution you have provided in your post did not really work.

When I run the command it is returning lots of rows with passed and failed, when there are two rows with ~ZZ~, the data I am interested in.
that's kin of strange - works just fine here. If you want you can post your sample file.
Quote:
Originally Posted by isingh786
If I want to grep the value of third column and store it in some variable, How I can achieve that. As I said, when I tried cut it did not gave me the desired results.

Regards,
Inder
assuming your fields are 'space' separated:
..... | cut -d' ' -f3
Reply With Quote
  #5  
Old 12-28-2005
Registered User
 

Join Date: Aug 2005
Posts: 75
Hi Vgersh99,

Thanks for the prompt reply!!!

This is the data file I am talking about.

ISA~00~ ~00~ ~ZZ~VISTN ~ZZ~U1CAD ~051227~183
7~U~00200~000011258~0~P~<
GS~FA~EE05J~U1CAD~051227~1831~000011258~X~002002
ST~997~0001
AK1~SH~247
AK2~856~2470001
AK5~A
AK2~856~2470002
AK5~A
AK9~A~2~2~2
SE~8~0001
GE~1~000011258
IEA~00001~000011258


ISA~00~ ~00~ ~ZZ~F159B ~ZZ~U1CAD ~051227~191
3~U~00200~000011467~0~P~<
GS~FA~AF52M~U1CAD~051227~1913~000011467~X~002002
ST~997~0001
AK1~SH~53
AK2~856~530001
AK5~A
AK9~A~1~1~1
SE~6~0001
GE~1~000011467
IEA~00001~000011467

And I am interested in every first line which starts with ISA, what I am trying to do is to get the value of third field which is ~ZZ~F159B and ~ZZ~VISTN, If it is ~ZZ~VISTN then it is PASS and if it is ~ZZ~F159B then it is fail.

I tried the cut command also which you send and that also did not work for me. I believe that I am doing something wrong.

Anyways, Thanks much for your help!!!

Regards,
Inder
Reply With Quote
  #6  
Old 12-28-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
well.... that's quite a different description.
try this:

nawk -f is.awk myFile

is.awk:
Code:
BEGIN {
  SEP=FS
  RS=FS=""
}
$1 ~ /^ISA~/ { split($1, arr, SEP); print (arr[3] == "~ZZ~VISTN") ? "passed" : "failed" }

Last edited by vgersh99; 12-28-2005 at 04:37 PM.
Reply With Quote
  #7  
Old 12-28-2005
Registered User
 

Join Date: Nov 2005
Posts: 2
Try this.
grep "~ZZ~" <data file> | nawk '{ print ($3=="~ZZ~VISTN") ? "passed":"failed" }'
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0