Delete character on specific position

Hi, im still new in unix.

i want to ask how to delete character on specific position in line, lets say i want to remove 5 character from position 1000, so characters from position 1000-1005 will be deleted.

i found this sed command can delete 4 characters from position 10, but i dont know if bigger position or more characters to be delete is needed.

sed 's/\(..........\)    /\1/'

btw i'm using HP-UX

thanks

Similar questions have already been asked and answered here. Right at the bottom on this thread you'll find these threads.

Check Delete character in determinate position with sed/awk and Using sed to replace specific character and specific position

Let us know if you could adjust it for your requirement or if you need further help :b:

1 Like

If you are using bash, there is no need to call any external tools to perform this. You can use parameter expansion alone as per below:

# Example line variable
line="this is an example line of text"

# Now lets delete 8 characters after the 10th character (deleting the word example)
echo ${line/${line:10:8}}
1 Like

hi thanks for the replies,

i find this will work (im not at hp-ux environment at the moment, so i try on ubuntu server)

sed "s/\(.\{4031\}\)......./\1 $filename > $output
or 
cut -c -4032,4039- $filename > $output

what if file contain more than 2 millions line is still fine if i do above command ?

You're on the right track. The file size or amount of lines should not matter, imho.

The sed command you mentioned will not work. Here's a fix:

sed "s/\(.\{4031\}\)......./\1/" $filename > $output

Following is even better, just in case, because it means read the first 4031 characters explicitly from the beginning of the line:

sed "s/^\(.\{4031\}\)......./\1/" $filename > $output

Note that the cut command you've posted will delete only 6 characters. To act same as the above sed command it should be:

cut -c -4031,4039- $filename > $output

No matter which command you run, I suggest to test it with only few records of the real data (before you process the whole file) to ensure the output is as expected:

head $filename > temp.data
sed ... temp.data
or
cut ... temp.data

Hope this helps.

1 Like

I'm afraid above will not work as you lost inportant chars. Try sed "s/\(.\{4031\}\)......./\1/"

That should work as you apply the command to only one line at a time.

1 Like

Although both sed and cut work on Ubuntu, the standards only require sed to work on text files. Files with lines containing more than 2048 bytes are not text files on HP-UX. The cut utility, however, is required to work on files with arbitrary line lengths; so it should work on any system.

As RudiC said for sed , the number of lines being processed doesn't matter; both cut and sed work on one line at a time; they don't need to read the whole file into memory at once to do this job.

1 Like

thanks for all replies

seem sed function doesnt work with HP-UX, but cut work fine.

sed "s/^\(.\{4031\}\)......./\1/" test.3
sed: Function s/^\(.\{4031\}\)......./\1/ cannot be parsed.