Link to home
Start Free TrialLog in
Avatar of PSGITech
PSGITech

asked on

scripting help

I have a folder with about 20,000 files in it, I need to find a way to delete the first four lines out of each file and append an @example.com to the end of each to and from address.  I'm trying to find a way to script this and I have no programming background.  Is there an easy way to do this, or can someone point me in the right direction?
Avatar of farzanj
farzanj
Flag of Canada image

Make a backup of your files.

To delete four lines of  files you can do something like

cd /to/the/folder
for filename in *
do
perl -i -ne 'print if $. >4' $filename
done

Open in new window


For appending @example.com, you need to provide a sample
Where the address is located in the file?
Avatar of PSGITech
PSGITech

ASKER

I will try this, this morning I have never used perl before.
Here is the contents of the files, I'm pasting in what it looks like now and what I need it to look live afterwards

THE FILE AS IT APPEARS NOW

X-BAAuthOn: <null>
x-BA-Received: from johnsmith@logprod.contoso.com (EHLO logprod.contoso.com 10.10.10.10 [10.10.10.10] )
      by 10.10.10.15 with ESMTP id LMVE5CWFPKZLEXZJL3CVARULDM51LHXNB
      for <>; Fri, 6 Jan 2012 12:00:18 -0500  (EST)
Received: from mail pickup service by logprod.contoso.com with Microsoft SMTPSVC;
       Fri, 6 Jan 2012 12:00:19 -0500
Message-ID: <16245629-FAD6-45BB-B07D-D8BC45DD299520726083742758233>
Date: Fri, 06 Jan 2012 13:43:06 UTC
From: johnsmith
To: billjones; johnsmith


HERE IS WHAT I NEED IT TO LOOK LIKE AFTERWARDS, I NEED THE FIRST 6 LINES REMOVED AND REPLACED WITH X-Receiver: bs@bm3.contoso, I THEN NEED TO APPEND @EXAMPLE.COM AT THE END OF THE ALIAS IN THE FROM AND TO FIELDS

X-Receiver: bs@bm3.contoso
Message-ID: <16245629-FAD6-45BB-B07D-D8BC45DD299520726083742758233>
Date: Fri, 06 Jan 2012 13:43:06 UTC
From: johnsmith@example.com
To: billjones@example.com; johnsmith@example.com

Any help would be greatly appreciated, to have to do this manually would take a year.
powershell:
$rootfolder = 'c:\temp\test'
gci $rootfolder | %{
	$lines = gc $_.fullname | select -Skip 6
	$lines = ,"X-Receiver: bs@bm3.contoso" + $lines
	$lastline = $lines | select -Last 1
	$lastline = "{0}{1}" -f $lastline.Replace(";", "@example.com;"), "@example.com"
	$lines[$lines.Count-1] = $lastline
	$lines | Set-Content $_.fullname
}

Open in new window

thank you going to try this right now
I ran the script and it seems to have pulled off a little too much, here is the result

X-Receiver: bs@bm3.contoso
X-OriginalArrivalTime: 06 Jan 2012 17:00:19.0212 (UTC) FILETIME=[AB8AACC0:01CCCC94]

I do see it did append @example.com at the end of the file but I didn't paste the whole file in the example for confidential purposes, it looks like in all the files @example.com needs to be appended in lines 9 and 10
can u post the file that wasn't processed as it should?
i'll test it again.
Try this one.

It is a Perl one liner.  The advantage is that you can test it on any file and if you have happy with the output, you can simply use option -i to change the file

perl -ne 'BEGIN{print "X-Receiver: bs@bm3.contoso\n";}if($.>6){if(/From:/){s/$/\@example.com/;print;}elsif(/^To:/){s/To: //;s/(\w+)/$1\@example.com/g;s/^/To: /; print;} else{ print;}}' filename

Open in new window


Once you are happy, you can do:
perl -i -ne 'BEGIN{print "X-Receiver: bs@bm3.contoso\n";}if($.>6){if(/From:/){s/$/\@example.com/;print;}elsif(/^To:/){s/To: //;s/(\w+)/$1\@example.com/g;s/^/To: /; print;} else{ print;}}' tt2

Open in new window

Here is a before and after with what the full file would look like with some dummy data, thanks for you help

WHAT IT LOOKS LIKE NOW

X-BAAuthOn: <null>
x-BA-Received: from johnsmith@logprod.contoso.com (EHLO logprod.contoso.com 10.10.10.10 [10.10.10.10] )
      by 10.10.10.15 with ESMTP id LMVE5CWFPKZLEXZJL3CVARULDM51LHXNB
      for <>; Fri, 6 Jan 2012 12:00:18 -0500  (EST)
Received: from mail pickup service by logprod.contoso.com with Microsoft SMTPSVC;
       Fri, 6 Jan 2012 12:00:19 -0500
Message-ID: <16245629-FAD6-45BB-B07D-D8BC45DD299520726083742758233>
Date: Fri, 06 Jan 2012 13:43:06 UTC
From: johnsmith
To: billjones; johnsmith
Subject: Giggins, Dave: buyer 25k now
X-OriginalArrivalTime: 06 Jan 2012 17:00:17.0962 (UTC) FILETIME=[AACBF0A0:01CCCC94]

Friday, January 06, 2012 11:11:35 AM EST
        Giggins, Dave started conversation.
Friday, January 06, 2012 11:11:35 AM EST
        philspencer has entered the conversation.
Friday, January 06, 2012 11:11:35 AM EST
        Giggins, Dave:     buyer 25k now
Friday, January 06, 2012 11:14:59 AM EST



WHAT I'M LOOKING TO ACHIEVE


