First Three Characters of the first line of a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers First Three Characters of the first line of a file
# 1  
Old 11-12-2009
First Three Characters of the first line of a file

Dear Members,

For example i have a file which contains 10 lines as below:

123testing
gopjp
jg9459\
834789rh
fh456
47rf
497rvg
409748\
08ntr

i need the first three characters of the first line of the file.

if i use

Code:
cut -c 1-3 < sample.txt

its displaying the 1st three characters of all the lines. I just need the first three characters of the first line of a file. How can we do this?


Thanks
# 2  
Old 11-12-2009
You can try the head command.

Code:
head -1 filename | cut blah blah

# 3  
Old 11-12-2009
Code:
head -c3 infile

??
# 4  
Old 11-12-2009
Quote:
Originally Posted by Scrutinizer
Code:
head -c3 infile

Nice solution if you don't care about a carriage return in the output.

Examples:

Code:
head -1 urllist.txt | cut -c 1-3
htt
root@www:/tmp#

Code:
root@www:/tmp# head -c3 urllist.txt
httroot@www:/tmp#

# 5  
Old 11-12-2009
Indeed but it would be usable like this:
Code:
var=$(head -c3 infile)

# 6  
Old 11-12-2009
Code:
read -n3 < infile
echo $REPLY
123


Last edited by daPeach; 11-12-2009 at 06:48 PM..
# 7  
Old 11-12-2009
Quote:
Originally Posted by daPeach
Code:
read -n3 < infile
echo $REPLY
123

Works nicely in bash/ksh (though not in sh/zsh)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying characters on each line in a file

Hello, I would like to copy the first and third char on each line of a file and place them in the 14h and 17th char positions. The file name is listed first and is 6 char's and the dir name is second and also same char size on each line. The file has thousands of lines. Initial... (6 Replies)
Discussion started by: dmm
6 Replies

2. Shell Programming and Scripting

Find new line characters in a file

Hi All, I need to find new line characters in a file and their location in the file and print the results. can someone pleas help. KM (4 Replies)
Discussion started by: Kevin Tivoli
4 Replies

3. Programming

Matching first n characters in a line in a file

hi to all, i am writing a simple file program in C which has to take a string of length n as input and match that input string with first n character of the line in file. if it does not match it has to compare with first n characters of next line. i am finding it difficult to know how exactly to... (2 Replies)
Discussion started by: ntrikoti
2 Replies

4. UNIX Desktop Questions & Answers

Remove new line characters from a file

I tried using below command tr -cd "" < InputFile.xml > output.txt ============= This removes all the tabs/newline/extra spaces from a file it successfully removed all the extra spaces,tabs and new line characters but then the complete file become one record. I want to retain one new line... (1 Reply)
Discussion started by: saini
1 Replies

5. Shell Programming and Scripting

cut certain characters for each line in a file

Hi Everyone, i have a file 1.txt <a><a"" dd>aaaaauweopriuew</f><">!(^)!</aa></ff> <a><a"" dd>bbbbbuweopriuew</f><">!(^*)!</aa></ff> i know i can use perl -p -i -e "s/>aaaaa/aa/g" 1.txt perl -p -i -e "s/>bbbbb/bb/g" 1.txt to acheive only keep the first two characters of the five characters,... (4 Replies)
Discussion started by: jimmy_y
4 Replies

6. Shell Programming and Scripting

New line characters in Ascii file

I am having a file(1234.txt) downloaded from windows server (in Ascii format).However when i ftp this file to Unix server and try to work with it..i am unable to do anything.When i try to open the file using vi editor the file opens in the following format ... @ @ @ @ @ @ @ @... (4 Replies)
Discussion started by: appu2176
4 Replies

7. Shell Programming and Scripting

Delete new line characters from a file

Hi, I have a file with about 25 colums separated with '~', but few of the lines have extra tabs ('^') and new line characters ('$'). Is there a way I can delete those characters if they are anywhere before the 25th column in a line? example: CLUB000650;12345678;0087788667;NOOP MEMBER ... (4 Replies)
Discussion started by: rudoraj
4 Replies

8. Shell Programming and Scripting

how to cut first 3 characters of each line in a file

Hi Friends I have a file like sample1.txt ------------ 10998909.txt 10898990.txt 1898772222.txt 8980000000000.txt I need to take first 3 characters of each line in a file and i need to print it ' like loop 109 108 189 898 (7 Replies)
Discussion started by: kittusri9
7 Replies

9. Shell Programming and Scripting

how to count characters by line of file ?

Hello, Member or professional need help how to count characters by line of file Example of the file is here cdr20080817164322811681txt cdr20080817164322811txt cdr20080817164322811683txt cdr20080817164322811684txt I want to count the characters by line of file . The output that I... (4 Replies)
Discussion started by: ooilinlove
4 Replies

10. Shell Programming and Scripting

Filling in characters to line a file up

Hi there, Ive got a feeling this is quite complex but I have a Comma delimited file which is being transfered up to a mainframe and I have to get every column lining up , I need to add in characters to line it up (but different characters and in different positions based on what field/column it... (1 Reply)
Discussion started by: hcclnoodles
1 Replies
Login or Register to Ask a Question