How to read only 1st three character from a file using sed in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read only 1st three character from a file using sed in unix
# 1  
Old 08-15-2009
How to read only 1st three character from a file using sed in unix

Hi
can any one please help me how to read forst three character from a file using sed or any other way

example

My file contains data like this

asd123#412

I want to just read 1st three characters no matter whatever it is (character, digit, special characters, space, tab..etc)

Thanks in advance
# 2  
Old 08-15-2009
Code:
[house@leonov] FULL='asd123#412' ; PART=${FULL:0:3} ; echo $PART
asd

# 3  
Old 08-15-2009
Code:
sed 's/\(.\{3\}\).*/\1/' file

# 4  
Old 08-15-2009
Why can you not use cut?
Code:
cut -c1-3  filename

Sounds like homework to me.....
# 5  
Old 08-15-2009
Few more:

Code:
awk '{print substr($0,1,3)}' file

perl -lne '/(.{3}).*/ and print $1' file

perl -lne '/(...).*/ and print $1' file

perl -lne 'print substr($_,0,3)' file

perl -lne '@x=unpack("A3"); print $x[0]' file

tyler_durden
# 6  
Old 08-16-2009
Quote:
Originally Posted by danmero
Code:
sed 's/\(.\{3\}\).*/\1/' file

Thank u very much,

Can u pls throw some light over it.
# 7  
Old 08-16-2009
Taking this task literally, any program which knows about records is not suitable. We need to read the raw file.

My solution to getting the first three characters of a file no matter what they are is:

Code:
dd bs=3 count=1 if=input_file of=output_file

We have avoided putting the characters in a shell variable because we don't know what they are.
Program "cut" is unsuitable if one of the characters is a linefeed for example.

Last edited by methyl; 08-16-2009 at 11:24 AM.. Reason: several typos!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read one character form each line of the file?

Hi, Maybe this iscorrect forum for my question... I should read one character at a fixed position from each line of the file. So how ??? should be substituted in the code below: while read line ; do single_char=`???` echo "$single_char" done < $input_file OK...I did get an... (0 Replies)
Discussion started by: arsii
0 Replies

2. Shell Programming and Scripting

How to remove last character in a string read from file

Hello, The last character is a comma , I have tried the following: sed -e 's/\,$//' filename-to-read however - there are still commas at the end of each line...:confused: (5 Replies)
Discussion started by: learning
5 Replies

3. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

4. Shell Programming and Scripting

How to read character by character in a file

Hi, How read character by character from a file . and i need replace '.' with null if it comes as a 5 character i am beginner ...please help me (1 Reply)
Discussion started by: kartheek
1 Replies

5. Shell Programming and Scripting

Unable to read special character from the file

Hello All, We are getting files from sftp server through file transmission protocol & after transmission we are removing all the control M (^M) characters from them.we are expecting various kind of special characters in the files. we are tried removing '^M' characters through 'dos2unix' command... (2 Replies)
Discussion started by: Aquilis
2 Replies

6. Shell Programming and Scripting

Can I read a file character by character?

Hello all respected people, Can i read a file character by character without using sed,awk and perl commands. Thanks in advance. (4 Replies)
Discussion started by: murtaza
4 Replies

7. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

8. Shell Programming and Scripting

Can i read a file character by character

How to read character by character using awk (6 Replies)
Discussion started by: karnan
6 Replies

9. Shell Programming and Scripting

Read First Character of Each Line in File

I need a better way to read the first character of each line in a file and check if it equals the special character ¤. This character tells me where there is a break in the reports. The file has over 500,000 lines. Currently, this is my code - if ] I am using Korn Shell as a scripting... (7 Replies)
Discussion started by: azelinsk
7 Replies

10. UNIX for Dummies Questions & Answers

need to read 3° character from a text file

Hi, I need a script to read the n° character from a text file. eg: if the text file contains the line "123456" ,I nedd a command to display the number 4, as an example. I tried with awk and printf but it seems only works with words separated with spaces, but in this case I have only one word... (15 Replies)
Discussion started by: piltrafa
15 Replies
Login or Register to Ask a Question