Delete one Line from Main File and Create another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete one Line from Main File and Create another
# 1  
Old 04-08-2010
Delete one Line from Main File and Create another

Hi

I need to write a script which will search for some text in some CSV files and will delete the record/line with the matching text and will insert that record/line into some other file.

Can you please help?

Here is what I want: -

Contents of MainFile.csv: -

1,2,3,Loan,4,5,6
1,2,3,Loan,4,5,6
1,2,3,Bank,4,5,6
1,2,3,Bank,4,5,6
1,2,3,Cheque,4,5,6
1,2,3,Cheque,4,5,6

I wish to search the current directory and file out which all files contain the text "Cheque" and I wish to delete these records from all the files and thus MainFile.csv will become

Contents of MainFile.csv: -

1,2,3,Loan,4,5,6
1,2,3,Loan,4,5,6
1,2,3,Bank,4,5,6
1,2,3,Bank,4,5,6

and another file should be created TEMP.CSV with following contents: -

1,2,3,Cheque,4,5,6
1,2,3,Cheque,4,5,6

Can you please help me how to do this?
# 2  
Old 04-08-2010
Code:
grep 'Cheque' MainFile.csv > TEMP.CSV ; sed -ie /Cheque/d MainFile.csv

# 3  
Old 04-08-2010
one way:

Code:
grep 'Cheque' MainFile.csv > file_with_cheque
mv file_with_cheque MainFile.csv

grep -v 'Cheque' MainFile.csv > TEMP.CSV


There are many other ways with sed,awk etc.
# 4  
Old 04-08-2010
MySQL

Code:
sed -n '/Cheque/p' main.csv > tmp.csv ; sed -i '/Cheque/d' main.csv

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

2. UNIX for Dummies Questions & Answers

How to create a file even root user also cant delete?

Is there any way to create a file in linux that root user also can't delete? (8 Replies)
Discussion started by: palani13dec
8 Replies

3. AIX

[Solved] Not able to delete/create file in /tmp

This is AIX box and I am not able to create or delete file in /tmp though space is there root@ttcols01/ #touch /tmp/test_file touch: 0652-046 Cannot create /tmp/test_file. root@ttcols01/ #mkdir /tmp/test_dir mkdir: 0653-358 Cannot create /tmp/test_dir. /tmp/test_dir: Invalid file system... (9 Replies)
Discussion started by: solaris_1977
9 Replies

4. Shell Programming and Scripting

create txt file form data file and add some line on it

Hi Guys, I have file A.txt File A Data AK1521 AK2536 AK3164 I want create text file of all data above and write some data on each file. want Output on below folder /home/kka/out AK1521.txt Hi Welocme (3 Replies)
Discussion started by: asavaliya
3 Replies

5. Shell Programming and Scripting

Delete a particular line from a file

I have a file of following form 2886758410 2886758500 17 1999-Mar-18 16:26:26 1 0 52 139 1129 2886758420 2886758500 17 1999-Mar-18 16:26:35 1 0 52 139 1131 2886758420 2886758500 17 1999-Mar-18 16:26:41 0 0 56 56 1132... (4 Replies)
Discussion started by: vaibhavkorde
4 Replies

6. Shell Programming and Scripting

create dir in main &subdirs,perform action

Hi, I have one dir which has N subdirs.For ex: /home/user/Project_Src /home/user/Project_Src/Dir_A /home/user/Project_Src/Dir_A/subdir/sub_dir2 /home/user/Project_Src/Dir_A/subdir/sub_dir3 /home/user/Project_Src/Dir_B /home/user/Project_Src/Dir_B/Build i want to create a folder with... (2 Replies)
Discussion started by: dragon.1431
2 Replies

7. Shell Programming and Scripting

Delete first line from file and more....

Hello, I have to deal with several files that will be named something like this: E00001.TXT, E00002.TXT etc. Each file will have a alpha character on the first position of the first line which I want to place in a variable, then delete the entire line leaving the remainder of text. This new file... (6 Replies)
Discussion started by: dfb500
6 Replies

8. Shell Programming and Scripting

read line by line and create new file

I would like to read line by line out of a file (one word per line) and create with each line a file. The read line should be also pasted into the file with some other text. Something like this: cat readfile.txt | mkfile readline << "bla bli $readline blu" (7 Replies)
Discussion started by: borobudur
7 Replies

9. Shell Programming and Scripting

Can I create a file from the command line

I want to create a file explicitly from the command line without going in to the file directly just create it. Can I do that? Thanks in advance. (5 Replies)
Discussion started by: airon23bball
5 Replies

10. UNIX for Dummies Questions & Answers

How to delete line from a file

Hi , I have a file it content is like that /vol.nas/u08/aip_triage/hany/Tesko/:CC::RPAS /home/biblawh/myscript:CC::RPAS i need to search for a certain pattern inside that file and delete the line if i find this pattern without redirecting the output into another file . so i used the... (0 Replies)
Discussion started by: ramezernest
0 Replies
Login or Register to Ask a Question