How to retain backslash in a line while reading a data file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to retain backslash in a line while reading a data file?
# 1  
Old 09-13-2010
CPU & Memory How to retain backslash in a line while reading a data file?

Hello Firends

I have a file that contains data within single quotes, which has meaning of its own. When I am trying to parse through the file for a different functionality I noticed that I was loosing the backslash when occurrences in the file look like ('\0'). I would want to retain the backslash. Please advise. Below is the kind of script I want, but with the backslash retained. Thanks in advance.

Example BEGIN


Input File

Code:
$ cat t9.dat
type string1_t = utf8 string('\0');
type string2_t = utf8 string('\0');
type string3_t = utf8 string('\0');
type string4_t = utf8 string('\0');

Command line Script : Example segment

Code:
$cat t9.dat | while read LINE
> do
> echo $LINE
> done
type string1_t = utf8 string('0');
type string2_t = utf8 string('0');
type string3_t = utf8 string('0');
type string4_t = utf8 string('0');
$

Note that the output above is missing the \ character.

EXAMPLE END


Thank You!!

Last edited by pludi; 09-14-2010 at 03:24 AM..
# 2  
Old 09-13-2010
Try:
Code:
while read -r line

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 09-14-2010
Thanks Scrutinizer, that was helpful.

But I also had to do echo -E $LINE, probably because of my shell default.

Code:
$cat t9.dat | while read -r LINE
> do
> echo -E $LINE
> done
type string1_t = utf8 string('\0');
type string2_t = utf8 string('\0');
type string3_t = utf8 string('\0');
type string4_t = utf8 string('\0');

how do I find out my SHELL default?

Thanks
Mani

Moderator's Comments:
Mod Comment Please use code tags instead of font or color formatting

Last edited by pludi; 09-14-2010 at 03:25 AM..
# 4  
Old 09-14-2010
Yes, the implementation of echo varies. It is best to use:
Code:
printf "%s\n" "$line"

which works the same on all platforms
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

3. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

4. Shell Programming and Scripting

retain last 1000 line in a file

I have large file with around 100k+ lines. I wanted to retain only the last 100 lines in that file. One way i thought was using tail -1000 filename > filename1 mv filename1 filename But there should be a better solution.. Is there a way I can use sed or any such command to change the... (9 Replies)
Discussion started by: nss280
9 Replies

5. Shell Programming and Scripting

Reading data from different position of a line

I want help reading data from different position of a line cat filename | cut -c120-131 6263-6280 or cat filename | cut -c120-131 -c6263-6280 doesn't seem to work. Please help jak (2 Replies)
Discussion started by: jakSun8
2 Replies

6. Shell Programming and Scripting

Reading data from a specific line in a text file

Hello, I have a problem which is giving me headache for days, can some please help. Please see code and text fiel below. Please see text in red for the problem I am facing # Program gets an input x from user while read line ; do echo... (4 Replies)
Discussion started by: jermaine4ever
4 Replies

7. Shell Programming and Scripting

Reading data from a specific line in a text file

hello, I have got the following problem that I am hoping someone can help with please. 1. I have got the following text file (below) , the columns data are 'Test Day', 'Board', 'Betting Number'. TEXT FILE ============================================ 1 3 02-01-27-28-29-30 0 1... (1 Reply)
Discussion started by: jermaine4ever
1 Replies

8. Shell Programming and Scripting

Shell Script -- problem reading backslash(\)!!

Hello! I am writing a program that reads a bunch of arguments from the command line,then read information from a file(passed as one of the arguments) and do some computation. The problem I am facing is when a backslash(\) is present as one of the arguments, suppose $ myprog \ abc xyz,the backslash... (2 Replies)
Discussion started by: rossi143
2 Replies

9. Shell Programming and Scripting

Using loop reading a file,retrieving data from data base.

Hi All, I am having trouble through, I am reading the input from tab delimited file containing several records, e.g. line1 field1 field2 field3 so on.. line2 field1 field2 field3 so on.. .. .. on the basis of certain fields for each record in input file, I have to retrieve... (1 Reply)
Discussion started by: Sonu4lov
1 Replies
Login or Register to Ask a Question