Deleting a line from a flatfile using Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting a line from a flatfile using Shell Script
# 8  
Old 06-23-2009
and what have you tried?

---------- Post updated at 07:37 PM ---------- Previous update was at 07:23 PM ----------

Code:
grep '^[A-Z]' inputfile > newfile
awk '/^[A-Z]/ {print}' inputfile > newfile

# 9  
Old 06-23-2009
Code:
perl -pi -e 's/^.*\n$// if (unpack "A5") eq ""' filename

tyler_durden
# 10  
Old 06-23-2009
Quote:
Originally Posted by dinesh1985
input file
First Name - position from 1-5
Last Name- position 6-10
Age-Position 11-12
Say above is the format of input file.
Example:
DINESKUMAR21
SACHISHAH 23
23
GURUNTARIM22

In the above example,in the 3 rd record Name is missing. Now while reading the file line by line,I want to delete the 3 rd line during validation.

---------- Post updated at 08:47 AM ---------- Previous update was at 08:46 AM ----------

for the third line blankspaces will be present before 23
Hi,
You can use the below command to delete the invalid line.

cat data | awk '{ if(substr($1,1,5)!="" && substr($1,6,10)!="" && substr($1,11,12)!="") print $1}' > new_filtered_file_name.

Where the data is the input file.

Thanks,
chidhu
# 11  
Old 06-23-2009
Quote:
Originally Posted by pa.chidhambaram
...
You can use the below command to delete the invalid line.

cat data | awk '{ if(substr($1,1,5)!="" && substr($1,6,10)!="" && substr($1,11,12)!="") print $1}' > new_filtered_file_name.

Where the data is the input file.

...
Really ?

Code:
$
$ cat data
DINESKUMAR21
SACHISHAH 23
          23
GURUNTARIM22
$
$ cat data | awk '{ if(substr($1,1,5)!="" && substr($1,6,10)!="" && substr($1,11,12)!="") print $1}'
DINESKUMAR21
GURUNTARIM22
$
$

tyler_durden
# 12  
Old 06-26-2009
Thanks for your replies ..i ll try it and update

---------- Post updated 06-26-09 at 03:20 AM ---------- Previous update was 06-25-09 at 03:54 AM ----------

I donot want to redirect to any new file. I want to delete the line from the file itself.
I have tried sed -i '3d' to delete the 3rd line. But my shell is not recognizing sed -i.

I have tried sed '3d' <filename> ..it is displaying the file without the 3rd line. But the 3rd line is not permanently deleted. Can anyone please help
# 13  
Old 06-26-2009
Quote:
Originally Posted by dinesh1985
Thanks for your replies ..i ll try it and update

---------- Post updated 06-26-09 at 03:20 AM ---------- Previous update was 06-25-09 at 03:54 AM ----------

I donot want to redirect to any new file. I want to delete the line from the file itself.
I have tried sed -i '3d' to delete the 3rd line. But my shell is not recognizing sed -i.

I have tried sed '3d' <filename> ..it is displaying the file without the 3rd line. But the 3rd line is not permanently deleted. Can anyone please help
With a little trick you can redirect the output of sed into the same file

Code:
sed '3d' file | tee file

# 14  
Old 06-26-2009
Thank you..this is working..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command within script wrongly deleting the last line

Hi, I have a shell script which has a for loop that scans list of files and do find and replace few variables using sed command. While doing this, it deletes the last line of all input file which is something wrong. how to fix this. please suggest. When i add an empty line in all my input file,... (5 Replies)
Discussion started by: rbalaj16
5 Replies

2. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

3. Shell Programming and Scripting

Script for identifying and deleting dupes in a line

I am compiling a synonym dictionary which has the following structure Headword=Synonym1,Synonym2 and so on, with each synonym separated by a comma. As is usual in such cases manual preparation of synonyms results in repeating the synonym which results in dupes as in the example below:... (3 Replies)
Discussion started by: gimley
3 Replies

4. Shell Programming and Scripting

Problem to get yesterday's date flatfile with shell script

hi, i was required to write a shell script to get yesterday's date flatfile. but i only know how to get today's date flatfile. Please observed my below scripting: Please help! Thanks ================================================= #!/bin/sh HOST='192.168.1.200' USER='ftp1'... (19 Replies)
Discussion started by: lifeseries
19 Replies

5. Shell Programming and Scripting

Finding a flatfile & deleting first line

I have a small script where I want to see if a file exists & then delete the first line from it. I have code to help me find if the file exists, but I am unsure as to how to then take in the answer and remove the first line from the flatfile: This is what I have so far just to output if the... (3 Replies)
Discussion started by: fatalxkiss
3 Replies

6. Shell Programming and Scripting

Shell Script for deleting the first line in a file

Hi, Could any one please post the shell script for deleting the first line in a file? (3 Replies)
Discussion started by: badrimohanty
3 Replies

7. Shell Programming and Scripting

shell script reqd - deleting files

I have written a script that deletes files: Requirement: i need to delete the files and to know how many files are deleted i.e the count of files and even i need to display when the started time of deletion and the ending time of deletion. I need to display these two times. script: ... (2 Replies)
Discussion started by: venkatesht
2 Replies

8. Shell Programming and Scripting

Deleting column from a flatfile with delimiter

I have a set of flatfiles which have columns delimited by #. How can a particular column be deleted in all the flatfiles. All flatfiles have same number of columns. (5 Replies)
Discussion started by: rsprabha
5 Replies

9. Shell Programming and Scripting

Urgent help required in deleting a line without opening a file usinga shell script

Hi, I need a help in deleting a line matching a particular pattern in a file using shell script without opening the file. The file is a .c/.cpp file. Is it possible? Thanks (6 Replies)
Discussion started by: naan
6 Replies

10. Shell Programming and Scripting

shell script: deleting files from a directory

i have the following problem: use shell script: Two directories have to be searched for files havin the same name. then delete these files from one of these directories. the directory names have to be accepted as command line arguments. what i have done till now is: 1. run a loop... (1 Reply)
Discussion started by: onlyc
1 Replies
Login or Register to Ask a Question