How to conditionally display and remove first line only?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to conditionally display and remove first line only?
# 8  
Old 12-14-2014
Don,

You are correct, it should have been 2,$b.

The idea was to remove the "From - " line if it was on the first line, and then remove the ">" from the line that contains >From in the header.

And yes, this will modify every file, but correctly formed files will not be changed.
This User Gave Thanks to derekludwig For This Post:
# 9  
Old 12-14-2014
Comparing above mail files to what I have on my systems, the first is an "mbox" type mail, while the second seems to be or already have been converted to "thunderbird" format.
Did you try to make thunderbird read them directly?
# 10  
Old 12-14-2014
Don and Derek,

Thanks so much for the assistance. I really appreciate the fact that your help will allow me to change mail clients again, (which is why I'm doing the conversion to mbox finally), and not lose the last couple of years of emails.

For an initial quick response, the files are email messages stored individually as separate files in a maildir hierarchy. While they are appended with the extension .eml usually in a Microsoft windows environment, in my hierarchy they're stored as files with filenames that are a string of numbers with no extension.

When I edited the first find commands that Derek suggested to search for all files, (removed the EeMmLl), it did indeed locate, and then later count, all affected files. Additionally while I originally thought I should remove the > character from the actual From header line, I found out that the conversion program works whether the > character is there or not. The important part is the removal of the first From line, (mbox postmark), when it exists. I appreciate your suggestion though of removing the > character from the actual From line, and will look to use that as well.

Finally, Rudi, the example headers I posted are pulled from two separate EML files in my hierarchy. They're not in an mbox file, (which is basically a single file with contains a number of email messages).

Thanks all,

Jason

---------- Post updated at 02:08 PM ---------- Previous update was at 08:58 AM ----------

Thanks again Don and Derek. I ended up using Don's shell script, and editing the find filename parameters to;
Code:
#!/bin/bash
bfc=0
find . -type f -name '*' | while read -r file
do      read -r f1 f2 rest < "$file"
        if [ "$f1" = "From" ] && [ "$f2" = "-" ]
        then    # Bad file found...
                bfc=$((bfc + 1))
                printf 'bad file #%d: %s\n\tFrom - %s\n' $bfc "$file" "$rest"
                ed -s "$file" <<-EOF
                        1d
                        w
                        q
                EOF
        fi
done

I ran it from the top level maildir subdirectory, and it worked perfectly, finding every file with an mbox postmark in my maildir hierarchy, displaying the filename, counting the file, and then deleting the mbox postmark from the affected file.

Total 'bad' eml files discovered, and cleaned of mbox postmark; 14,473

Thanks very much to both of you!

Jason
# 11  
Old 12-14-2014
Quote:
Originally Posted by jasn
... ... ...

Thanks again Don and Derek. I ended up using Don's shell script, and editing the find filename parameters to;
Code:
#!/bin/bash
bfc=0
find . -type f -name '*' | while read -r file
do      read -r f1 f2 rest < "$file"
        if [ "$f1" = "From" ] && [ "$f2" = "-" ]
        then    # Bad file found...
                bfc=$((bfc + 1))
                printf 'bad file #%d: %s\n\tFrom - %s\n' $bfc "$file" "$rest"
                ed -s "$file" <<-EOF
                        1d
                        w
                        q
                EOF
        fi
done

I ran it from the top level maildir subdirectory, and it worked perfectly, finding every file with an mbox postmark in my maildir hierarchy, displaying the filename, counting the file, and then deleting the mbox postmark from the affected file.

Total 'bad' eml files discovered, and cleaned of mbox postmark; 14,473

Thanks very much to both of you!

Jason
Jason,
I'm very glad you were able to get my script to work for you. For future reference note that:
Code:
find file... -type f -name '*'

will produce exactly the same output as:
Code:
find file... -type f

since the -name pattern * will match every file.
This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 12-14-2014
Don,

One last thing. Based on Derek's suggestion and his find one liners, is it possible to add to your shell script a function to replace all lines that begin with

Code:
>From

with

Code:
From

I'm not sure that is having an effect on this conversion, but I think it would be useful to clean things up from that perspective.

Thanks,

Jason
# 13  
Old 12-14-2014
In traditional mbox format mail files, mail body lines starting with From are converted by software submitting messages to the mailer daemons to >From to keep it from being misinterpreted as a message boundary. Assuming that the lines you really want to convert are headers that start with >From and contain an email address (i.e. something containing an @ as in user@company.com), the following, in addition to what it did before, will remove the > from the start of a line that starts with >From and contains an @:
Code:
		ed -s "$file" <<-EOF
			1d
			g/^>From .*@/s/^>//
			w
			q
		EOF

If you really want to remove the > from every line containing >From , change the global substitute line above to:
Code:
			g/^>From /s//From /

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ksh: Read line parse characters into variable and remove the line if the date is older than 50 days

I have a test file with the following format, It contains the username_date when the user was locked from the database. $ cat lockedusers.txt TEST1_21062016 TEST2_02122015 TEST3_01032016 TEST4_01042016 I'm writing a ksh script and faced with this difficult scenario for my... (11 Replies)
Discussion started by: humble_learner
11 Replies

2. Shell Programming and Scripting

Conditionally add character at end of line

Hi, I would like have a shell script to check every line in a file to see if it ends with ";". If this is NOT the last character ";" should be added. MyFile.csv : web9331801;01/01/2014 23:39:35;;"93962";353150256; web9331802;01/01/2014 23:44:29;;"479288";353153538; web9331803;01/01/2014... (14 Replies)
Discussion started by: vg77
14 Replies

3. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

4. Shell Programming and Scripting

awk to insert new line conditionally

Dear All, I have a file like: If $4=2001, a duplicated line will be inserted and $4 will be assigned value 2011; so that the new file would be: How to write awk to accomplish this? thank you very much! (5 Replies)
Discussion started by: littlewenwen
5 Replies

5. Shell Programming and Scripting

Add text at the end of line conditionally

Hi All, I have a file as below: cat myfile abcdef NA rwer tyujkl na I wish to add the text ".txt" at the end of all lines except the lines starting with NA or na. I know i can add text at the end of line using following command but I am not sure how to valiate the condition. (14 Replies)
Discussion started by: angshuman
14 Replies

6. Shell Programming and Scripting

Remove file conditionally between two server using sftp

Hi, I am having 2 servers, Need to delete files from server1 if those files exist in server2 other wise no action using sftp .And the process is non-interactive way. I have got confused how to check the condition in sftp because there is non of the shell condition or loop command is executing.... (2 Replies)
Discussion started by: posix
2 Replies

7. Shell Programming and Scripting

Remove newline character conditionally

Hi All, I have 5000 records like this Request_id|Type|Status|Priority|Ticket Submitted Date and Time|Actual Resolved Date and Time|Current Ticket Owner Group|Case final Ticket Owner Group|Customer Severity|Reported Symptom/Request|Component|Hot Topic|Reason for Missed SLA|Current Ticket... (2 Replies)
Discussion started by: j_53933
2 Replies

8. Shell Programming and Scripting

Display mutiple line in single line

Hi All, I had a file called Input.txt, i need to group up in a single line as 1=ttt and the no of lines may vary bewteen the 1=ttt cat Input.txt 1=ttt,2=xxxxxx, 3=4545 44545, 4=66667 7777, 5=77723 1=ttt, 2=xxxxxx, 3=34436 66 3545, 4=66666, 5=ffffff, 6=uuuuuuu 1=ttt, 2=xxxxxx,... (4 Replies)
Discussion started by: manosubsulo
4 Replies

9. Shell Programming and Scripting

sed csv remove conditionally

Hello, I have many csv file, but I would like to delete lines with some values in a column conditionally. My example look like this, ex1e, ex2g, ex39, pasg, ssg, mrlc, pc, kb, coop -112, -53, -177, 64, 62, 71, 1, 487, 20 -101, -61, -53, 0, 32767, 51, 0, ... (6 Replies)
Discussion started by: Jae
6 Replies
Login or Register to Ask a Question