Sponsored Content
Top Forums Shell Programming and Scripting why am I losing line-end chars Post 302408546 by bakunin on Monday 29th of March 2010 10:13:05 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Template::Plugin::YAML(3pm)				User Contributed Perl Documentation			       Template::Plugin::YAML(3pm)

NAME
Template::Plugin::YAML - Plugin interface to YAML SYNOPSIS
[% USE YAML %] [% YAML.dump(variable) %] [% YAML.dump_html(variable) %] [% value = YAML.undump(yaml_string) %] [% YAML.dumpfile(filename, variable) %] [% value = YAML.undumpfile(filename) %] DESCRIPTION
This is a simple Template Toolkit Plugin Interface to the YAML module. A YAML object will be instantiated via the following directive: [% USE YAML %] As a standard plugin, you can also specify its name in lower case: [% USE yaml %] METHODS
These are the methods supported by the YAML object. dump( @variables ) Generates a raw text dump of the data structure(s) passed [% USE Dumper %] [% yaml.dump(myvar) %] [% yaml.dump(myvar, yourvar) %] dump_html( @variables ) Generates a dump of the data structures, as per "dump", but with the characters <, > and & converted to their equivalent HTML entities, spaces converted to &nbsp; and newlines converted to <br>. [% USE yaml %] [% yaml.dump_html(myvar) %] undump( $string ) Converts a YAML-encoded string into the equivalent data structure. Here's a way to deep-copy a complex structure by completely serializing the data. [% USE yaml; yaml_string = yaml.dump(complex_data_structure); complex_copy = yaml.undump(yaml_string); %] dumpfile( $file, @variables ) Converts the data to YAML encoding, and dumps it to the specified filepath. [% USE yaml; yaml.dumpfile(".storage", my_data) %] undumpfile( $file ) Loads the YAML-encoded data from the specified filepath [% USE yaml; my_data = yaml.undumpfile(".storage") %] AUTHORS
Richard Clamp <richardc@unixbeard.net>, with undump, undumpfile, and dumpfile implementation by Randal L. Schwartz <merlyn@stonehenge.com> based on Simon Matthews' Template::Plugin::Dumper COPYRIGHT
Copyright 2003, 2008 Richard Clamp All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
Template::Plugin::Dumper perl v5.10.0 2008-12-15 Template::Plugin::YAML(3pm)
All times are GMT -4. The time now is 07:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy