copy columns only IF


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copy columns only IF
# 1  
Old 09-05-2008
copy columns only IF

Hi again,


Can someone please help me on extracting only certain columns from any text (Tab Delimited).


Eg.

Read all the text files under zzz directory (Tab Delimited).

If the cell value in column 8 contains the following strings ( HP OR IBM OR Dell)

Then
Copy Colum 1, Colum 4, Colum 8.



I tried this code but it didn't work.


Find . -name '*PC*.txt' | gawk -F\\\t $1,$4,$8 '$8=="HP" or "IBM" or "Dell" {print}'


thanks in advanceSmilie
# 2  
Old 09-05-2008
You need to repeat the comparison operator, the == doesn't accept a list of alternatives with "or" between them on the right hand side.

Code:
find . -name '*PC*.txt' |
gawk -F '\t' '$8 == "HP" || $8 == "IBM" || $8 == "Dell" { print $1, $4, $8 }'

Also note that the fields to print are passed to the print function, not given as command-line parameters.

You could use a regular expression match like $8 ~ /^(HP|IBM|Dell)$/ if you want to make it more succint.

As per your problem description, you could replace the find with gawk -options 'script' zzz/*PC*.txt
# 3  
Old 09-05-2008
Hi era,



Thanks alot ,

I am using this gawk -options 'script' zzz/*PC*.txt

and it works .

But I have a minor issue, that all the txt files are not located under the ZZZ folder.

There are other folders under zzz which contains txt files.


How can I reference the input directory to all files inside the zzzz and any sub folders?

For some reason this script didn't work.
find . -name '*PC*.txt' | gawk -F '\t' '$8 == "HP" || $8 == "IBM" || $8 == "Dell" { print $1, $4, $8 }'

Once again thanks for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy columns from one file into another and get sum of column values and row count

I have a file abc.csv, from which I need column 24(PurchaseOrder_TotalCost) to get the sum_of_amounts with date and row count into another file say output.csv abc.csv- UTF-8,,,,,,,,,,,,,,,,,,,,,,,,, ... (6 Replies)
Discussion started by: Tahir_M
6 Replies

2. Shell Programming and Scripting

Make copy of text file with columns removed (based on header)

Hello, I have some tab delimited text files with a three header rows. The headers look like, (sorry the tabs look so messy). index group Name input input input input input input input input input input input... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

3. Shell Programming and Scripting

awk to copy previous line matching a particular columns

Hello Help, 2356798 7689867 999 000 123678 20385907 9797 666 17978975 87468976 968978 98798 I am trying to have out put which actually look for the third column value of 9797 and then it insert line there after with first, second column value exactly as the previous line and replace the third... (3 Replies)
Discussion started by: Indra2011
3 Replies

4. Shell Programming and Scripting

How to calculate average of two columns and copy into another file?

Hi, I need help with the awk command. I have a folder with aprox 500 files each one with two columns and I want to print in a new file, the average of column 1 and average of column 2 and the name of each file. Input files are: File-1: 100 99 20 99 50 99 50 99 File-2: 200 85... (3 Replies)
Discussion started by: Lokaps
3 Replies

5. Shell Programming and Scripting

Copy values from columns matching in those in second file.

Hi All, I have two sets of files. Set 1: 100 text files with extension .txt with names like 1.txt, 2.txt, 3.txt until 100.txt Set 2: One big file with extension .dat The text files have some records in columns like this: 0.7316431 82628 0.7248189 82577 0.7248182 81369 0.7222999... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

6. Shell Programming and Scripting

Copy and Paste Columns in a Tab-Limited Text file

I have this text file with a very large number of columns (10,000+) and I want to move the first column to the position of the six column so that the text file looks like this: Before cutting and pasting ID Family Mother Father Trait Phenotype aaa bbb ... (5 Replies)
Discussion started by: evelibertine
5 Replies

7. UNIX for Dummies Questions & Answers

Two files; if cells match then copy over other columns

My current issue is dealing with two space delimited files. The first file has column 1 as the sample ID's, then columns 2 - n as the observations. The second file has column 1 as the sample ID's, column 2 as the mother ID's, column 3 as the father ID's, column 4 as the gender, and column 5... (3 Replies)
Discussion started by: Renyulb28
3 Replies

8. UNIX for Dummies Questions & Answers

How to copy one columns and print to the last column in unix?

I want to copy column no 3 to the end of column example : alter table RECOVER_USR.MPULKIXD rename to alter table RECOVER_USR.CS_ADV_PROMO rename to alter table RECOVER_USR.BCH_HISTORY_TABLE rename to alter table BILLOPS.HISHAM_DATAPLUS_FINAL rename to alter table... (8 Replies)
Discussion started by: arifahel
8 Replies

9. UNIX for Dummies Questions & Answers

What is in-core copy and disk-copy of i-node table?

I have found a question from the exercises of my study mat. The question is "Why are there a in-core copy and a disk-copy of i-node block and super block?" If any one know the proper answer then please send me..... (1 Reply)
Discussion started by: dearanik
1 Replies

10. Shell Programming and Scripting

Copy the first & third columns in to another test file. Its very urgent please

Hello friends! Here is the scenario. I have a flat file named FILE1.TXT with multiple columns and comma as delimiter. Now my task is to create another file called FILE2.TXT which contains the FIRST & THIRD column values from FILE1.TXT separated by comma. Which mean i am creating a new... (5 Replies)
Discussion started by: sai_kris_007
5 Replies
Login or Register to Ask a Question