I am curious. If I have a line of code run in bash
and I would like to be able to print this with the newlines in tact, is there a way? Are the newlines actually there but stripped by
or
?
In Python you can:
Notice that the newlines are actually present when displaying VAR in the interactive shell
I'm mainly using this concept to generate DOC strings for each function I'm writting. Each function adds to this DOC string. Currently, I'm hand-formatting with
then running
when I want to print this.
I'd love to be able to:
and have this print this to screen with newlines.
I would like to get for variables what you can get with the below for generating files.
vs
to put a .list file in /etc/apt/sources.list.d for example
For a more complex example, this:
is much easier than
There's got to be a similar trick for creating variables and echoing them short of making a bunch of /tmp files and then cat'ing them using a similar technique as above (and IMHO makes the code much more readable)
Could someone please point me in the right direction?
The newlines are in the value of VAR, but after the shell substitutes VAR's value, the resulting string is split into words. The characters in the value of the IFS (internal field separator) variable -- which defaults to space, tab, newline -- determine which characters delimit words. So, your newlines are used to split the string into words which are then passed to echo as its arguments. What you see is the end result is echo doing its job, which is to print each of those words separated by a space and followed by a newline.
Solution: Wrap the variable in double quotes to prevent the word splitting step from occurring.
The newlines are in the value of VAR, but after the shell substitutes VAR's value, the resulting string is split into words. The characters in the value of the IFS (internal field separator) variable -- which defaults to space, tab, newline -- determine which characters delimit words. So, your newlines are used to split the string into words which are then passed to echo as its arguments. What you see is the end result is echo doing its job, which is to print each of those words separated by a space and followed by a newline.
Solution: Wrap the variable in double quotes to prevent the word splitting step from occurring.
Regards,
alister
Wow! I feel pretty stupid now. Why didn't I think about quoting? Works like a charm. Can't wait to use this in new apps!"
By the way, Narnie, unless you want the shell to expand variables and do command substitution insde your python function docstrings, use single quotes to ensure that all characters within it are taken literally. The only caveat being that you need to replace occurrences of ' with '\'' to get them in (this closes the single quoted string, backslash escapes a single quoted string, then reopens the single quoted string ... if you get that then you got shell quoting down ).
Using double quotes for assigning to DOC would have caused the shell to expand $var (if unset, a null string would have taken its place) and remove importantfile. I mention this just in case.
Hi All,
I am trying a shell script and need your help on taking the results to different output files.
I have tried the below code:
nawk '
{CNF = (length()-10)/7
printf "%9s", substr ($0, 1, 9)
for (i=0; i<=CNF; i++) T = substr ($0, 10+i*7, 7)
TMP = 100 - (T + T + T + T + T + T + T + T... (24 Replies)
Discussion started by: am24
24 Replies
2. Post Here to Contact Site Administrators and Moderators
Hi All,
I am trying to redirect the grep output to multiple files, can you please help with that.
Below is the command im using to match my pattern
grep \<proxyType\>$PxyType $DIR/EndureFiles.json > File_Name*.json
Note : $DIR and $PxyType is already defined in my script
Im able... (0 Replies)
Hi everybody,
I am trying to do the thing you see in the title, and I can't simply do
a=$(svn up)
echo $a
because the program (svn) gives output on lots of lines and in the variable the output is stored on only one line (resulting in a horribly formatted text). Any tips?
Thanks,... (2 Replies)
Well, it didn't take me long to get stumped again. I assure you that I'm not mentally deficient, just new to scripting.
So, here's the gist. I want to redirect output from awk based off of which branch of an if-else statement under which it falls.
#!/bin/bash
#some variables... (2 Replies)
Hi all,
I have two years worth of daily files (e.g. 090107.dat) and I'd like to write a BASH script to extract the same 2 columns from each of these files and copy them all to one separate file.
Could anyone please point me in the right direction as I'm new to shell scripting?
It would... (3 Replies)
Hello all,
I have the following 2 questions..
1) I would like to capture the output of an echo command to 2 different files at the same time. This does not seem to work. Any ideas?
echo ==== started on `date` ==== >> res1.log res2.log
2) Would it be possible to just get the 5th... (2 Replies)
Hello,
I have a set of data (comma seperated) that I want to save to multiple files according to one of the fields in the data set.
I can easily do this with the following script:
BEGIN { FS = OFS = ","}
NF {print $0 >> ($2 "" ".csv")}
It works perfectly on a set of dummy data I have set... (8 Replies)
Im currently using execl() to run the ls command and redirect the output to a text file. Unfortunately, when I check the text file, no information has been written to it.
I am calling execl() with the ls command like this
execl( "/bin/ls" , "-al" , '>' , "dirlist.txt" ,(char *) 0 );
... (5 Replies)
I am trying to take a list of filenames and copy those files from one directory into another. Is there a way I can do a 'more' of the file with the filenames and redirect or pipe the output into a 'cp' command. I am using a bash shell. I apologise in advance for what is probably a very simple... (2 Replies)
If I wanted to redirect output to multiple log files, what would be the best way to do that?
echo "Unix is awesome" >>unixgod.log >>unixgod.log (3 Replies)