need to read 3° character from a text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers need to read 3° character from a text file
# 1  
Old 07-21-2005
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 "123456" and the characters are not separated.


Thanks in advance for your kindly help.
# 2  
Old 07-21-2005
echo 12345 | sed 's/^.\{3\}\(.\).*/\1/'
# 3  
Old 07-22-2005
Phew,

vgersh99 can you throw some light on that.
It bounced over my head.
# 4  
Old 07-22-2005
Quote:
Originally Posted by vgersh99
echo 12345 | sed 's/^.\{3\}\(.\).*/\1/'
Code:
^        - from the beginning of the line
.\{3\}  - any 3 characters
\(.\)    - capture one character [the 4-th chars from the beginning]
.*       - any number of characters

\1       - print the first 'capture'

# 5  
Old 07-25-2005
echo 12345 | sed 's/^.\{3\}\(.\).*/\1/'

Then won't this work.

echo 12345 | sed 's/^\(...\)\(.\).*/\1\2/'
# 6  
Old 07-25-2005
Quote:
Originally Posted by vibhor_agarwali
echo 12345 | sed 's/^.\{3\}\(.\).*/\1/'
echo 12345 | sed 's/^.\{3\}\(.\).*/\1/'
4

Quote:
Originally Posted by vibhor_agarwali
Then won't this work.
echo 12345 | sed 's/^\(...\)\(.\).*/\1\2/'
1234

I don't know - depends on the the definition of 'works'
# 7  
Old 07-25-2005
sed and awk wont work if the character you've got isn't on the first line of the file.

If you have GNU text utiliities installed you can do it using the versions tail and head in the distribution.

head -c 4 yourfile.txt | tail -c 1

If you wanted the 4th charcter for example. However this counts carriage returns and spaces as characters. If you want to ignore spaces and/or carraige returns you can do it but its a little more complicated.

It would be easy if you have access to perl:

cat youfile.txt | perl -e 'read(STDIN,$data,4); print substr($data,-1);'

again gives your the 4th character.

Sean
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Read text from file and print each character in separate line

performing this code to read from file and print each character in separate line works well with ASCII encoded text void preprocess_file (FILE *fp) { int cc; for (;;) { cc = getc (fp); if (cc == EOF) break; printf ("%c\n", cc); } } int main(int... (1 Reply)
Discussion started by: khaled79
1 Replies

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question