X-Receiver: bs@bm3.contoso
Message-ID: <16245629-FAD6-45BB-B07D-D8BC45DD299520726083742758233>
Date: Fri, 06 Jan 2012 13:43:06 UTC
From: johnsmith@example.com
To: billjones@example.com; johnsmith@example.com
Subject: Giggins, Dave: buyer 25k now

Friday, January 06, 2012 11:11:35 AM EST
        Giggins, Dave started conversation.
Friday, January 06, 2012 11:11:35 AM EST
        philspencer has entered the conversation.
Friday, January 06, 2012 11:11:35 AM EST
        Giggins, Dave:     buyer 25k now
Friday, January 06, 2012 11:14:59 AM EST
that should work:
$rootfolder = 'c:\temp\test'
gci $rootfolder | %{
	$lines = gc $_.fullname | select -Skip 6
	$lines = ,"X-Receiver: bs@bm3.contoso" + $lines
	$toline = $lines | where {$_ -match "to:"}
	$tolineindex = [array]::IndexOf($lines, $toline)
	$toline = "{0}{1}" -f $toline.Replace(";", "@example.com;"), "@example.com"
	$fromline = $lines | where {$_ -match "from:"}
	$fromlineindex = [array]::IndexOf($lines, $fromline)
	$fromline = "{0}{1}" -f $fromline.Replace(";", "@example.com;"), "@example.com"
	$lines[$tolineindex] = $toline
	$lines[$fromlineindex] = $fromline
}

Open in new window

I had tested it on my system and gave it only when it worked correctly
Weird I changed the first line to match the folder path where I have my test file, and I pasted it into my powershell session and it didn't error out or anything but the file is unchanged, could i be doing something wrong here?
sorry i forgot to update the file after i changed it. here:
$rootfolder = 'c:\temp\test'
gci $rootfolder | %{
	$lines = gc $_.fullname | select -Skip 6
	$lines = ,"X-Receiver: bs@bm3.contoso" + $lines
	$toline = $lines | where {$_ -match "to:"}
	$tolineindex = [array]::IndexOf($lines, $toline)
	$toline = "{0}{1}" -f $toline.Replace(";", "@example.com;"), "@example.com"
	$fromline = $lines | where {$_ -match "from:"}
	$fromlineindex = [array]::IndexOf($lines, $fromline)
	$fromline = "{0}{1}" -f $fromline.Replace(";", "@example.com;"), "@example.com"
	$lines[$tolineindex] = $toline
	$lines[$fromlineindex] = $fromline
$lines | set-content $_.fullname

}

Open in new window

Great!!! thank you for you help, I now need to fun this against a folder that now has about 200,000 messages, this should make my life much easier, all the files have a .txt extension is there also a way to save them as .doc after the update is completed also?
yes, and also i can change the script to run parallel jobs so it would take much less time to process the 200k files.

in the meantime here's the update script which saves as doc:
$rootfolder = 'c:\temp\test'
gci $rootfolder | %{
	$lines = gc $_.fullname | select -Skip 6
	$lines = ,"X-Receiver: bs@bm3.contoso" + $lines
	$toline = $lines | where {$_ -match "to:"}
	$tolineindex = [array]::IndexOf($lines, $toline)
	$toline = "{0}{1}" -f $toline.Replace(";", "@example.com;"), "@example.com"
	$fromline = $lines | where {$_ -match "from:"}
	$fromlineindex = [array]::IndexOf($lines, $fromline)
	$fromline = "{0}{1}" -f $fromline.Replace(";", "@example.com;"), "@example.com"
	$lines[$tolineindex] = $toline
	$lines[$fromlineindex] = $fromline
	$lines | Set-Content $_.fullname.Replace('.txt','.doc')
}

Open in new window

Thank you for all your help this is a hugh help after I have all the file updated and renamed I have to move them to a different folder so they will be picked up by my smtp server, i figured i'd do this with an xcopy job, unless it would be easy to add this to the script also?  When I run this on a folder with alot of files is there a way to monitor its progress, will it run through the files as its doing it or will it just show a blinking cursor?
here the script with some nice UI progress bar and the copy stuff, change $target_dir to whatever u need.

$rootfolder = 'c:\temp'
$target_dir = '\\ws-meirr\shared\files'
$files = gci $rootfolder
$files | %{
	$i++
	Write-Progress -activity "Processing files" -status "Percent complete: " -PercentComplete (($i / $files.length)  * 100)
	$lines = gc $_.fullname | select -Skip 6
	$lines = ,"X-Receiver: bs@bm3.contoso" + $lines
	$toline = $lines | where {$_ -match "to:"}
	$tolineindex = [array]::IndexOf($lines, $toline)
	$toline = "{0}{1}" -f $toline.Replace(";", "@example.com;"), "@example.com"
	$fromline = $lines | where {$_ -match "from:"}
	$fromlineindex = [array]::IndexOf($lines, $fromline)
	$fromline = "{0}{1}" -f $fromline.Replace(";", "@example.com;"), "@example.com"
	$lines[$tolineindex] = $toline
	$lines[$fromlineindex] = $fromline
$target_file = $_.fullname.Replace('.txt','.doc')
	$lines | sc $target_file
copy-item $target_file $target_dir

}

Open in new window

I can't thank you enough for the help here, you've saved me a boat load of time here, when running this it creating a new copy of the file and saving it as a .doc and copying it to the other folder, if i change the command in line 19 to move-item will that work.  This way I don't have a .txt and .doc in the source directory.  I did try that and it seems to work but errors pertaining to percentcomplete Write-Progress : Cannot validate argument on parameter 'PercentComplete'. The 120 argument is greater than the maximum
allowed range of 100. Supply an argument that is less than 100 and then try the command again.
At line:3 char:94
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As it looks now, this is working great, I will test it in a full batch after hours, thanks for all your help on this issue.