How to copy particular record with cp command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to copy particular record with cp command
# 1  
Old 10-05-2009
How to copy particular record with cp command

Hi ,
I have a table of 5 records. I am using FOR condition in this table. Using FOR , I want to copy those particular record into someother file which satisfies the condition. How to use 'cp' command in this situation in UNIX

For Ex

Code:
No Prod Price Bar Code
 
1 Colgate 23 34564 col
2 ParkAvenue 118 45456 par
3 Cadbury 15 12357 cad
4 Lipton 23 56234 lip
5 Iodex 09 32334 Iod

Now , using FOR command , I want to copy those records whose price >=20 .

Code:
for (( i =0 , i>=20, i++)
do 
  cp ?????????? (Help Here!!!! )
done


Last edited by Franklin52; 10-05-2009 at 05:55 AM.. Reason: Please use code tags!
# 2  
Old 10-05-2009
you can use awk..
Code:
awk '$3>=20{print}' inputfile > outputfile

# 3  
Old 10-05-2009
Some observations.,

1. Put the code in CODE tags for better visibility

2. cp is not intended for copying particular records, refer, man cp -> http://linux.die.net/man/1/cp
Code:
$ whatis cp
cp (1)               - copy files and directories

# 4  
Old 10-05-2009
awk '$3>=20{print}' inputfile > outputfile

This is not giving the required output. Infact, this is adding "20" figure in the table as a sepeare column.

I want only.

Code:
                   1 Colgate        23     34564    col
                   2 ParkAvenue 118     45456    par
                   4 Lipton          23     56234    lip

rows in my final answer , since price of these product is greater than (>=20)

Last edited by Franklin52; 10-05-2009 at 05:56 AM.. Reason: Please use code tags!
# 5  
Old 10-05-2009
Also you can't use the "for" construct like that. That is for iterations.

Code:
while read no prod price bar code; do
  if [ $price -ge 20 ]; then
    echo $no $prod $price $bar $code
  fi
done < inputtable > someotherfile

# 6  
Old 10-05-2009
Hi.

Quote:
awk '$3>=20{print}' inputfile > outputfile

This is not giving the required output. Infact, this is adding "20" figure in the table as a sepeare column.
The awk solution given works fine. When you say something doesn't work, it helps if you show what you did and the result.

Code:
awk '$3>=20' file1
1 Colgate 23 34564 col
2 ParkAvenue 118 45456 par
4 Lipton 23 56234 lip

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Record topas command output

How to record output of below command in a text file. topas -P or is there any other command which will do the same thing in AIX. I would like to get a report something similar to below commands. top -b prstat -c (5 Replies)
Discussion started by: NarayanaPrakash
5 Replies

2. UNIX for Dummies Questions & Answers

how to copy files and record original file location?

:EDIT: I think my post name should have been labeled: how to copy files and record original file location. not "retain". Hello, this is my first post! I searched the forums a lot before posting, but was unable to answer my question. Here's my problem: There are several hundred text files... (4 Replies)
Discussion started by: willie8605
4 Replies

3. Shell Programming and Scripting

Record all users' command

Hi experts, .history can record one user's own command history, but my request is, I want to record all users who run commands on that box, and export the commands to a file somewhere, which record the userid, time and full command. Is it possible? I just need for audit, and trace back, if... (3 Replies)
Discussion started by: newoz
3 Replies

4. Shell Programming and Scripting

Shell script for searching a record,copy to a file and then delete it

Hi, I have a requirement in hand: I have a file with millions of records say file 1.I have another file, say file 2 which has 2000 records in it. The requirement is to read file2 , and remove the read record from file 1 and move i to a seperate file, file 3. For eg: Read file 2, get the... (5 Replies)
Discussion started by: kumara2010
5 Replies

5. Shell Programming and Scripting

Unix command to extract a record from a table

Suppose there is a table like the following...I just wanted to know if there is any command using which we can get the record/name of the person who joined before 2005.. Sl Name des y.o.joining 1 Ram Engineer 2001 2 Hari Doctor 2004 3 David Plumber 2005 4 Rahim painter 2007 5 gurmeet... (1 Reply)
Discussion started by: satyajit007
1 Replies

6. UNIX for Advanced & Expert Users

command to insert a record at a particular loaction

Hi, Is there any command to insert a line in between two lines? My input data is as below: 1|ETG|63121387883|Alternate|Y 3|79.58|||GBP|| 4|001137001 4|0011372 5|1021701 5|1021901 1|ETG|63121387884|Alternate|Y 3|79.58|||GBP|| 4|001137001 5|1021702 5|1021802... (1 Reply)
Discussion started by: laxmi131
1 Replies

7. UNIX for Dummies Questions & Answers

Need a command to get part of a record from file

I have a file which contains a record like follows /dir1/dir2/dir3/file.dat I need command so that output can be only file.dat (4 Replies)
Discussion started by: sreenusola
4 Replies

8. UNIX for Advanced & Expert Users

Command nee to cut the record

I have a file which contains a record like follows /dir1/dir2/dir3/file.dat I need command to so that output can be only file.dat (6 Replies)
Discussion started by: sreenusola
6 Replies

9. UNIX for Dummies Questions & Answers

command to remove last record on file

Hi, First time on the forum. I have converted some files using the Unix to DOS command but need to strip off the last record that is generated from this conversion that contains just a ^Z. Is there any command that would accomplish this without having to do stream editing? (4 Replies)
Discussion started by: mheinen
4 Replies

10. Shell Programming and Scripting

fl command - set record length

Hi, I have a KSH shell script running on hpux that uses the fl command - ex: cat /foo | fl 50 > bar This command will take each record in the file foo, make it 50 characters long and then write it to the file bar. my problem is that I am porting my scripts over to solaris which doesn't... (2 Replies)
Discussion started by: Tom Siegel
2 Replies
Login or Register to Ask a Question