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:
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:
Moderator's Comments:
edit by bakunin: please use CODE-tags to mark code, output and similar data. Thank you.
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
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.
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>".
[..]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:
or bash/ksh93/zsh:
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:
or bash/ksh93/zsh:
Last edited by Scrutinizer; 08-29-2014 at 06:15 AM..
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)