---------- Post updated at 12:31 PM ---------- Previous update was at 12:19 PM ----------
Quote:
Originally Posted by protocomm
That's an extremely fragile and unreliable method for reading a file.
After the cat command substitution runs, its output undergoes field splitting. With the default IFS value, the distinction between single instances and multiple consecutive sequences of whitespace and blank lines will be lost.
After field splitting, pathname expansion is performed. If there are any pathname pattern matching metacharacters in the file, unintended globbing can occur. Normally these characters are just *, ?, and [, but if extended globbing is enabled, there are others. If nullglobbing is enabled, words can be lost.
You cannot defend against these expansions because any quoting places the entire file's contents in the variable at once, yielding at most a single iteration.
You should never use that approach ... ever. A safe way to read a file's lines into a variable:
Regards,
Alister
---------- Post updated at 12:31 PM ---------- Previous update was at 12:19 PM ----------
That's an extremely fragile and unreliable method for reading a file.
After the cat command substitution runs, its output undergoes field splitting. With the default IFS value, the distinction between single instances and multiple consecutive sequences of whitespace and blank lines will be lost.
After field splitting, pathname expansion is performed. If there are any pathname pattern matching metacharacters in the file, unintended globbing can occur. Normally these characters are just *, ?, and [, but if extended globbing is enabled, there are others. If nullglobbing is enabled, words can be lost.
You cannot defend against these expansions because any quoting places the entire file's contents in the variable at once, yielding at most a single iteration.
You should never use that approach ... ever. A safe way to read a file's lines into a variable:
Regards,
Alister
Yes it's true, i have understood my beginner error, i wrote this command line so quickly….thx
Last edited by protocomm; 05-03-2014 at 05:53 PM..
Don't understand, can you give me an example when this command line doesn't work ? if i double quote the command subshell cat, i can read file with spaces ?
If the following script is problems.sh:
Its output:
Note how your code expands the *.txt into file.txt. Note also how the blank lines are lost. And finally, obeserve how it splits the single line "a b _052014.txt" into three lines.
The quoted version fails because jams the entire file into the variable and the loop only iterates once. It also needs to quote the $line in the echo statement (this is the reason that *.txt still becomes file.txt).
My file (the output of an experiment) starts off looking like this,
_____________________________________________________________
Subjects incorporated to date: 001
Data file started on machine PKSHS260-05CP
**********************************************************************
Subject 1,... (9 Replies)
I'm trying to fully escape/quote a directory path for use in a regular expression with no characters interpreted symbolically. Printable characters get \* and white space gets "*"
$ inputDirectory="/home/dir1/dir/"
$ echo $( { while read -r -n1 a; do ] ]] && echo -n "\"""$a""\"" || echo -n... (7 Replies)
Hi,
I have a file which is an extract of jil codes of all autosys jobs in our server.
Sample jil code:
**************************
permission:gx,wx
date_conditions:yes
days_of_week:all
start_times:"05:00"
condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Platform: Solaris 10
I have a file like below
$ cat languages.txt
Spanish
Norwegian
English
Persian
German
Portugese
Chinese
Korean
Hindi
Malayalam
Bengali
Italian
Greek
Arabic
I want to append the string " is a great language" at end of each line in this file. (3 Replies)
Dear All
I am having a text file which is having more than 200 lines.
EX:
001010122 12000 BIB 12000 11200 1200003
001010122 2000 AND 12000 11200 1200003
001010122 12000 KVB 12000 11200 1200003
In the above file i want to search for string KVB... (5 Replies)
I have a file that looks like:
file1:
www_blank_com 20121008153552
www_blank_com 20121008162542
www_blank_com 20121009040540
www_blank_com 20121009041542
www_blank_com 20121010113548
www_blank_com 20121011113551
www_blank_com 20121012113542
I want the new file to show the day of... (3 Replies)
Hi,
My requirement is to append a date in format DDMMYYYYHHMISS at the end of first line of file which is HEADER. I am trying command
sed -i '1s/.*/&<date_format>/' <file_name>
Where <date_format>=`date +%m%d%Y%H%M%S`
I am somehow misisng the right quotes ti get this added in above... (2 Replies)
Hello
How can I add a string (always the same) at the end of a specific line in a file...
The file is:
000000001 041 L $$aspa
000000001 088 L $$aJ.E.N. 551
000000001 090 L $$aINFORMES JEN
000000001 100 L $$aautor 1
----
000000002 041 L $$aeng
000000002 088 L $$aJ.E.N. 1... (13 Replies)
I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed).
AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC
MY * A02 RESALE 6990788000 Y... (3 Replies)
#!/bin/ksh
if test -f file6.txt
then
rm file6.txt
fi
a=`date +"%F"`
awk '{print $0,"$a"}' file3.txt > file6.txt
-----------------------------------------------------------------
i need to append date at the end of each line in file 3 and o/p it to file6.txt (3 Replies)