The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Break line after last "/" if length > X characters SportBilly Shell Programming and Scripting 9 10-25-2008 03:15 PM
sed remove last 10 characters of a line start from 3rd line minifish Shell Programming and Scripting 7 03-26-2008 04:42 PM
check position of end of line for some specific lines senthil_is Shell Programming and Scripting 1 11-09-2007 01:19 AM
Deleting First Two Characters On Each Line scotbuff Shell Programming and Scripting 5 12-16-2006 01:03 AM
Deleting the blank line in a file and counting the characters.... rkumar28 Shell Programming and Scripting 4 04-17-2005 12:13 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-15-2008
Cailet Cailet is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 19
Question Deleting Characters at specific position in a line if the line is certain length

I've got a file that would have lines similar to:
12345678 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00
23456781 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00
34567812 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00
45678123 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 xx.00 xx.00

I'm looking to basically do:

if [ current line > 1208 characters ]
delete characters at position 102-106
delete characters at position 1208-1214
copy modified line to new file
else
copy line as is to new file

so the new file would be:
12345678 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00
23456781 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00
34567812 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00
45678123 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00

all lines will start with an 8 digit key
we don't know what the characters we are deleting will be (so no pattern searches)

Right now I've been looking at doing a while read inputline loop and checking the character length of each line with the wc -m command. If -gt 1208 perform corrections and copy to new file.

I wasn't sure if there was an easier way to do this -- and I haven't actually been able to figure out the best way to make the corrections to each individual line.

Any ideas?
Thanks!
  #2 (permalink)  
Old 12-15-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
nawk '
{
   if (length <= 1280) 
     print
   else
     print substr($0, 1, 102) substr($0,106, 1208 -106) substr($0, 1214)
}
' myFile > myNewFile
  #3 (permalink)  
Old 12-16-2008
Cailet Cailet is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 19
Thanks vgersh99!

I had to change nawk to awk (it was telling me nawk not found) and that seemed to do the trick
  #4 (permalink)  
Old 12-16-2008
Cailet Cailet is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 19
Ok, just kidding. I don't know what happened the first time, but now it is no longer working. Is this maybe a problem with trying to switch from nawk to awk?
  #5 (permalink)  
Old 12-16-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by Cailet View Post
Ok, just kidding. I don't know what happened the first time, but now it is no longer working. Is this maybe a problem with trying to switch from nawk to awk?
maybe...
pls define 'no longer working'. what's happenin'?
do you have 'gawk' available (maybe under /usr/local/bin)?
What OS?
  #6 (permalink)  
Old 12-16-2008
Cailet Cailet is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 19
I thought it had worked, but I must have had my line numbers mixed up when I checked it.

Right now I have this:

Code:
    awk '{if (length($0) <= 1280) 
            print
          else
            print substr($0, 1, 602) substr($0,610, 1208 -610) substr($0, 1214)
          }' $oldFile > $newFile
It is printing to the new file fine until it gets to the first line which needs to be changed. Then it stops (no error messages).

I did a which gawk command and it says no gawk found

I'm doing some more research myself to see if I can figure it out

OS is Windows XP pro
  #7 (permalink)  
Old 12-16-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Windows XP, eh?

try putting the 'body' of the code code in a separate file - cailet.awk:

cailet.awk:
Code:
{
   if (length($0) <= 1280) 
            print
   else
            print substr($0, 1, 602) substr($0,610, 1208 -610) substr($0, 1214)
}
and execute it like so:
Code:
awk -f cailet.awk $oldFile > $newFile
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0