Read character by character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read character by character
# 1  
Old 04-18-2011
Read character by character

Guys,

Here is the input text file

Code:
<7001> 34 789 701                                                               2
<HJS1>                                                                          2
<HJS2>                                                                          2
<HJS3>                                                                          2
<HJS4>                                                                          2
<HJS5>                                                                          2
<HJS6>                                                                          2
<9134>                                                                          2
<6011> 37<NL>   38<NL>   39<NL>   40<NL>   41<BL>   42<NL>   43<NL>             2
       44<NL>   45<NL>   46<BL>   47<NL>   48<NL>   49<NL>   50<NL>             2
       51<BL>   52<NL>   53<NL>   54<NL>   55<NL>   56<BL>   57<NL>             2
       58<NL>   59<NL>   60<NL>   61<BL>   62<NL>   63<NL>   64<NL>             2
       65<NL>   66<BL>   67<NL>   68<NL>   69<NL>   70<NL>   71<BL>             2
<6012>     0.34417<NL> 0.37000<NL> 0.39667<NL> 0.42750<NL> 0.46750<BL> 0.51417  2
<NL> 0.57000<NL> 0.63667<NL> 0.70667<NL> 0.77250<BL> 0.84500<NL> 0.88583        2
<NL> 0.93250<NL> 0.99917<NL> 1.07917<BL> 1.18833<NL> 1.31167<NL> 1.46667        2
<NL> 1.64250<NL> 1.82167<BL> 2.01167<NL> 2.16167<NL> 2.33583<NL> 2.54667        2
<NL> 2.80833<BL> 3.12500<NL> 3.47833<NL> 3.84167<NL> 4.20083<NL> 4.54833        2
<BL> 4.88833<NL> 5.24667<NL> 5.60333<NL> 6.03750<NL> 6.51500                    2
<7005> 72<NL>   73<NL>   74<NL>   75<NL>   76<BL>   77<NL>   78<NL>             2
       79<NL>   80<NL>   81<BL>   82<NL>   83<NL>   84<NL>   85<NL>             2
       86<BL>   87<NL>   88<NL>   89<NL>   90<NL>   91<BL>   92<NL>             2
       93<NL>   94<NL>   95<NL>   96<BL>   97<NL>   98<NL>   99<NL>             2
      100<NL>  101<BL>  102<NL>  103<NL>  104<NL>  105<NL>  106<BL>             2
<7006>     7.16833<NL> 7.83000<NL> 8.50333<NL> 9.29250<NL>10.14000<BL>11.13083  2
<NL>12.28833<NL>13.62500<NL>15.07417<NL>16.72583<BL>18.42333<NL>20.23917        2
<NL>22.25583<NL>24.68833<NL>27.42917<BL>30.47500<NL>33.79333<NL>37.36417        2
<NL>41.16250<NL>44.73583<BL>48.53917<NL>52.65417<NL>57.14083<NL>62.39667        2
<NL>67.32167<BL>72.84417<NL>79.09750<NL>83.33250<NL>83.33250<NL>83.33250        2
<BL>83.33250<NL>83.33250<NL>83.33250<NL>83.33250<NL>83.33250                    2
<7007>107<NL>  108<NL>  109<NL>  110<NL>  111<BL>  112<NL>  113<NL>             2
      114<NL>  115<NL>  116<BL>  117<NL>  118<NL>  119<NL>  120<NL>             2
      121 AND<NL>                                                               2
      OVER                                                                      2
<7008>    83.33250<NL>83.33250<NL>83.33250<NL>83.33250<NL>83.33250<BL>83.33250  2
<NL>83.33250<NL>83.33250<NL>83.33250<NL>83.33250<BL>83.33250<NL>83.33250        2
<NL>83.33250<NL>83.33250<BL> 0.00000                                            2
<NL    /DMRG>11-100-4-1                                                         2

I want to make 2 txt files which are as follows

txt1
[
Code:
37
38
39
40 and so on

txt2
Code:
0.34417
0.37000
0.39667
0.42750 and so on

Its reading the values against the copy mark <6011>, <7005>, <7007> in input file for txt1
and reading <6012>, <7006>, <7008> for txt2.

Any help is appreciated...
# 2  
Old 04-18-2011
See if this works for you:

Code:
#!/usr/bin/ksh
mFlag='N'
rm -f mFile1
rm -f mFile2
while read mLine
do
  mC1=$(echo ${mLine} | cut -c1)
  mC6=$(echo ${mLine} | cut -c6)
  if [[ "${mC1}" = "<" && "${mC6}" = ">" ]]; then
    mTag=$(echo ${mLine} | cut -c1-6)
    if [[ "${mTag}" = "<6011>" || "${mTag}" = "<7005>" || "${mTag}" = "<7007>" ]]; then
      mFlag='1'
    else
      if [[ "${mTag}" = "<6012>" || "${mTag}" = "<7006>" || "${mTag}" = "<7008>" ]]; then
        mFlag='2'
      else
        mFlag='N'
      fi
    fi
  fi
  case "${mFlag}" in
    "1") echo ${mLine} >> mFile1;;
    "2") echo ${mLine} >> mFile2;;
  esac
done < mInp_File
sed 's/<....>/ /g;s/<..>/ /g;s/ 2$//' mFile1 | xargs | tr ' ' '\n' | egrep -i '[a-z/-]' > mFinal1
sed 's/<....>/ /g;s/<..>/ /g;s/ 2$//' mFile2 | xargs | tr ' ' '\n' | egrep -i '[a-z/-]' > mFinal2

# 3  
Old 04-18-2011
try:

Code:
sed 's/2$//' file|
awk '/<[0-9]+>/{print $0;while ((getline line)>0) {if(line~/^<[0-9]+>/) {print "";printf line}else{printf line}}}' |
awk -F'>| +' '{$1=""}!/\./&&/[0-9]+</ {print |"grep -Eo '[0-9]+' > txt1"} /\./{print |"grep -Eo '[0-9]+[.][0-9]+' >txt2"}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 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. UNIX for Dummies Questions & Answers

read from character 23 through character 32

I need a one-liner that will output characters 23 through 32 from a user defined record. Thanks, Kenny. (1 Reply)
Discussion started by: kenneth.mcbride
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

Deleting all characters from 350th character to 450th character from the log file

Hi All, I have a big log file i want to delete all characters (between 350th to 450th characters) starting at 350th character position to 450th character position. please advice or sample code. (6 Replies)
Discussion started by: rajeshorpu
6 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. UNIX for Dummies Questions & Answers

read a variable character by character, substitute characters with something else

im having trouble doing this: i have a variable with 2 characters repeating e.g. aababbbaaaababaabbaabbba is there a way i can search the variable for a's and b's and then change a's to b's and b's to a's? im guessing its like getting the 1's compliment of the string im doing this in... (2 Replies)
Discussion started by: vipervenom25
2 Replies

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