DBAsupport.com Forums - Powered by vBulletin
Results 1 to 9 of 9

Thread: UNIX question

  1. #1
    Join Date
    Mar 2000
    Posts
    39

    UNIX question

    Hello everyone,

    How can I find the number of occurences of a certain character in a text file in unix? Thanks for your help.
    Thanks,
    Afshin.

  2. #2
    Join Date
    Jun 2001
    Location
    California
    Posts
    124
    cat filename | grep yourtext | wc -l

  3. #3
    Join Date
    Mar 2000
    Posts
    39
    Thank you!
    Thanks,
    Afshin.

  4. #4
    Join Date
    May 2002
    Posts
    2,645
    No need to cat.

    grep yourtext filename | wc -l

  5. #5
    Join Date
    Mar 2000
    Posts
    39
    Unfortunately I get 1, which is wrong. I am using the desktop utility Cygwin. I am counting the number of times the pipe delimiter appears in my row. Here is the data:

    *18*resp|status|fproj|timezone|odate|otime|otimer|oiname|oattmpt|odow|cdate|ctime|ctimer|ciname|catt mpt|cdow|cshift|dpquota|*20*dispo|ismssmp|qpdate|qprun|qkey|qtz|qpk|qupid|qinvtype|qusecd|qwtprior|q wholesp|qownini|qtype|qlstdat|qcon|qmarket|dqtype|up1|s1|*100*s4|softref|s2|s5|so|nphn|nrintro|s3|s6 |so2|so3|q1a|q1b|q1c|q1d|q1e|q1f|q2_1|q2_2|*80*q2_3|q2_4|q2_5|q2_6|q2_7|q3|q4a|q4b|q5_1|q5_2|q5_3|q5 _4|q6_1|q6_2|q6_3|q7|q7@|q8|q9|q9a|*60*q10|q11a|q11b|q11c|q11d|q11e|q12|qu13@|q13_1|q13_2|q13_3|q13_ 4|q13_5|q14_1|q14_2|q14_3|q14_4|q14_5|q14_6|q14_7|*40*q14_8|q14_9|q14_10|q15_1|q15_2|q15_3|q15_4|q15 _5|q15_6|q15_7|q15_8|q15_9|q15_10|q16_1|q16_2|q16_3|q16_4|q16_5|q16_6|q16_7|*20*q16_8|q16_9|q16_10|q 17|q18|q19_1|q19_2|q19_3|q19_4|q19_5|q20_1|q20_2|q20_3|q20_4|q20_5|q20_6|q20_7|q21|q22|q23_1|*100*q2 3_2|q23_3|q23_4|q23_5|q23_6|q24_1|q24_2|q24_3|q24_4|q25|q25a|q25a@|q26|q27|qu28@|q28_1|q28_2|q28_3|q 28_4|q28_5|*80*q28_6|q28_7|q28_8|q28_9|q28_10|q29a|q30|q31|q32_1|q32_2|q32_3|q32_4|q32_5|q32_6|q32_7 |q32_8|q33|q34|q35|q35a|*60*q36_1|q36_2|q36_3|q36_4|q36_5|q36_6|q36_7|q36_8|q36_9|q36_10|q37_1|q37_2 |q37_3|q37_4|q37_5|q37_6|q37_7|q37_8|q37_9|q37_10|*40*q38oe|q38.1|q38.2|q38.3|q38.4|q38.5|q38@|q39|q u40@|q40_1|q40_2|q40_3|q40_4|q40_5|q40_6|q40_7|q40_8|q41|qu42@|q42_1|*20*q42_2|q42_3|q42_4|q42_5|q42 _6|q42_7|q42_8|q44|q45|q46|q47_1|q47_2|q47_3|q47_4|q47_5|q47_6|q47_7|q47_8|qu48@|q48_1|*100*q48_2|q4 8_3|q48_4|q48_5|q48_6|q48_7|q48_8|q49|q50|q51|q52_1|q52_2|q52_3|q52_4|q52_5|q52_6|q52_7|q52_8|q52_9| q52_10|*80*q52k|q52koe.1|q52koe.2|q52koe.3|q52koe.4|q52koe.5|q52koe@|q52krate|q53oe|q53.1|q53.2|q53. 3|q53.4|q53.5|q53@|q54|q55|q56_1|q56_2|q56_3|*60*q56_4|q56_5|q56_6|q56_7|q56_8|q56_9|q56_10|q57_1|q5 7_2|q57_3|q57_4|q57_5|q57_6|q57_7|q57_8|q57_9|q57_10|q57_11|q58|q59_1|*40*q59_2|q59_3|q59_4|q59_5|q5 9_6|q59_7|q59_8|q59_9|q60|q61_1|q61_2|q61_3|q61_4|q62|q63|q64|q65_1|q65_2|q65_3|q65_4|*20*q66|q67|q6 8a|q68b|qchk|fulladra|eaddr|fend|fdate|finame|ftimedi|ftimedr|fshift|fdow|tabseg9|actual|predict|bgi seg6|bgiseg9|channel

    Why I get 1 is beyond me. Any ideas? Thank you!
    Thanks,
    Afshin.

  6. #6
    Join Date
    May 2002
    Posts
    2,645
    No mystery there. Searching for special characters is a different animal. The wc shows what it is supposed to - number of lines. You have one row, and "|" is in that row, that's why you get 1.

  7. #7
    Join Date
    May 2002
    Posts
    2,645
    You can write a script that combines the following steps:
    copy your file and use the copy as a working file
    strip out the pipes using tr command, saving output into a new file
    do a difference on number of characters between the files

    To strip out the pipe, save the results in a new file:
    tr -d \| '<' input_file '>' output_file (ignore the single quotes around the angle brackets)

    wc -c input_file | awk '{print $1}' minus wc -c output_file | awk '{print $1}'

    I get 2041 minus 1705 = 336, confirmed in a word processor
    Last edited by stecal; 02-26-2004 at 05:23 PM.

  8. #8
    Join Date
    Mar 2000
    Posts
    39
    Great, thank you!
    Thanks,
    Afshin.

  9. #9
    Join Date
    Mar 2002
    Location
    Manchester, England
    Posts
    202
    Cygwin is a life saver - esspecially if you've moved from Unix to Windoz.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width