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:
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:
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:
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:
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)
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)
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)
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)
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)
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)
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)
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)
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)