Reading a single character from each line of the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading a single character from each line of the file
# 1  
Old 12-11-2010
Reading a single character from each line of the file

Hi,

I should read one character at a fixed position from each line of the file. So how ??? should be substituted in the code below:
Code:
while read line ; do
        single_char=`???`
        echo "$single_char"
done < $input_file


Last edited by Scott; 12-11-2010 at 12:40 PM.. Reason: Please use code tags
# 2  
Old 12-11-2010
use cut option.

for example your input file is

Code:
fdgfdgfdg
gegthhy
mjtumjuymy
uymyfyjhr
trjhryh

and u want to cut characters from 3rd position to 6th position than execute below command

Code:
cut -c 3-6 inputfile

which will generate following output

Code:
gfd
gth
tum
myf
jhr

R0H0N
# 3  
Old 12-11-2010
Thank You

Now I'm wondering why this does not work:
Code:
while read line ; do
        k=`echo $line|cut -c 26`
        if [ $k = "X" ] ; then
                echo "Character at the row was X"
        fi
done < $input_file


Last edited by Scott; 12-11-2010 at 12:41 PM.. Reason: Code tags
# 4  
Old 12-11-2010
Quote:
while read line ; do
k=`echo $line|cut -c 26`
if [ $k = "X" ] ; then
echo "Character at the row was X"
fi
done < $input_file

Try quotes round string variables.
Code:
while read line ; do
        k=`echo "${line}"|cut -c 26`
        if [ "${k}" = "X" ] ; then
                echo "Character at the row was X"
        fi
done < $input_file

# 5  
Old 12-12-2010
Thank You again

With quotes everything works well..Thanks for both of You
# 6  
Old 12-12-2010
bash/ksh93:
Code:
while read line; do
  echo "${line:25:1}"
done

# 7  
Old 12-12-2010
If your input file is large you might benefit from using awk. It will be faster. Considerably faster if you have mawk on your box.

Tested on a infile with 13000 lines:
Code:
$ time ./read_line_do.sh infile > /dev/null
real	0m18.873s
user	0m18.285s
sys	0m0.580s

$ time gawk '{print substr($0,25,1)}' infile > /dev/null
real	0m3.884s
user	0m3.860s
sys	0m0.024s

$ time original-awk '{print substr($0,25,1)}' f > /dev/null
real	0m1.244s
user	0m1.224s
sys	0m0.020s

$ time mawk '{print substr($0,25,1)}' infile > /dev/null
real	0m0.111s
user	0m0.084s
sys	0m0.028s

By the way, the original awk seems to be faster than gawk this time! But it can't beat the lightning fast mawk.
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 understand special character for line reading in bash shell?

I am still learning shell scripting. Recently I see a function for read configuration. But some of special character make me confused. I checked online to find answer. It was not successful. I post the code here to consult with expert or guru to get better understanding on these special characters... (3 Replies)
Discussion started by: duke0001
3 Replies

2. Shell Programming and Scripting

Reading of variable in a single line command

Hi All, Below is a sample command that I can run without any problem in the command line. Command Line dtToday=`date +%Y%m%d`; ls -ltr ./filename_${dtToday}.txt -rw-r--r-- 1 monuser oinstall 0 Jan 18 11:02 ./filename_20130118.txt But once I put that command line in file (list.txt) and... (3 Replies)
Discussion started by: padi
3 Replies

3. Shell Programming and Scripting

How to avoid the truncating of multiple spaces into a single space while reading a line from a file?

consider the small piece of code while read line do echo $line done < example content of example file sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the output is like sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the... (4 Replies)
Discussion started by: Kesavan
4 Replies

4. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

5. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

6. Shell Programming and Scripting

Looking for a single line to count how many times one character occurs in a word...

I've been looking on the internet, and haven't found anything simple enough to use in my code. All I want to do is count how many times "-" occurs in a string of characters (as a package name). It seems it should be very simple, and shouldn't require more than one line to accomplish. And this is... (2 Replies)
Discussion started by: Shingoshi
2 Replies

7. UNIX for Dummies Questions & Answers

Trying to remove single character from a line

Here is a sample code grep '903' -i user.txt | tail -2 | awk '{print $2}' | sed 's/B//g' the input file has data as such 903-xxx-xxxxB 903-xxx-xxxxB It is a dialer file i want to remove the "B" any help thanks (5 Replies)
Discussion started by: Iz3k34l
5 Replies

8. Shell Programming and Scripting

Reading Multiple Variables From a Single Line in Shell

I'm a Linux newb, I've been running a Debian Linux server for about a year now, and I've written some simple scripts to automate various things, but I still don't know much, and I forget what I learn as fast as I figure it out... Anyway, that really isn't important, I just want you to know that... (14 Replies)
Discussion started by: Drek
14 Replies

9. Shell Programming and Scripting

reading a single line

hello, i need an algorithm which reads a line specified to it in shortest possible time. i am already using sed, i need a better way. please suggest me alternatives (2 Replies)
Discussion started by: rochitsharma
2 Replies

10. Programming

reading a single character in C

Can anyone help me????? My problem is that i want to read only one charcter from keyboard. Each time my program waits to press enter or ^d. I don't want that. As soon as i press a charcter it should proceed to next statement in program without pressing enter or ^d. please help... (3 Replies)
Discussion started by: alodha
3 Replies
Login or Register to Ask a Question