Removing \r and \n during reading file through while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing \r and \n during reading file through while loop
# 1  
Old 08-29-2014
Removing \r and \n during reading file through while loop

Hi,
I am writing in a file through cat command. This file will contain the path of file along with filename. e.g. /home/user/folder1/folder2/filename.txt
There might be very large number of this path in same file like say 140

when I try to run while command:
Code:
while read -r file
do
 //command
done

Here I am able to see the file path which has '\n' as EOL but those which have '\r' are skipped.

I also tried using IFS='\r\n' but this splitted the file path from letter 'r'.

I need to display in the below format:
Code:
/home/user/folder1/folder2/filename.txt
/home/user/folder1/folder2/filename1.txt
/home/user/folder1/folder2/filename2.txt
/home/user/folder1/folder2/filename3.txt
/home/user/folder1/folder2/filename4.txt
/home/user/folder1/folder2/filename5.txt
/home/user/folder1/folder2/filename6.txt
/home/user/folder1/folder2/filename7.txt

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags to mark code, output and similar data. Thank you.

Last edited by bakunin; 08-29-2014 at 04:21 AM..
# 2  
Old 08-29-2014
Hello Pulkit,

It is advised to use code tags for any commands and code as per forum rules. Before clicking on submit button you can preview your post. Could you please be more clear in your requirement, also kindly let us know the input and expected output on same.


Thanks,
R. Singh

Last edited by rbatte1; 08-29-2014 at 09:52 AM.. Reason: Spelling
# 3  
Old 08-29-2014
Hi Ravinder,
I have one executable file of two version i.e. 1st version - 114, 2nd version - 120
I am converting from one file to another say bmp to pdf in these 2 versions.
Once the file is converted, a output file is created in pdf format in both versions folder.

There paths with filename are stored in a textfile using cat command. I am now reading this textfile using while loop to read the filepaths.
The problem is in the while loop when it is reading the textfile, it is accepting only the filepaths which has '\n' (New Line) at the end of line
Those which has '\r' (Carriage Return) are not accepted and skipped.
# 4  
Old 08-29-2014
Quote:
Originally Posted by Pulkit Lall
I also tried using IFS='\r\n'
You were on the right track, but the shell doesn't work like printf. "\r" is just an escaped "r" (an "r" made sure to be literally meant) and this is why it split at the letter "r".

To use control codes you need to enter them as they are, (single-) quoted like you did, but otherwise "uncooked". Depending on the editor you use there are different ways to accomplish this, here is how it is done in vi:

Enter a CTRL-V in input mode. The next character you enter is interpreted as literal, then.

