Script to return first x characters from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to return first x characters from file
# 1  
Old 12-16-2010
Script to return first x characters from file

I need a script to read the first 1000 characters of a text file regardless of how many lines it will span. So if my first line has 2000 characters i only want the first 1000 from this line, if my first and second lines have 500 character each then i want all the characters from both lines and ignore the rest of the file.

So the output should be the 1000 characters at the start of the file regardless of the how many lines it spans. Seems like a tough one but i'm sure someone can help.
# 2  
Old 12-16-2010
Is this homework? What did you try so far?
# 3  
Old 12-16-2010
No just something i need for a project.
# 4  
Old 12-16-2010
Code:
 
#/bin/ksh
typeset -i n
typeset -t cnt
cnt=1000
while read line
do
    n=$(echo $line | wc -m)
    if [ $n -le $cnt ]; then
        echo $line
    else
        echo $line | cut -c 1-$cnt
        exit
    fi
    cnt=$cnt-$n
done < inputFile


Last edited by anurag.singh; 12-16-2010 at 07:32 AM..
# 5  
Old 12-16-2010
Not sure if the below command works for 1000 characters, but give a try
Code:
sed 's/\(^.\{1000\}\) *.*/\1/' inputfile > outfile

# 6  
Old 12-16-2010
Code:
 
head -c1000 inputfile > outputfile

This User Gave Thanks to xoops For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

2. Shell Programming and Scripting

Shell Scripting | Return list of unique characters in files

Hi, I am trying to script the below, but I am not very good at it :( Your help would be greatly appreciated. 1. read all files in the directory in strings strings *.* 2. in each file, for each line that contains "ABCD", store characters located at position 521 and 522 of this line... (9 Replies)
Discussion started by: clippertm
9 Replies

3. Shell Programming and Scripting

Need output of script on screen and file with correct return status of the called script.

Hi, I am trying to capture logs of the script in the file as well as on the screen. I have used exec and tee command for this. While using exec command I am getting the correct output in the file but, script output is not getting displayed on the screen as it get executed. Below is my sample... (14 Replies)
Discussion started by: Prathmesh
14 Replies

4. Shell Programming and Scripting

Find a string and then return the next 20 characters in multiple files

Hello all, I have a directory with 2000+ files. I need to look in each file for an invoice number. To identify this, i can search for the string 'BIG' and then retrieve the next 30 characters. I was thinking awk for this, but not sure how to do it. Each file contains one long string and in... (8 Replies)
Discussion started by: jdinero
8 Replies

5. Shell Programming and Scripting

Return all characters to the left of the last delimeter of each line

Hello, Working on a ksh script and a little stumped... how can I return all characters to the left of the last delimeter per line in a file, skipping any lines without that delimeter? ie, sample.txt: Once_upon-a-Midnight_dreary_while I pondered, weak_and weary over many a quaint and... (4 Replies)
Discussion started by: MoreCowbell
4 Replies

6. Shell Programming and Scripting

line carriage return characters

Hi, I would like to insert the line carriage retrun characters on each line. (2 Replies)
Discussion started by: koti_rama
2 Replies

7. Shell Programming and Scripting

Return error if - or certain characters are present in a list of strings

I have a list of strings, for example: set strLst = "file1 file2 file3 file4" I want to log an error if some of the fields happen to begin with -, or have characters like ; : ' , ? ] { = Which means for example setting set ierr = 1 (2 Replies)
Discussion started by: kristinu
2 Replies

8. Shell Programming and Scripting

Help to find string and return following characters from list of files

Hi, I'm fairly new to UNIX, but hopefully some-one can help me with this: I am using the following code to find files with the name "example.xml": find . -name "example.xml" -print that would print me a list like the example here: ./dir1/dir2/example.xml... (5 Replies)
Discussion started by: boijie
5 Replies

9. Shell Programming and Scripting

Return to HTML file from shell script

Hi! I'm writing a simple script which I call on using a simple html button. The script also recives a simple argument. After the script is done I immediately want to return to my html page. But I dont know how to do that! #!/bin/sh echo "Content-type: text/html" echo "" if then echo... (1 Reply)
Discussion started by: crille
1 Replies

10. UNIX for Dummies Questions & Answers

Removing carriage return characters from file

Hello there, I need to remove carriage return characters (\n and \r) from any input file specified. This is what I am doing right now: - dumping the file to octal format using the command 'od -c file_name - removing and \s and \n characters using sed commands What I need to do now is... (3 Replies)
Discussion started by: b1saini
3 Replies
Login or Register to Ask a Question