why am I losing line-end chars


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting why am I losing line-end chars
# 1  
Old 03-29-2010
why am I losing line-end chars

Hello,

I do not know why the output from these two methods differs. One method retains the newlines, the other method appears to ignore or lose the newlines.

Writing a file with the redirection operator:

egrep -e 'matchstring' infile.txt > outfile.txt

The resulting outfile.txt contains separated line when viewing the file.

However, when I do this method (with the goal of having all the output lines in a variable called OUTFILE), the resulting text appears to have lost all the line breaks:

OUTFILE=$(egrep -e 'matchstring' infile.txt)

Thank you!
# 2  
Old 03-29-2010
The reason is that several characters - newline among them - have special meaning to the shell and are therefore interpreted instead of used like "normal" characters.

The general way to protect these special characters - that is, make the shell treat them like they were normal ones - is to enclose a string into double quotes:

Code:
myvar="foo bar"      # works
myvar=foo bar        # will not work because the space after foo is interpreted
                     # as end-of-statement and that leaves "bar", which the shell
                     # cannot understand.

In your case you have to protect your variable two times: first when you fill it with content, second when you print it out. The reason is that the shell interprets the commands in a certain step-by-step process: in one step all the variables are replaced by their content and in the next step the command is interpreted. To understand what i mean make the following test:

Code:
myvar="-lisa a*"
ls ${myvar}
ls "${myvar}"

With the first command you will see the same directory listing format as if you had entered "ls -lisa a*" directly. The reason is that first the shell replaces "${myvar}" with its contents and only then executes the resulting line as the string "-lisa a*" was not protected it splits into two distinct strings, the first one being "-lisa" and the second one being "a*". This is exactly what the ls command expects.

The second command will give a strange error message. Because we protected the string it is still one string, the space character included. The ls command doesn't have any option "a*" (this can only be a parameter) and it doesn't have an option " " (space) either! This is where the error message will come from.

You see this mechanism of protecting (or not protecting) variables can be used both ways. Here is your solution:

Code:
OUTFILE="$(egrep -e 'matchstring' infile.txt)"    # notice the double quotes

echo "$OUTFILE"            # with newlines
echo $OUTFILE              # newlines replaced by spaces

I hope this helps.

bakunin
# 3  
Old 03-29-2010
Code:
IFS=""
OUTFILE=$(egrep -e 'matchstring' infile.txt)

# 4  
Old 03-29-2010
You peeps are awesome!
Thank you for the shell quoting refresher course!

Cheers!

Last edited by duderonomy; 03-30-2010 at 01:16 AM.. Reason: keeping it "G"! lol!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to split data with a delimiter having chars and special chars

Hi Team, I have a file a1.txt with data as follows. dfjakjf...asdfkasj</EnableQuotedIDs><SQL><SelectStatement modified='1' type='string'><! The delimiter string: <SelectStatement modified='1' type='string'><! dlm="<SelectStatement modified='1' type='string'><! The above command is... (7 Replies)
Discussion started by: kmanivan82
7 Replies

2. UNIX for Dummies Questions & Answers

Grep with special chars line by line

I have a file that includes strings with special characters, eg file1 line: 1 - special 1 line: = 4 line; -3 etc How can I grep the lines of file1 from file2, line by line? I used fgrep and egrep to grep a particular line and worked fine, but when I used: cat file1|while read line;do... (2 Replies)
Discussion started by: FelipeAd
2 Replies

3. Shell Programming and Scripting

How to erase junk Chars coming in only the first line

I have a file containing few thousands of lines. when I do cat on it , i find it having two special Chars at the start of first line alone as shown down here. ÿþHDR|20111024|01 If i delete this line and do a cat on file , the current first line is shown to have the same special Chars. ... (3 Replies)
Discussion started by: subramanian2008
3 Replies

4. Shell Programming and Scripting

sed - how to insert chars into a line

Hi I'm new to sed, and need to add characters into a specific location of a file, the fileds are tab seperated. text <tab> <tab> text <tab> text EOL I need to add more characters to the line to look like this: text <tab> <tab> newtext <tab> text <tab> text EOL Any ideas? (2 Replies)
Discussion started by: tangentviper
2 Replies

5. Shell Programming and Scripting

find 4 chars on 2nd line, 44 chars over

I know this should be simple, but I've been manning sed awk grep and find and am stupidly stumped :( I'm trying to use sed (or awk, find, etc) to find 4 characters on the second line of a file.txt 44-47 characters in. I can find lots of sed things for lines, but not characters. (4 Replies)
Discussion started by: unclecameron
4 Replies

6. Shell Programming and Scripting

insert new line at found chars

Hey gang, I have: XXZZXXZZXX 123 asdaffggh dfghyrgr ertyhdhh XXZZXXZZXX 234 sdg XXZZXXZZXX 456 gfg fggfd That is all on one line. Very simply put I want to do is something like: sed s'/XXZZXXZZXX /\n/g' or tr 'XXZZXXZZXX ' '/n' I have tried various things but can never get the desired... (6 Replies)
Discussion started by: crowman
6 Replies

7. Shell Programming and Scripting

Get multiple line content between two chars

Hello - I have a file that has the something like the following : REM CREATE TABLE lots of text REM table specifc creation text ; REM ALTER TABLE lots of text REM text specific to the the alter command REM could be more lines of text; What I need is to get all the lines for the ALTER... (2 Replies)
Discussion started by: Feliz
2 Replies

8. Shell Programming and Scripting

How to convert C source from 8bit chars to 16bit chars?

I was using the following bash command inside the emacs compile command to search C++ source code: grep -inr --include='*.h' --include='*.cpp' '"' * | sed "/include/d" | sed "/_T/d" | sed '/^ *\/\//d' | sed '/extern/d' Emacs will then position me in the correct file and at the correct line... (0 Replies)
Discussion started by: siegfried
0 Replies

9. Shell Programming and Scripting

to append few chars at the end of a file

hi i want to open a file at runtime append few chars at the end of each line all these i want to have done automatically how to do it (2 Replies)
Discussion started by: trichyselva
2 Replies

10. Shell Programming and Scripting

Using PERL to append chars to each line

I tried using SED to do this, but I'm not having any luck with it. See the previous thread here. I have a program called AMStracker (on OS X) that spits out the values of the motion sensor in the HDD. It has output that looks like this: . . 3 0 -75 3 0 -76 3 0 -77 . . I need to... (5 Replies)
Discussion started by: c0nn0r
5 Replies
Login or Register to Ask a Question