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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To truncate one input file content to a fixed length?

Status
Not open for further replies.

micotao

Programmer
Jun 16, 2004
33
CN
Hi, All:
Here I'd like to read in a plain text file,, but I need to keep its original format( its orginal format is line after line, we will keep this), and to keep the read-in characters to be no more than 160 characters, and write these read-in content(keep its original format,even if we can't get the last complete line because of the 160 characters' limitation) to another file.
Does anyone can help with this issue? As I am a newbie, not very sure about if shell script has such a ability? Or it can't do such a truncating work??
Any suggestions are welcomed!
 
A starting point:
awk '{printf "%-160.160s\n",$0}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi, PHV:
Thanks. But this is execution result,which is strange, it is not what I want.
We are sure that our original text is much more than 160 characters, and I hope the characters' number in the output file will be no more than 160 characters. I use ksh to execute it, and get the following output file.

Our original file is like this:

Line 1
Line 2
....
Line 17
And the output file is something like this, it keeps all the lines from the original files and insert %-160.160s for each line.
===========
Output file
===========
%-160.160s
Line 1
%-160.160s
Line 2
........
%-160.160s
Line 17
%-160.160s

I copied your command to do it, but got this. Please help to investigate. Thanks a lot!
 
Please, post the EXACT code you tried.
I suspect print instead of printf.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi, PHV:
Thanks for your reply. Yes, My mistake, I should use printf. But now, I even got the wrong result, it insert a blank line between the original lines,which is not what I want. I want to read in the content of the original file, keep its format unchanged, and just read in 160 characters totally,and other content will be truncated.This is related with the whole file, not lines.
Original file:
Line1
Line2
Line3
...
Line 17

If I use the command you provided, I got this:
Line1

Line2

Line3

...

Line 17

Could you please help to make sure of this?Thanks.
 
You want only the 160 first characters of the whole file ?
dd ibs=1 count=160 <input >output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Try...
[tt]
head -160c file1 > file2[/tt]
 
Hi, PHV:
Thanks for your help. "dd" can perform the work. And I think there is another same kind question here. If we want to truncate a string to a fixed length. Say, there is a 500 characters long string1, we want to keep the first 160 characters and assigned it to another string2 if we don't use file to support us, can we do the truncating work directly to the string1??
Can you provide some suggestions?


Hi, Ygor:
Does head support characters specifying? This is the result I tried, it doen't work:

head -160c input > output
head: Badly formed number
 
You may try this:
string2=$(expr substr "$string1" 1 160)"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
did you tried the cut command:
example:
cut -c1-160 file1 >file2

 
Hi, maltais:
Thanks for your comments. But the usage you described is aimed to pass the first 160 characters of each line, instead of the first 160 characters of the file1, so it is not what we want. Do you have any further comments?
Thanks.
 
[tt]
awk '{all=all $0 RS
if (length(all)>159)exit}
END{printf "%s",substr(all,1,160)}' infile >outfile
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top