Solved

How to write this shell script

Posted on 2009-04-03
2
197 Views
Last Modified: 2013-12-26
Hi experts,

I have many files in following format

22  cpv 88  99  cpithisis
33  cpi 90  22  hecpvto
11  cpi ee  pp  timecpi
10  cpv 3e  79  shecpvcomes


The 2nd column is either "cpv" or "cpi". Each line has 5 columns and columns are "\t" seperated. Now I want to use shell script to split those lines of "cpv" to one file and "cpi" to another file.

I use following script but it is not correct. Can anybody help me to correct it?

Thanks so much.

cat *cpv_cpi*.txt | awk '{if ($2=="cpv") print $0}' | gzip > aa.txt

Open in new window

0
Comment
Question by:davidw88
2 Comments
 
LVL 10

Accepted Solution

by:
cyberstalker earned 125 total points
Comment Utility
The command you posted should work to put all the lines with cpv as second argument in a zipped file.


# put all lines with cpv in a gzipped file called aa.txt.gz and cpi into bb.txt.gz

cat *cpv_cpi*.txt | awk '{if ($2=="cpv") print $0}' | gzip > aa.txt.gz

cat *cpv_cpi*.txt | awk '{if ($2=="cpi") print $0}' | gzip > bb.txt.gz
 

# do the same, but now uncompressed

cat *cpv_cpi*.txt | awk '{if ($2=="cpv") print $0}' > aa.txt

cat *cpv_cpi*.txt | awk '{if ($2=="cpi") print $0}' > bb.txt

Open in new window

0
 

Author Comment

by:davidw88
Comment Utility
Great. Thanks cyberstalker!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now