Deleting Characters at specific position in a line if the line is certain length


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting Characters at specific position in a line if the line is certain length
# 1  
Old 12-15-2008
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  
Old 12-15-2008
Code:
nawk '
{
   if (length <= 1280) 
     print
   else
     print substr($0, 1, 102) substr($0,106, 1208 -106) substr($0, 1214)
}
' myFile > myNewFile

# 3  
Old 12-16-2008
Thanks vgersh99!

I had to change nawk to awk (it was telling me nawk not found) and that seemed to do the trick
# 4  
Old 12-16-2008
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  
Old 12-16-2008
Quote:
Originally Posted by Cailet
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  
Old 12-16-2008
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 Smilie

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

OS is Windows XP pro
# 7  
Old 12-16-2008
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove line break at specific position

Hi, I need to remove line breaks from a file, but only the ones at specific position. Input file: this is ok this line is divided at posit ion 30. The same as this one, also position 30 the rest of lines are ok with different lengths The longest ones are always s plitted at same... (15 Replies)
Discussion started by: qranumo
15 Replies

2. Shell Programming and Scripting

Need help on find and replacement on specific line and position

I have a file with 100 lines. On 50 th line , from position 5 to rest of the data , I need to change the occurrence of A to B and Occurrence of M to N. Input file : Line1 Line2 Line3 -- -- 12345ABCDEFM --- -- Line 100 Output Line1 Line2 (40 Replies)
Discussion started by: Rajesh_us
40 Replies

3. UNIX for Dummies Questions & Answers

Help with awk, where line length and field position are variable

I have several questions about using awk. I'm hoping someone could lend me a hand. (I'm also hoping that my questions make sense.) I have a file that contains pipe separated data. Each line has similar data but the number of fields and the field position on each line is variable. ... (3 Replies)
Discussion started by: Cheese64
3 Replies

4. Shell Programming and Scripting

Extract substring specif position and length from file line

Hi gurus, I am trying to figure out how to extract substring from file line (all lines in file), as specified position and specified legth. Example input (file lines) dhaskjdsa dsadhkjsa dhsakjdsad hsadkjh dsahjdksahdsad sahkjd sahdkjsahd sajkdh adhjsak I want to extract substring on... (5 Replies)
Discussion started by: ProsteJa
5 Replies

5. Shell Programming and Scripting

Printing characters at specific position in line

Hi, I am trying to get an output like : +----------------------------------+ ----------- + + some variable substitution + some text + Is there a way I can specify in printf (in ksh) the particular position I want to print a character, and also repeat a character from... (1 Reply)
Discussion started by: neil.k
1 Replies

6. Shell Programming and Scripting

Deleting new line characters

Hi, I have a weird requirement. I am having a file with 12fields in it and the end of the line for each record is "\n" (Just \n and no carriage returns) and the field delimiter is "|". Problem is I can have new line characters in any field in the data and these new line characters can even come... (11 Replies)
Discussion started by: ngkumar
11 Replies

7. Shell Programming and Scripting

Removing characters from end of line (length unknown)

Hi I have a file which contains wrong XML, There are some garbage characters at the end of line that I want to get rid of. Example: <request type="product" ><attributes><pair><name>q</name><value><!]></value></pair><pair><name>start</name><value>1</value></pair></attributes></request>�J ... (7 Replies)
Discussion started by: dirtyd0ggy
7 Replies

8. Shell Programming and Scripting

Search in specific position and print the whole line

I have two files abc.dat and sant.dat (Big file 60k rows) for every line's 1,4 of abc.dat need to seach if this is present on 28,4 of sant.dat every line. if its present the output needs to go to bde.dat Example: contents abc.dat aaaa bbbb cccc dddd contents sant.dat this is... (4 Replies)
Discussion started by: ssantoshss
4 Replies

9. UNIX for Dummies Questions & Answers

Need help with deleting certain characters on a line

I have a file that looks like this: It is a huge file and basically I want to delete everything at the > line except for the number after “C”. >c1154... (2 Replies)
Discussion started by: kylle345
2 Replies

10. Shell Programming and Scripting

Deleting First Two Characters On Each Line

How would one go about deleting the first two characters on each line of a file on Unix? I thought about using awk, but cannot seem to find if it can explicitly do this. In this case there might or might not be a field separator. Meaning that the data might look like this. 01999999999... (5 Replies)
Discussion started by: scotbuff
5 Replies
Login or Register to Ask a Question