Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to drop n chars from a record...

Status
Not open for further replies.

bondtrails

Technical User
Nov 19, 2002
31
US
Hi all,
what's the best way to drop n characters from each record in a given file? The n characters are consecutive, but they could be at the beginning, middle, or end of the record.

For example, given file A.txt which has a record length of 1000, how can I get make file B.txt which has a record length of 800 and the first, middle, or last 200 characters of file A.txt dropped?

Thanks for the help everyone!!

--Bondster!!
 

To drop 4 chars from the beginning/end of every record - a "record" is a LINE.
You can extrapolate the rest from this example.


nawk -f drop.awk drop.txt

#------------------- drop.awk
BEGIN {
num2drop=4
}

{
# beginning
#$0=substr($0, 1, num2drop);
#end
$0=substr($0, length($0)-num2drop);
print
}

#------------------ drop.txt
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18
vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi,
why not simply use cut -c

cut -c 1-x,y-1000 file >! file2

where x and y are the columns around what you want drop.

if you want to drop multiple ranges you can simple add other column numbers

cut -c 1-5,10-20,30-40,... file >! file2

----
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top