Find the answer to your Linux question:
Results 1 to 7 of 7
Someone can give me some clue or script in writing the below requirement . I am having 5 or 10 files of unix files which contain ^M charactes. Each file ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    4

    Post Find ^M characters and then concat and then remove it

    Someone can give me some clue or script in writing the below requirement .

    I am having 5 or 10 files of unix files which contain ^M charactes.


    Each file contains 10 to 50 lines consists of ^M characters like


    CDMA 1x=5102305, 2008-09-05 07:59:04, NAI=$QCMIPNAI^M
    0000000000@vzw3g.com, MIN=0000007356, Config=62558-11 Verizon Std P3



    First we have to find ^M character and concat the line where it has broken and then we have to remove the ^M character from the unix files .That is my requirement.


    Thanks

  2. #2
    Just Joined!
    Join Date
    Aug 2007
    Posts
    25
    Quote Originally Posted by itsbunny08 View Post
    Someone can give me some clue or script in writing the below requirement .

    I am having 5 or 10 files of unix files which contain ^M charactes.


    Each file contains 10 to 50 lines consists of ^M characters like


    CDMA 1x=5102305, 2008-09-05 07:59:04, NAI=$QCMIPNAI^M
    0000000000@vzw3g.com, MIN=0000007356, Config=62558-11 Verizon Std P3



    First we have to find ^M character and concat the line where it has broken and then we have to remove the ^M character from the unix files .That is my requirement.


    Thanks
    If you have it installed, dos2unix command convert the file for you.

    From within a program you can do it with sed.
    # cat file | sed 's/^M$//' > newfile
    NOTE the ^M is a control M and is inserted using Control V Control M

    If EVERYLINE in the file has a control M then you can use this version of sed.

    sed 's/.$//'

  3. #3
    Just Joined!
    Join Date
    Oct 2008
    Posts
    4
    Is there any automated script in concating it by checking first ^M character in the file.

    Thanks

  4. #4
    Just Joined!
    Join Date
    Aug 2007
    Posts
    25
    You could always grep for control M and if it exist run the sed command but unless your files are extremely large just runt he all through would be just as fast.

  5. #5
    Just Joined!
    Join Date
    Oct 2008
    Posts
    4

    Post

    Found the script manually using

    sed '/^M$/N;s/^M\n//' filenew > filenew1.txt

    But I am trying to do automatic the script is below which is not working.

    For ^M character --> I am pressing < crtl v and Ctrl M> in the script.

    for file in `ls /home/applmgr/test/`
    do
    CHECK=`cat $file | grep "^M" | wc -l`
    if [ $CHECK -eq 1 ]
    then
    sed '/^M$/N;s/^M\N//' $file > /tmp/tempfile.tmp
    mv /tmp/tempfile.tmp $file
    fi
    done


    Please let me know if any clue or suggestion for making automatic



    Thanks

  6. #6
    Just Joined!
    Join Date
    Aug 2007
    Posts
    25
    Quote Originally Posted by itsbunny08 View Post
    Found the script manually using

    sed '/^M$/N;s/^M\n//' filenew > filenew1.txt

    But I am trying to do automatic the script is below which is not working.

    For ^M character --> I am pressing < crtl v and Ctrl M> in the script.

    for file in `ls /home/applmgr/test/`
    do
    CHECK=`cat $file | grep "^M" | wc -l`
    if [ $CHECK -eq 1 ]
    then
    sed '/^M$/N;s/^M\N//' $file > /tmp/tempfile.tmp
    mv /tmp/tempfile.tmp $file
    fi
    done


    Please let me know if any clue or suggestion for making automatic



    Thanks
    Why not just do the following

    for file in `ls /home/applmgr/test/`
    do
    dos2unix $files.
    done



    Again unless your files a GB in size and you have LOTS of big files you will spend as much time checking for ^m as you will fixing them.
    Your check goes through the entire file then goes back through it to change if a file exist. With dos2unix or sed you read the file once

  7. #7
    Just Joined!
    Join Date
    Oct 2008
    Posts
    4
    Dos2unix is not working .

    How to concat the line after finding the ^M character.

    Thanks

Posting Permissions

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