Press "<ENTER>", for instance, and a "^M" will appear. Notice that this is one character, not two! You see that when you switch back to command mode and go over the character with the cursor. Pressing "<TAB>" instead will give you a "^I", which is the display equivalent of the TAB-character (you will see these control characters also when you set your vi to ":set line". Switch back to normal via ":set nolines".

I hope this helps.

bakunin

/PS: this works on the command line too if you use Korn shell and switch to vi-mode ("set -o vi").

EDIT: you posted while i was writing my answer. Probably there is a way easier solution for your problem: you got your file perhaps transferred from some DOS/Windows system and this is what causes your problem.

Either: ftp your file with the "ASCII"-mode set instead of the default binary next time;
or: change the file by running it through one of these "dos2unix"-commands or something similar;
or: do that yourself with a small sed-script. Notice, you will need the above-mentioned method for this, the "^M" is a literally entered "<ENTER>".

Code:
sed 's/^M$//' /your/input/file > /some/output/file


Last edited by bakunin; 08-29-2014 at 04:41 AM..
# 5  
Old 08-29-2014
Hello Pulkit,

You can use gsub utility of awk by using \r in it. One very good example is present in following thread, just take a look on same.

Awk to remove carriage return from 65th field


Thanks,
R. Singh
# 6  
Old 08-29-2014
Quote:
Originally Posted by Pulkit Lall
[..]it is accepting only the filepaths which has '\n' (New Line) at the end of line
Those which has '\r' (Carriage Return) are not accepted and skipped.
They are not some much skipped, but the output gets printed and then overwritten, since the Carriage Return makes the the writes start from the beginning of the line...

There is no need to specify '\n' for IFS (it won't hurt either) since the newlines will be automatically stripped by the read command...

Alternative methods of specifying a CR as IFS:

Code:
CR=$(printf '\r')
while IFS=$CR read file; do
  printf "%s\n" "$file"
done < infile

or bash/ksh93/zsh:
Code:
while IFS=$'\r' read file; do
  printf "%s\n" "$file"
done  < infile

These methods only change IFS local to the read command. So IFS retains its old value and the old IFS does not need to be saved first and can remain at the default of $' \t\n'

Of course one can always remove it from the file first:
Code:
tr -d '\r' < infile |
while read file; do
  printf "%s\n" "$file"
done

or bash/ksh93/zsh:
Code:
while read file; do
  printf "%s\n" "$file"
done < <(tr -d '\r' < infile)


Last edited by Scrutinizer; 08-29-2014 at 06:15 AM..
# 7  
Old 08-29-2014
None of them worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sequential Reading from two file in a loop

Hello All, I have two files with me file1.txt and file2.txt file1.txt has: 333 222 111 file2.txt has ccc bbb aaa ccc is related to 333 only, bbb is related to 222 only and aaa is related to 111 only. I have to get the values from each of the file and pass them in the URL... (3 Replies)
Discussion started by: ankur328
3 Replies

2. Shell Programming and Scripting

While loop reading file with multiple conditions

Hi Am trying to print the PIDs of process in a file and trying to grep any PID from that file I set the if condition as $value != "PID" and $value != "-" Assign that number to a variable Am confused since am using while loop to read the line from file and again if condition to check those... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. Shell Programming and Scripting

Loop is not reading tabs from the file

Hi, I am on HP-UX and K shell. When I am using while/for loop for reading a file. It is working fine but not reading tabs: Suppose, if the line is: ; ;COMP; ; ; ; then loop is reading as ; ;COMP; ;... (5 Replies)
Discussion started by: ezee
5 Replies

4. Shell Programming and Scripting

While loop is not reading next line in the file when IF condition is used.

Hi Guys I am new to scripting.Please forgive for asking basic questions. I want to write a script to check whether the logs are getting updated in last 15 mins. cat server 192.168.1.6 192.168.1.7 cat list 192.168.1.7 /logs/logpath1 192.168.1.7 /logs/logpath2 192.168.1.6... (4 Replies)
Discussion started by: vdurai
4 Replies

5. Shell Programming and Scripting

Problem reading file in while/read loop

I know I should be able to see a way of doing this easily, but my brain just won't engage. I have a script working on an embedded device that checks to see if an item is in a blacklist before performing some actions. At the moment the code reads thus.... while read BLACKLIST ; do ... (7 Replies)
Discussion started by: Bashingaway
7 Replies

6. AIX

How to pause a while loop while reading from a file

Hi, I am building a script to grep for a string in all the files from a folder and display the results. I am reading the files one by one by placing the names in other file using while loop my code is as below while read inp do chk=`grep -c "$str" $pth/$inp` ... (2 Replies)
Discussion started by: sekhar gajjala
2 Replies

7. Shell Programming and Scripting

How to get the modified value of variable outside the while loop reading from a file

Hi Friends , Sorry if this is a repeated question , The input file contains 5 lines , so the the values of the variables i and count should b i=5; count=15 but the variables are not updating , the value of variables showing i=0 and count =0 only.:mad: can any1 help me please. (11 Replies)
Discussion started by: babusek
11 Replies

8. Shell Programming and Scripting

Not access variable outside loop when a reading a file

I am writing a shell script using the korn shell. It seems that I am only able to use local variables within a while loop that is reading a file. (I can't access a variable outside a previously used while loop.) It's been a while since I wrote shell scripts. Here is a sample cat file.txt... (4 Replies)
Discussion started by: ricardo.ludwig
4 Replies

9. UNIX for Dummies Questions & Answers

How to make a loop base on reading a file?

To make it clearer: I have a file, List.txt List.txt contains: (these are actually splitted files w/c I got from ls command and dump them to the List.txt file) SAMPLEa SAMPLEb SAMPLEc SAMPLEd SAMPLEe SAMPLEf . . . . . And I want to rename these files to have a .dat extension.... (3 Replies)
Discussion started by: JohnBalayo
3 Replies

10. HP-UX

How to make a loop base on reading a file?

Need help on making a loop script base on what is inside a file... File to read: List.txt List.txt contains below w/c are file name as well: SAMPLEa SAMPLEb SAMPLEc SAMPLEd SAMPLEe SAMPLEf . . . Want to make a loop that will manipulate those that are inside the file.txt w/c are... (3 Replies)
Discussion started by: JohnBalayo
3 Replies
Login or Register to Ask a Question