grep "\012$" problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep "\012$" problem
# 1  
Old 03-29-2008
grep "\012$" problem

Hi,
I am trying to weed out DOS '^M' new-line characters from a file which I copied from windows inside my Linux box, using this following script. But it seems this script is not working properly if I have made a check 'xargs grep -l "\012$"'. Here, I am trying to filter only the files which contains the octal code of '\n'. Though I can grep directly '^M', I want to know why this script fails at this point, so pls tell me what I am doing wrong.

Code:
for fil in `find . -type f -name "*" | xargs grep -l "\012$" | xargs file | grep text | cut -d: -f1`
do
    tr -d "\015\032" < $fil > "${fil}".tmp
    mv "${fil}".tmp $fil
done

# 2  
Old 03-29-2008
Why are you looking for \n? By definition, if they are text files (and do not consist of a single line without any line ending terminator) they will have newlines in them. And grep can't grep for those, it greps the actual contents of the line but internally doesn't see the newlines.

Grepping for \015 (aka ^M, aka \r) before you attempt to remove it would make some sense. But unless the files are very large, it might make more sense to simply tr them all and then just remove the temp file if it's identical to the original.

A large backtick expression like that is pretty hard to debug, kudos for pulling it off. I would have refactored it into a while loop, though.

Code:
find . -type f |  # name "*" is redundant, isn't it?
# xargs grep -l '\015$' |  # ?  or grep -l '^M$' (that's opening quote, ctrl-v ctrl-m dollar sign, closing quote)
xargs file |
grep text |
cut -d: -f1 |
while read fil; do ...

My GNU grep can't grep for \octal codes anyway.
# 3  
Old 03-29-2008
if you are just getting rid of new line characters, run your files through the dos2unix command.
# 4  
Old 03-31-2008
Quote:
Originally Posted by era
Why are you looking for \n? By definition, if they are text files (and do not consist of a single line without any line ending terminator) they will have newlines in them. And grep can't grep for those, it greps the actual contents of the line but internally doesn't see the newlines.

Grepping for \015 (aka ^M, aka \r) before you attempt to remove it would make some sense. But unless the files are very large, it might make more sense to simply tr them all and then just remove the temp file if it's identical to the original.

A large backtick expression like that is pretty hard to debug, kudos for pulling it off. I would have refactored it into a while loop, though.

Code:
find . -type f |  # name "*" is redundant, isn't it?
# xargs grep -l '\015$' |  # ?  or grep -l '^M$' (that's opening quote, ctrl-v ctrl-m dollar sign, closing quote)
xargs file |
grep text |
cut -d: -f1 |
while read fil; do ...

My GNU grep can't grep for \octal codes anyway.
Thanks era for a clear illustration.

Could you tell me what purpose does '\032' serve in my script, i.e what symbol it is for?

Quote:
Originally Posted by ghostdog74
if you are just getting rid of new line characters, run your files through the dos2unix command.
Thanks ghostdog for the suggestion, however I would like to use script only rather a command.

Last edited by royalibrahim; 03-31-2008 at 06:34 AM..
# 5  
Old 03-31-2008
\032? No idea, but I bet your man ascii will tell you it's a control code one less than ESC called SUB. Never seen that in anything but then I have no idea what file type you are processing anyway. Ask somebody who does?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with error "por: 0403-012 A test command parameter is not valid."

Hi, im asking for help with the next script: echo ^; then if then printf "\033 query1.sh: export TERM=vt100 export ORACLE_TERM=at386 export ORACLE_HOME=/home_oracle8i/app/oracle/product/8.1.7 export ORACLE_BASE=/home_oracle8i/app/oracle export... (8 Replies)
Discussion started by: blacksteel1988
8 Replies

2. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

3. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

4. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

5. AIX

xx=`date +"%a %b %d"`;rsh xxx grep "^$XX" zzz ?

AIX 4.2 I am trying to do an rsh grep to search for date records inside server logs by doing this : xx=`date +"%a %b %d"` rsh xxx grep "^$XX" zzz gives : grep: 0652-033 Cannot open Jun. grep: 0652-033 Cannot open 11. But if I do : xx=`date +"%a %b %d"` grep "^$XX" zzz it works... (2 Replies)
Discussion started by: Browser_ice
2 Replies

6. UNIX for Advanced & Expert Users

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have a problem about the Oracle related components. I'm not able to find any answer yet, and waiting for your responses... Here is the configuration of my system: * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or... (1 Reply)
Discussion started by: talipk
1 Replies

7. UNIX and Linux Applications

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or question of my own) is: Oracle tns listener, "CT_LISTENER", and the enterprise manager (EM) of the instance, which is uniq instance and called... (0 Replies)
Discussion started by: talipk
0 Replies

8. Shell Programming and Scripting

ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt"

Hi, I don't know hot to make this command work: ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt" It should return the list of file .txt It's important to search .txt at the end of the line, becouse some file name have "txt" in their name but have other extensions (13 Replies)
Discussion started by: DNAx86
13 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. Shell Programming and Scripting

grep to find content in between curly braces, "{" and "},"

problem String ~~~~~~~~~~~~~~~~~~ icecream= { smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" } aend = {smart vc4 eatr kalu} output needed ~~~~~~~~~~~~~~~~~~ smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" smart vc4... (4 Replies)
Discussion started by: keshav_rk
4 Replies
Login or Register to Ask a Question