cut the third column from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cut the third column from a file
# 1  
Old 12-28-2005
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
# 2  
Old 12-28-2005
Code:
nawk '{ print ($3 == "~ZZ~VISTN" || $3 == "~ZZ~F159B") ? "passed" : "failed" }' myFile

# 3  
Old 12-28-2005
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
# 4  
Old 12-28-2005
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
# 5  
Old 12-28-2005
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
# 6  
Old 12-28-2005
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 07:37 PM..
# 7  
Old 12-29-2005
Try this.
grep "~ZZ~" <data file> | nawk '{ print ($3=="~ZZ~VISTN") ? "passed":"failed" }'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut specific column from 2 file and merge

Hi ALL, I have two file. I need to combine these two file based on a layout. I used the below code and able to extract the record. But now able to insert that to a 3'rd file in between the extract FILE 1 CAID NUMBER 1-20 TID NUMBER 21-22 LABEL CHAR 23-44 BASE 45-60... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

2. Shell Programming and Scripting

Cut first column from file in Vi(Vim)

I have a parts file that looks like this: EE36264|0NH46||Y|A|EA|0|0|0|N|LUNETTE 3" ADJ. EYE|0|0|*|0|PEOZZU|N|12|N|N|VPS|N|N|N|N|LUNETTE 3" ADJ. EYE|0|||Receive into Inventory|81755|EE36264|*|*|*|0|0||EE36264|A|*|* F1.5|53932||Y|A|EA|0|0|0|N|FLAT ZERO CAL... (4 Replies)
Discussion started by: djehresmann
4 Replies

3. UNIX for Dummies Questions & Answers

Cut the two column from non delimited file

Hi, I have a question how to cut the column from non delimited file This is my file gene_id ENSG00000223972 transcript_id ENST00000456328 exon_number 1 gene_biotype pseudogene gene_name DDX11L1 transcript_name DDX11L1-002 tss_id TSS26614 gene_id ENSG00000223972 transcript_id ENST00000515242... (1 Reply)
Discussion started by: Wan Fahmi
1 Replies

4. Shell Programming and Scripting

Pick the column value based on another column using awk or CUT

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Please someone help me to resolve this issue. Source column1 column2 column3 column4... (2 Replies)
Discussion started by: Ganesh L
2 Replies

5. UNIX for Dummies Questions & Answers

How to cut from a text file based on value of a specific column?

Hi, I have a tab delimited text file from which I want to cut out specific columns. If the second column equals one, I want to cut out columns 1 and 5 and 6. If the second column equals two, I want to cut out columns 1 and 5 and 7. How do I go about doing that? Thanks! (4 Replies)
Discussion started by: evelibertine
4 Replies

6. Shell Programming and Scripting

cut the first row first column value

Hi All, file create2.txt contains . /test.txt ./create.txt ./test2.txt test.txt contains inb-1|kuuhsdkjfhsd|djshfjksd|ndsbs896 inb-2|kuuhsdkjfhsd|djshfjksd|ndsbs896 inb-3|kuuhsdkjfhsd|djshfjksd|ndsbs896create.txt contains abv-1|kjsdgfjksd|sdkjbfhokjsd|skjdbfhjkosd... (8 Replies)
Discussion started by: adaleru
8 Replies

7. Shell Programming and Scripting

how to cut off last column from the output

I have a problem with my script. I am using following code awk -F"," '{print $0,",",substr($2,3,3)}' $REG_InputFileName > $TargetSeqPath/Master.tmp while read i do echo $i > $TargetSeqPath/Ref.tmp OutFileName=`awk -F"," '{print $3}' $TargetSeqPath/Ref.tmp` rm -f... (9 Replies)
Discussion started by: manmeet
9 Replies

8. UNIX for Advanced & Expert Users

cut a specific value from a column

Dear Friends, I have an output like this: 7072;0;7072901 7072;1001;7072902 7072;101;7072903 7072;102;7072904 7072;1101;7072905 7072;1301;7072906 7072;1401;7072907 7072;162;7072908 7072;1;7072909 and I need to print the value in the column 3 , row number 1. which is 7072901 only.... (2 Replies)
Discussion started by: sfaqih
2 Replies

9. Shell Programming and Scripting

To cut entire column from a file and apend it to another file as another column

file1.txt : india pakistan bangladesh japan canada africa USA srilanka Nepal file2.txt Delhi Tokyo washington I have to cut the first column of file1.txt and apend it with file2.txt as another column like this Delhi india Tokyo japan washington USA ... (4 Replies)
Discussion started by: sakthifire
4 Replies

10. Shell Programming and Scripting

cut column

I have a file as below, $vi myfile aaa;20071217 bbb;20070404 ccc;20070254 " if I want to cut the column 9-12 of the first line , the output should be 1217 , can advise how to write a script to get the result ? thx p.s. can a script that have only ONE line could do that ? (5 Replies)
Discussion started by: ust
5 Replies
Login or Register to Ask a Question